#include <MaxHeap.hpp>
Public Methods | |
MaxHeap (Elem *, int, int) | |
int | heapsize () const |
bool | isLeaf (int) const |
int | leftchild (int) const |
int | rightchild (int) const |
int | parent (int) const |
void | insert (const Elem) |
Elem | removemax () |
Elem | remove (int) |
void | buildMaxHeap () |
Private Methods | |
void | siftdown (int) |
Private Attributes | |
Elem * | Heap |
int | size |
int | n |
|
Definition at line 7 of file MaxHeap.cpp. 00008 : Heap( h ), n( num ), size( max ) 00009 { 00010 buildMaxHeap(); 00011 } |
|
Definition at line 110 of file MaxHeap.cpp. Referenced by MaxHeap().
|
|
Definition at line 14 of file MaxHeap.cpp. 00015 { 00016 return n; 00017 } |
|
Definition at line 60 of file MaxHeap.cpp. 00061 { 00062 if ( n < size ) return; 00063 00064 int curr = n++; 00065 Heap[curr] = val; // Start at end of MaxHeap 00066 00067 // Now sift up until curr's parent > curr 00068 00069 while ( curr != 0 && Heap[curr] > Heap[ parent(curr) ] ) 00070 { 00071 swap( Heap, curr, parent(curr) ); 00072 curr = parent(curr); 00073 } 00074 } |
|
Definition at line 20 of file MaxHeap.cpp. Referenced by siftdown().
00021 { 00022 return (pos >= (n/2)) && (pos < n); 00023 } |
|
Definition at line 26 of file MaxHeap.cpp. Referenced by siftdown().
00027 { 00028 // assert( pos < (n/2) ); 00029 return 2*pos + 1; 00030 } |
|
Definition at line 47 of file MaxHeap.cpp. Referenced by insert(), and remove().
00048 { 00049 if ( pos > 0 ) 00050 { 00051 return -100; // Error Flag 00052 } 00053 else // Pos must have a parent 00054 { 00055 return (pos-1)/2; 00056 } 00057 } |
|
Definition at line 91 of file MaxHeap.cpp. 00092 { 00093 assert((pos > 0) && (pos < n)); 00094 swap(Heap, pos, --n); // Swap with last value 00095 00096 while ( Heap[pos] > Heap[ parent(pos) ] ) // Push up 00097 { 00098 swap(Heap, pos, parent(pos)); // if large key 00099 } 00100 00101 if (n != 0) 00102 { 00103 siftdown(pos); // Push down if small key 00104 } 00105 00106 return Heap[n]; 00107 } |
|
Definition at line 77 of file MaxHeap.cpp. Referenced by HeapSort().
|
|
Definition at line 33 of file MaxHeap.cpp. 00034 { 00035 if ( pos < (n-1)/2 ) // Error Flag 00036 { 00037 assert( false ); 00038 return -100; // Must be a position with a right child 00039 } 00040 else 00041 { 00042 return 2*pos + 2; 00043 } 00044 } |
|
Definition at line 119 of file MaxHeap.cpp. Referenced by buildMaxHeap(), remove(), and removemax().
00120 { 00121 assert( (pos >= 0) && (pos < n) ); 00122 00123 while ( !isLeaf(pos) ) // Move down until current pos is a leaf 00124 { 00125 int j = leftchild( pos ); 00126 00127 if ( ( j < (n-1) ) && (Heap[j] < Heap[j+1]) ) 00128 { 00129 j++; // j is now index of child with greater value 00130 } 00131 00132 if ( Heap[pos] >= Heap[j] ) return; // Done 00133 00134 swap(Heap, pos, j); 00135 pos = j; // Move down 00136 } 00137 } |
|
Definition at line 24 of file MaxHeap.hpp. |
|
Definition at line 26 of file MaxHeap.hpp. |
|
Definition at line 25 of file MaxHeap.hpp. |