Mastering Heaps: Key Concepts and Implementations Explained
School
University of Illinois, Urbana Champaign**We aren't endorsed by this school
Course
CS 225
Subject
Computer Science
Date
Dec 12, 2024
Pages
33
Uploaded by DukeStraw14390
Department of Computer ScienceData StructuresHeaps AnalysisOctober 14, 2024 CS 225 Brad Solomon
Exam 3 (10/23 — 10/25)Autograded MC and one coding questionManually graded short answer promptPractice exam on PLTopics covered can be found on websiteRegistration started October 10https://courses.engr.illinois.edu/cs225/fa2024/exams/
Learning ObjectivesReview the heap data structureDiscuss heap ADT implementationsProve the runtime of the heap
(min)HeapBy storing as a complete tree, can avoid using pointers at all!If index starts at 1:leftChild(i): 2irightChild(i): 2i+1parent(i): floor(i/2)51594672045615972012345670
(min)HeapBy storing as a complete tree, can avoid using pointers at all!If Index starts at 0:leftChild(i): 2i+1rightChild(i): 2(i+1)parent(i): floor((i-1)/2)51594672045615972012345670
Implementation of heap array45615972016251412111234567891011120131514T* StartT* SizeT* Capacitysize_t Startsize_t Sizesize_t CapacityArray List (Pointer implementation)Array List (Index implementation)16
insert - heapifyUptemplate <class T> void Heap<T>::_insert(const T & key) { // Check to ensure there’s space to insert an element // ...if not, grow the array if ( size_ == capacity_ ) { _growArray(); } // Insert the new element at the end of the array item_[size_++] = key; // Restore the heap property _heapifyUp(size_ - 1); }1 2 3 4 5 6 7 8 9 10 11 12515946745615971234567template <class T> void Heap<T>::_heapifyUp( size_t index ) { if ( index > 1 ) { if ( item_[index] < item_[ parent(index) ] ) { std::swap( item_[index], item_[ parent(index) ] ); _heapifyUp( parent(index) ); // index / 2; } } }1 2 3 4 5 6 7 8 9 10 110
removeMin51592546720111612144561597201625141211What is the Big O of array remove?What else can we do?
removeMin51592546720111612144561597201625141211
removeMin9151125567201612145961511720162514121) Swap root with last item2) HeapifyDown( ) root(and remove) (and modify size)
template <class T> T Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_ - 1]; size--; // Restore the heap property _heapifyDown(); // Return the minimum value return minValue; }1 2 3 4 5 6 7 8 9 10 11 12 13removeMin51592546720111612144561597201625141211
template <class T> T Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_ - 1]; size--; // Restore the heap property _heapifyDown(1); // Return the minimum value return minValue; }removeMin - heapifyDown1 2 3 4 5 6 7 8 9 10 11 12 13template <class T> void Heap<T>::_heapifyDown(int index) { if ( !_isLeaf(index) ) { int minChildIndex = _minChild(index); if ( item_[index] _______ item_[minChildIndex] ) { std::swap( item_[index], item_[minChildIndex] ); _heapifyDown( ________________ ); } } }1 2 3 4 5 6 7 8 9 10 11 1251594672045615972012345670
buildHeap (minHeap Constructor)ULDPBIHEWAONBUILDHEAPNOWHow can I build a minHeap?
buildHeap - heapifyUpADBCECAEDBDo we heapifyUp from top or bottom?ADBCECAEDB
buildHeap - heapifyUpBDACECBEDAStarting at index _________Ending at index __________RepeatedlyheapifyUp(i):
buildHeap - heapifyDownBDACECBEDAFGFGDo we hDown from top or bottom?
buildHeap - heapifyDownBDACECBEDAFGFGStarting at index _________Ending at index __________RepeatedlyheapifyDown(i):
buildHeaptemplate <class T> void Heap<T>::buildHeap() { for (unsigned i = size/2; i > 0; i--) { heapifyDown(i); } }1 2 3 4 5 61. Sort the array — its a heap!2. heapifyUp()3. heapifyDown()1 2 3 4 5 6template <class T> void Heap<T>::buildHeap() { for (unsigned i = 2; i < size_; i++) { heapifyUp(i); } }
ULDPBIHEWAONO(h’) = 1buildHeap - heapifyDownLets break down the total ‘amount’ of work:BUILDHEAPNOW
BUIDHEPNOWALUADPBIHEWLONO(h’) = 2buildHeap - heapifyDownLets break down the total ‘amount’ of work:
ALDPBEHIWUONbuildHeap - heapifyDownLets break down the total ‘amount’ of work:O(h’) = 3BDHPNOWAELIU
Proving buildHeap Running TimeTheorem: The running time of buildHeap on array of size n is:Strategy:
Proving buildHeap Running TimeTheorem: The running time of buildHeap on array of size n is:Strategy:1) Call heapifyDown on every non-leaf node2) Worst case work for any node is the height of node3) To prove time, simply add up worst case swaps of every node
Proving buildHeap Running TimeS(h): Sum of the heights of all nodes in a perfecttree of height h.S(0) =S(1) =S(2) =S(h) =
Proving buildHeap Running TimeBase Case:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+1−2−h
Proving buildHeap Running TimeBase Case:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+1−2−hh = 0 20+1−2−0 = 0h = 1 10021+1−2−1 = 10vs
Proving buildHeap Running TimeInduction Step:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+1−2−h
Proving buildHeap Running TimeInduction Step: is true for all values S(i) =i+ 2S(i−1)i<hClaim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+1−2−hS(h−1) = 2h−1+1−2−(h−1) = 2h−h−1S(h) =h+ 2S(h−1) =h+(2 (2h−h−1))(By IH)(Plug in)S(h) = 2h+1−2−h(Simplify)
Proving buildHeap Running TimeTheorem: The running time of buildHeap on array of size n is O(n)How can we relate hand n?S(h) = 2h+1−2−hHow can we estimate running time?
Proving buildHeap Running TimeTheorem: The running time of buildHeap on array of size n is O(n)How can we relate hand n?S(h) = 2h+1−2−hHow can we estimate running time?h≤log n2log n+1−2−log n2 * 2log2n−2−log n2n−log n−2≈O(n)(Plug in)(Simplify)(Rearrange)
minHeap515925467201116121445615972016251412111. Construction 2. Insert 3. RemoveMinminHeap is a good example of tradeoffs: