| | |
Writing a Heap
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi
I'm trying to write a heap data structure but I have no idea how to do this.Could Someone help?
thanks
I'm trying to write a heap data structure but I have no idea how to do this.Could Someone help?
thanks
My heart hates death
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
I tried to write a Heap in C++ but I couldn't so I googled about it but I found nothing useful so I came here to know that is there somebody who can help me to write a binary heap data structure that can implement both max and min heap in C++.
(Was it proper?)
(Was it proper?)
My heart hates death
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
I simply wrote nothing.I just wanted to ask how to write a heap but you are confusing me and yourself with your posts.Just tell me how to write a heap data structure class in C++.I don't know what more do you want to know.
My heart hates death
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
OK.I found a code one the internet with a lot of suffer(your lazy link was forbidden for me niek)And I changed it to be generic and overloaded [] operator for it,too.Here's the code:
With the main file like below:
I get the don't send message when I execute the resulting exe file.
And I made the for-loop line a comment and there was no problem.But I couldn't find what is wrong with it.
Could you tell what is the problem?
c++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<stdio.h> #include<stdlib.h> #include <iomanip.h> #include<time.h> template <typename type> class Heap{ public: Heap(int IntArray[], int Count); Heap(int MAXSIZE); type Delete(); void Insert(type Item); type FindMax(); type FindMin(); bool IsEmpty(){ //IS EMPTY FUNCTION return HeapSize == 0; } void SORT(type IntAarray[], int MAX); void Remove(type item); type operator[](int index){ return HeapArrayPtr[index]; } private: type * HeapArrayPtr; // pointer to array holding the heap data int MaxHeapSize; int HeapSize; void FilterDown(int StartIndex); void FilterUp(int StartIndex); int Parent(int CurrentIndex){ return (CurrentIndex - 1) / 2; } int RightChild(int CurrentIndex){ return 2 * (CurrentIndex + 1); } int LeftChild(int CurrentIndex){ return 2 * CurrentIndex + 1; } }; //consrurctors template <typename type> Heap<type>::Heap(int MAXSIZE){ int *HeapArrayPtr = new int[MAXSIZE] ; MaxHeapSize = MAXSIZE; HeapSize=0; } template <typename type> Heap<type>::Heap(int IntArray[], int Count){ int CurrentPosition; if (Count <= 0){ cerr << "Cannot construct a heap with size 0 or less." << endl; exit(1); } MaxHeapSize = Count; HeapSize = Count; HeapArrayPtr = IntArray;// a pointer assignment statement CurrentPosition = (HeapSize - 2) / 2;// Set CurrentPosition to the last index of a parent node: while (CurrentPosition >= 0){// Get heap condition for subtree rooted at index CurrentPosition: FilterDown(CurrentPosition); CurrentPosition--; } } //DELETE() template <typename type> type Heap<type>::Delete(){ int Temp; if (HeapSize == 0){ cerr << "Cannot remove from an empty heap" << endl; exit(1); } Temp = HeapArrayPtr[0]; // Item at index 0 is the smallest HeapArrayPtr[0] = HeapArrayPtr[HeapSize - 1];// copy last one to root: HeapSize--; FilterDown(0); // readjust to be a heap return Temp; } //FILTER DOWN() template <typename type> void Heap<type>::FilterDown(int StartIndex){ int CurrentPosition, ChildPosition, RightChildPosition, Target; CurrentPosition = StartIndex; Target = HeapArrayPtr[StartIndex]; ChildPosition = LeftChild(CurrentPosition); while (ChildPosition < HeapSize){ RightChildPosition = ChildPosition + 1;// Set ChildPosition to index of smaller of right, left children: if ((RightChildPosition < HeapSize) && (HeapArrayPtr[RightChildPosition] <= HeapArrayPtr[ChildPosition])) ChildPosition = RightChildPosition; if (Target <= HeapArrayPtr[ChildPosition]) break; // Get out of loop, heap OK with Target at CurrentPosition HeapArrayPtr[CurrentPosition] = HeapArrayPtr[ChildPosition];// Move value of the smaller child to the parent node: CurrentPosition = ChildPosition;// Move down the tree: ChildPosition = LeftChild(CurrentPosition); } HeapArrayPtr[CurrentPosition] = Target;// Put Target into the correct location: } //FILTER UP() template <typename type> void Heap<type>::FilterUp(int StartIndex){ int CurrentPosition, ParentPosition; type Target; CurrentPosition = StartIndex; ParentPosition = Parent(CurrentPosition); Target = HeapArrayPtr[StartIndex]; while (CurrentPosition != 0){ if (HeapArrayPtr[ParentPosition] <= Target) break; // Get out of loop, heap OK with Target at CurrentPosition HeapArrayPtr[CurrentPosition] = HeapArrayPtr[ParentPosition];// Move parent value to child: CurrentPosition = ParentPosition;// Move up in the tree: ParentPosition = Parent(CurrentPosition); } HeapArrayPtr[CurrentPosition] = Target;// Place Target at the position located for it: } //INSERT() template <typename type> void Heap<type>::Insert(type Item){ if (HeapSize == MaxHeapSize){ cerr << "Cannot insert into a full heap" << endl; exit(1); } HeapArrayPtr[HeapSize] = Item;// At first, place item at the end of the heap: FilterUp(HeapSize); // restore heap property HeapSize++; } //SORT() template <typename type> void Heap<type>::SORT(type IntArray[], int MAX){ type Smallest; int a; Heap (IntArray, MAX); // constructor makes IntArray a heap for (a = MAX - 1; a >= 1; a--){ Smallest = Delete();// Remove smallest item and place at index k IntArray[a] = Smallest;// At this point IntArray[0] contains the largest item by default } cout<<"print data in descending order"<<endl; for (int k = 0; k <MAX; k++){ cout << setw(10) << IntArray[k]; } cout << endl; } //FindMin() template <typename type> type Heap<type>::FindMin(){ type Temp; if (HeapSize == 0){ cerr << "empty heap" << endl; exit(1); } return Temp = HeapArrayPtr[0]; // Item at index 0 is the smallest } //FINDMax() template <typename type> type Heap<type>::FindMax(){ type temp1; temp1=HeapArrayPtr[0]; if (HeapSize == 0){ cerr << "empty heap" << endl; exit(1); } for(int i=1; i<MaxHeapSize; i++){ if(temp1<HeapArrayPtr[i]) temp1=HeapArrayPtr[i]; } return temp1; } //Remove() template <typename type> void Heap<type>::Remove(type item){ for(int i=0; i<MaxHeapSize; i++){ if(HeapArrayPtr[i]==item){ HeapArrayPtr[i] = HeapArrayPtr[HeapSize - 1]; // HeapSize--; } } Heap(HeapArrayPtr, MaxHeapSize-1); }
c++ Syntax (Toggle Plain Text)
#include "heap.h" int main(){ Heap<int> h(10); for(int i=1;i<=10;++i) h.Insert(i); cout<<h.IsEmpty(); int max=h.FindMax(); int min=h.FindMin(); for(int i=1;i<=10;++i) cout<<h[i]; h.Remove(20); int a[10],b=10; h.SORT(a,b); h.Delete(); Heap<int> g(a,b); return 0; }
And I made the for-loop line a comment and there was no problem.But I couldn't find what is wrong with it.
Could you tell what is the problem?
Last edited by pywriter; Jul 17th, 2009 at 2:51 pm.
My heart hates death
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
Hey I think with that big code,the question can't be seen but please pay attention there is a question in the post above!
My heart hates death
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
but in the moment that the morning of life is dark,
and there is a war between the good and the evil
being sank by the mouth of death,is sweet.
and yes that is worthy for chilvary
![]() |
Similar Threads
- Memory Allocation Error, writing past end of heap! (C++)
- HEAP CORRUPTION for no apparent reason (C++)
- _popen not allowing writing to process (C++)
- Article Writing Services (Post your Resume)
- Move up/down in a tree (C++)
- Heap sort, a little help (C)
Other Threads in the C++ Forum
- Previous Thread: Set operations
- Next Thread: Extract Lines of text from File
Views: 437 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






