Advertisement
Guest User

Untitled

a guest
Nov 13th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.89 KB | None | 0 0
  1.  
  2.  
  3. // *********************************************************
  4. // Implementation file Heap.cpp for the ADT heap.
  5. // *********************************************************
  6. #include "Heap.h"  // header file for heap
  7.  
  8. heapClass::heapClass() : Size(0)
  9. {
  10. }  // end default constructor
  11.  
  12. bool heapClass::HeapIsEmpty() const
  13. {
  14.     return bool(Size == 0);
  15. }  // end HeapIsEmpty
  16.  
  17. void heapClass::HeapInsert(const heapItemType& NewItem,
  18.                            bool& Success)
  19. // Method: Inserts the new item after the last item in the
  20. // heap and trickles it up to its proper position. The
  21. // heap is full when it contains MAX_HEAP items.
  22. {
  23.     Success = bool(Size < MAX_HEAP);
  24.    
  25.     if (Success)
  26.     {  // place the new item at the end of the heap
  27.         Items[Size] = NewItem;
  28.        
  29.         // trickle new item up to its proper position
  30.         int Place = Size;
  31.         int Parent = (Place - 1)/2;
  32.         while ( (Parent >= 0) &&
  33.                (Items[Place].Key() > Items[Parent].Key()) )
  34.         {  // swap Items[Place] and Items[Parent]
  35.             heapItemType Temp = Items[Parent];
  36.             Items[Parent] = Items[Place];
  37.             Items[Place] = Temp;
  38.            
  39.             Place = Parent;
  40.             Parent = (Place -1 )/2;
  41.         }  // end while
  42.        
  43.         ++Size;
  44.     }  // end if
  45. }  // end HeapInsert
  46.  
  47. void heapClass::HeapDelete(heapItemType& RootItem,
  48.                            bool& Success)
  49. // Method: Swaps the last item in the heap with the root
  50. // and trickles it down to its proper position.
  51. {
  52.     Success = bool(!HeapIsEmpty());
  53.    
  54.     if (Success)
  55.     {  RootItem = Items[0];
  56.         Items[0] = Items[--Size];
  57.         RebuildHeap(0);
  58.     }  // end if
  59. }  // end HeapDelete
  60.  
  61. void heapClass::RebuildHeap(int Root)
  62. {
  63.     // if the root is not a leaf and the root's search key
  64.     // is less than the larger of the search keys in the
  65.     // root's children
  66.     int Child = 2 * Root + 1;  // index of root's left
  67.     // child, if any
  68.     if ( Child < Size )
  69.     {  // root is not a leaf, so it has a left child at Child
  70.         int RightChild = Child + 1;  // index of right child,
  71.         // if any
  72.        
  73.         // if root has a right child, find larger child
  74.         if ( (RightChild < Size) &&
  75.             (Items[RightChild].Key() > Items[Child].Key()) )
  76.             Child = RightChild;  // index of larger child
  77.        
  78.         // if the root's value is smaller than the
  79.         // value in the larger child, swap values
  80.         if ( Items[Root].Key() < Items[Child].Key() )
  81.         {  heapItemType Temp = Items[Root];
  82.             Items[Root] = Items[Child];
  83.             Items[Child] = Temp;
  84.            
  85.             // transform the new subtree into a heap
  86.             RebuildHeap(Child);
  87.         }  // end if
  88.     }  // end if
  89.    
  90.     // if root is a leaf, do nothing
  91. }  // end RebuildHeap
  92. // End of implementation file.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement