943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1251
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2009
0

Writing a Heap

Expand Post »
Hi
I'm trying to write a heap data structure but I have no idea how to do this.Could Someone help?
thanks
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008
Jul 17th, 2009
0

Re: Writing a Heap

Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 17th, 2009
0

Re: Writing a Heap

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?)
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008
Jul 17th, 2009
0

Re: Writing a Heap

>I tried to write a Heap in C++ but I couldn't
Can you at least post what you've tried then?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 17th, 2009
0

Re: Writing a Heap

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.
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008
Jul 17th, 2009
0

Re: Writing a Heap

I want to know what exactly the "heap" you plan to write in C++ involves?
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 17th, 2009
0

Re: Writing a Heap

Click to Expand / Collapse  Quote originally posted by pywriter ...
.Just tell me how to write a heap data structure class in C++.
heap
Also look here
And a
lazy link


If you want descent information, ask a descent question.
Last edited by Nick Evan; Jul 17th, 2009 at 8:09 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jul 17th, 2009
0

Re: Writing a Heap

OK.I found a code one the internet with a lot of suffer(your lazy link was forbidden for me nick)And I changed it to be generic and overloaded [] operator for it,too.Here's the code:
c++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include <iomanip.h>
  5. #include<time.h>
  6.  
  7. template <typename type> class Heap{
  8. public:
  9.  
  10. Heap(int IntArray[], int Count);
  11.  
  12. Heap(int MAXSIZE);
  13.  
  14. type Delete();
  15.  
  16. void Insert(type Item);
  17.  
  18. type FindMax();
  19.  
  20. type FindMin();
  21.  
  22. bool IsEmpty(){ //IS EMPTY FUNCTION
  23. return HeapSize == 0;
  24. }
  25.  
  26. void SORT(type IntAarray[], int MAX);
  27.  
  28. void Remove(type item);
  29.  
  30. type operator[](int index){
  31. return HeapArrayPtr[index];
  32. }
  33.  
  34. private:
  35.  
  36. type * HeapArrayPtr; // pointer to array holding the heap data
  37.  
  38. int MaxHeapSize;
  39.  
  40. int HeapSize;
  41.  
  42. void FilterDown(int StartIndex);
  43.  
  44. void FilterUp(int StartIndex);
  45.  
  46. int Parent(int CurrentIndex){
  47. return (CurrentIndex - 1) / 2;
  48. }
  49.  
  50. int RightChild(int CurrentIndex){
  51. return 2 * (CurrentIndex + 1);
  52. }
  53.  
  54. int LeftChild(int CurrentIndex){
  55. return 2 * CurrentIndex + 1;
  56. }
  57.  
  58. };
  59.  
  60. //consrurctors
  61. template <typename type> Heap<type>::Heap(int MAXSIZE){
  62.  
  63. int *HeapArrayPtr = new int[MAXSIZE] ;
  64. MaxHeapSize = MAXSIZE;
  65. HeapSize=0;
  66. }
  67.  
  68. template <typename type> Heap<type>::Heap(int IntArray[], int Count){
  69.  
  70. int CurrentPosition;
  71. if (Count <= 0){
  72. cerr << "Cannot construct a heap with size 0 or less." << endl;
  73. exit(1);
  74. }
  75.  
  76. MaxHeapSize = Count;
  77. HeapSize = Count;
  78. HeapArrayPtr = IntArray;// a pointer assignment statement
  79. CurrentPosition = (HeapSize - 2) / 2;// Set CurrentPosition to the last index of a parent node:
  80.  
  81. while (CurrentPosition >= 0){// Get heap condition for subtree rooted at index CurrentPosition:
  82. FilterDown(CurrentPosition);
  83. CurrentPosition--;
  84. }
  85. }
  86.  
  87. //DELETE()
  88. template <typename type> type Heap<type>::Delete(){
  89.  
  90. int Temp;
  91. if (HeapSize == 0){
  92. cerr << "Cannot remove from an empty heap" << endl;
  93. exit(1);
  94. }
  95.  
  96. Temp = HeapArrayPtr[0]; // Item at index 0 is the smallest
  97. HeapArrayPtr[0] = HeapArrayPtr[HeapSize - 1];// copy last one to root:
  98. HeapSize--;
  99. FilterDown(0); // readjust to be a heap
  100. return Temp;
  101. }
  102.  
  103. //FILTER DOWN()
  104. template <typename type> void Heap<type>::FilterDown(int StartIndex){
  105.  
  106. int CurrentPosition, ChildPosition, RightChildPosition, Target;
  107. CurrentPosition = StartIndex;
  108. Target = HeapArrayPtr[StartIndex];
  109. ChildPosition = LeftChild(CurrentPosition);
  110.  
  111. while (ChildPosition < HeapSize){
  112. RightChildPosition = ChildPosition + 1;// Set ChildPosition to index of smaller of right, left children:
  113.  
  114. if ((RightChildPosition < HeapSize) && (HeapArrayPtr[RightChildPosition] <= HeapArrayPtr[ChildPosition]))
  115. ChildPosition = RightChildPosition;
  116.  
  117. if (Target <= HeapArrayPtr[ChildPosition])
  118. break; // Get out of loop, heap OK with Target at CurrentPosition
  119.  
  120. HeapArrayPtr[CurrentPosition] = HeapArrayPtr[ChildPosition];// Move value of the smaller child to the parent node:
  121. CurrentPosition = ChildPosition;// Move down the tree:
  122. ChildPosition = LeftChild(CurrentPosition);
  123. }
  124. HeapArrayPtr[CurrentPosition] = Target;// Put Target into the correct location:
  125. }
  126.  
  127. //FILTER UP()
  128. template <typename type> void Heap<type>::FilterUp(int StartIndex){
  129.  
  130. int CurrentPosition, ParentPosition;
  131. type Target;
  132. CurrentPosition = StartIndex;
  133. ParentPosition = Parent(CurrentPosition);
  134. Target = HeapArrayPtr[StartIndex];
  135.  
  136. while (CurrentPosition != 0){
  137. if (HeapArrayPtr[ParentPosition] <= Target)
  138. break; // Get out of loop, heap OK with Target at CurrentPosition
  139.  
  140. HeapArrayPtr[CurrentPosition] = HeapArrayPtr[ParentPosition];// Move parent value to child:
  141. CurrentPosition = ParentPosition;// Move up in the tree:
  142. ParentPosition = Parent(CurrentPosition);
  143. }
  144. HeapArrayPtr[CurrentPosition] = Target;// Place Target at the position located for it:
  145. }
  146.  
  147. //INSERT()
  148. template <typename type> void Heap<type>::Insert(type Item){
  149.  
  150. if (HeapSize == MaxHeapSize){
  151. cerr << "Cannot insert into a full heap" << endl;
  152. exit(1);
  153. }
  154.  
  155. HeapArrayPtr[HeapSize] = Item;// At first, place item at the end of the heap:
  156. FilterUp(HeapSize); // restore heap property
  157. HeapSize++;
  158. }
  159.  
  160. //SORT()
  161. template <typename type> void Heap<type>::SORT(type IntArray[], int MAX){
  162.  
  163. type Smallest;
  164. int a;
  165. Heap (IntArray, MAX); // constructor makes IntArray a heap
  166.  
  167. for (a = MAX - 1; a >= 1; a--){
  168. Smallest = Delete();// Remove smallest item and place at index k
  169. IntArray[a] = Smallest;// At this point IntArray[0] contains the largest item by default
  170. }
  171.  
  172. cout<<"print data in descending order"<<endl;
  173.  
  174. for (int k = 0; k <MAX; k++){
  175. cout << setw(10) << IntArray[k];
  176. }
  177.  
  178. cout << endl;
  179. }
  180.  
  181. //FindMin()
  182.  
  183. template <typename type> type Heap<type>::FindMin(){
  184. type Temp;
  185.  
  186. if (HeapSize == 0){
  187. cerr << "empty heap" << endl;
  188. exit(1);
  189. }
  190.  
  191. return Temp = HeapArrayPtr[0]; // Item at index 0 is the smallest
  192. }
  193.  
  194. //FINDMax()
  195. template <typename type> type Heap<type>::FindMax(){
  196.  
  197. type temp1;
  198. temp1=HeapArrayPtr[0];
  199.  
  200. if (HeapSize == 0){
  201. cerr << "empty heap" << endl;
  202. exit(1);
  203. }
  204.  
  205. for(int i=1; i<MaxHeapSize; i++){
  206.  
  207. if(temp1<HeapArrayPtr[i])
  208. temp1=HeapArrayPtr[i];
  209.  
  210. }
  211. return temp1;
  212. }
  213.  
  214. //Remove()
  215. template <typename type> void Heap<type>::Remove(type item){
  216.  
  217. for(int i=0; i<MaxHeapSize; i++){
  218. if(HeapArrayPtr[i]==item){
  219. HeapArrayPtr[i] = HeapArrayPtr[HeapSize - 1];
  220. // HeapSize--;
  221. }
  222. }
  223.  
  224. Heap(HeapArrayPtr, MaxHeapSize-1);
  225. }
With the main file like below:
c++ Syntax (Toggle Plain Text)
  1. #include "heap.h"
  2. int main(){
  3. Heap<int> h(10);
  4. for(int i=1;i<=10;++i) h.Insert(i);
  5. cout<<h.IsEmpty();
  6. int max=h.FindMax();
  7. int min=h.FindMin();
  8. for(int i=1;i<=10;++i) cout<<h[i];
  9. h.Remove(20);
  10. int a[10],b=10;
  11. h.SORT(a,b);
  12. h.Delete();
  13. Heap<int> g(a,b);
  14. return 0;
  15. }
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?
Last edited by Nick Evan; Jan 25th, 2011 at 6:46 am.
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008
Jul 18th, 2009
0

Re: Writing a Heap

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!
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008
Jul 20th, 2009
-1

Re: Writing a Heap

And one more question.How should I pronounce the first name of C++'s inventor,Bjarne strostroup?
Reputation Points: 13
Solved Threads: 0
Junior Poster
pywriter is offline Offline
115 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Authenticate without password on a domain
Next Thread in C++ Forum Timeline: Extract Lines of text from File





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC