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
Background image
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/
Background image
Learning ObjectivesReview the heap data structureDiscuss heap ADT implementationsProve the runtime of the heap
Background image
(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
Background image
(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
Background image
Implementation of heap array45615972016251412111234567891011120131514T* StartT* SizeT* Capacitysize_t Startsize_t Sizesize_t CapacityArray List (Pointer implementation)Array List (Index implementation)16
Background image
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
Background image
removeMin51592546720111612144561597201625141211What is the Big O of array remove?What else can we do?
Background image
removeMin51592546720111612144561597201625141211
Background image
removeMin9151125567201612145961511720162514121) Swap root with last item2) HeapifyDown( ) root(and remove) (and modify size)
Background image
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
Background image
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
Background image
buildHeap (minHeap Constructor)ULDPBIHEWAONBUILDHEAPNOWHow can I build a minHeap?
Background image
buildHeap – sorted arrayBEHOADILWNUPBUILDHEAPNOWABDEHILNOPUW
Background image
buildHeap - heapifyUpADBCECAEDBDo we heapifyUp from top or bottom?ADBCECAEDB
Background image
buildHeap - heapifyUpBDACECBEDAStarting at index _________Ending at index __________RepeatedlyheapifyUp(i):
Background image
buildHeap - heapifyDownBDACECBEDAFGFGDo we hDown from top or bottom?
Background image
buildHeap - heapifyDownBDACECBEDAFGFGStarting at index _________Ending at index __________RepeatedlyheapifyDown(i):
Background image
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); } }
Background image
ULDPBIHEWAONO(h’) = 1buildHeap - heapifyDownLets break down the total ‘amount’ of work:BUILDHEAPNOW
Background image
BUIDHEPNOWALUADPBIHEWLONO(h’) = 2buildHeap - heapifyDownLets break down the total ‘amount’ of work:
Background image
ALDPBEHIWUONbuildHeap - heapifyDownLets break down the total ‘amount’ of work:O(h’) = 3BDHPNOWAELIU
Background image
Proving buildHeap Running TimeTheorem: The running time of buildHeap on array of size n is:Strategy:
Background image
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
Background image
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) =
Background image
Proving buildHeap Running TimeBase Case:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+12h
Background image
Proving buildHeap Running TimeBase Case:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+12hh = 0 20+120 = 0h = 1 10021+121 = 10vs
Background image
Proving buildHeap Running TimeInduction Step:Claim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+12h
Background image
Proving buildHeap Running TimeInduction Step: is true for all values S(i) =i+ 2S(i1)i<hClaim: Sum of heights of all nodes in a perfect tree:S(h) = 2h+12hS(h1) = 2h1+12(h1) = 2hh1S(h) =h+ 2S(h1) =h+(2 (2hh1))(By IH)(Plug in)S(h) = 2h+12h(Simplify)
Background image
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+12hHow can we estimate running time?
Background image
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+12hHow can we estimate running time?hlog n2log n+12log n2 * 2log2n2log n2nlog n2O(n)(Plug in)(Simplify)(Rearrange)
Background image
minHeap515925467201116121445615972016251412111. Construction 2. Insert 3. RemoveMinminHeap is a good example of tradeoffs:
Background image
Heap Sort515925467201116121445615972016251412111. 2. 3.Running time?
Background image