9.3 Heaps 1. definition: A max heap(min Heap) is a complete binary tree The value in each node is greater(less)than or equal to those in its children(if any)
9.3 Heaps 1.definition: A max heap(min Heap) • is A complete binary tree • The value in each node is greater(less) than or equal to those in its children(if any)
9.3 Heaps For example: an max heap k={87,78,53456509,31,1723} 87 78 650931
9.3 Heaps • For example:an max heap k={87,78,53,45,65,09,31,17,23} 87 78 45 65 53 09 17 31 23
9.3 Heaps Example of min heap k={09,1765,2345,78875331} 09 0765 3)4578⑧7 53)(31
9.3 Heaps • Example of min heap k={09,17,65,23,45,78,87,53,31} 09 17 23 45 65 78 53 87 31
9.3 Heaps class M ahEap Data member of heap T* heap; int MaxSize, currentsize template<class T>class Maxheap i public MaxHeap(int MaxHeap Size=10); MaxHeapoidelete[heap int sizeOconst(return currentsize
9.3 Heaps 2.class MaxHeap Data member of heap: T * heap; int MaxSize, currentSize template<class T>class Maxheap { public: MaxHeap(int MaxHeapSize=10); ~MaxHeap(){delete[]heap;} int size()const{return currentSize;}
9.3 Heaps T Maxfif( Currentsize==O)throw OutofBoundso: return heap[1]; j MaxHeap <t>&insert(const T&x) MaxHeap<t>& deleteMax(t& x) void initialize(t all, int size, int Array Size); private int CurrentSize, Maxsize T* heap
9.3 Heaps T Max(){if (CurrentSize==0)throw OutOfBounds(); return heap[1];} MaxHeap<T>&insert(const T&x); MaxHeap<T>& DeleteMax(T& x); void initialize(T a[], int size, int ArraySize); private: int CurrentSize, MaxSize; T * heap; }