Writing a Heap

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Writing a Heap

 
0
  #1
Jul 17th, 2009
Hi
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Writing a Heap

 
0
  #2
Jul 17th, 2009
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Re: Writing a Heap

 
0
  #3
Jul 17th, 2009
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?)
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,973
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Writing a Heap

 
0
  #4
Jul 17th, 2009
>I tried to write a Heap in C++ but I couldn't
Can you at least post what you've tried then?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Re: Writing a Heap

 
0
  #5
Jul 17th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,973
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Writing a Heap

 
0
  #6
Jul 17th, 2009
I want to know what exactly the "heap" you plan to write in C++ involves?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,996
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Writing a Heap

 
0
  #7
Jul 17th, 2009
Originally Posted by pywriter View Post
.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 niek_e; Jul 17th, 2009 at 8:09 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Re: Writing a Heap

 
0
  #8
Jul 17th, 2009
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:
  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:
  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 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Re: Writing a Heap

 
0
  #9
Jul 18th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 94
Reputation: pywriter is an unknown quantity at this point 
Solved Threads: 0
pywriter's Avatar
pywriter pywriter is offline Offline
Junior Poster in Training

Re: Writing a Heap

 
-1
  #10
Jul 20th, 2009
And one more question.How should I pronounce the first name of C++'s inventor,Bjarne strostroup?
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 437 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC