CS 225 Data Structures Oc October 31 – He Heaps and Priority Qu Queues G G Carl Evans
Ru Running T Times Hash Table AVL Linked List SUHA: Find Worst Case: SUHA: Insert Worst Case: Storage Space
Se Secr cret, M Mystery D Data St Stru ruct cture ADT: insert remove isEmpty
Pr Priority Queue Implementat ation insert removeMin unsorted unsorted sorted sorted
Another p An r possibly s stru ructure… 4 5 6 15 9 7 20 16 25 14 12 11
(m (min)He n)Heap 4 A complete binary tree T 5 6 is a min-heap if: 15 9 7 20 • T = {} or • T = {r, T L , T R } , where r is 16 25 14 12 11 less than the roots of { T L , T R } and { T L , T R } are min-heaps.
(m (min)He n)Heap 4 5 6 15 9 7 20 16 25 14 12 11 4 5 6 15 9 7 20 16 25 14 12 11
in insert 4 5 6 15 9 7 20 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11
insert in 1 template <class T> 2 void Heap<T>::_insert(const T & key) { 3 // Check to ensure there’s space to insert an element 4 // ...if not, grow the array 5 if ( size_ == capacity_ ) { _growArray(); } 6 4 7 // Insert the new element at the end of the array 8 item_[++size] = key; 9 5 6 10 // Restore the heap property 11 _heapifyUp(size); 12 } 15 9 7 20 16 25 14 12 11 4 5 6 15 9 7 20 16 25 14 12 11
gr growA wArray 4 5 6 15 9 7 20 16 25 11 14 12
insert in t - he heapi pifyUp yUp 1 template <class T> 2 void Heap<T>::_insert(const T & key) { 3 // Check to ensure there’s space to insert an element 4 // ...if not, grow the array 5 if ( size_ == capacity_ ) { _growArray(); } 6 7 // Insert the new element at the end of the array 8 item_[++size] = key; 9 10 // Restore the heap property 11 _heapifyUp(size); 12 } 1 template <class T> 2 void Heap<T>::_heapifyUp( _________________ ) { 3 if ( index > _________ ) { 4 if ( item_[index] < item_[ parent(index) ] ) { 5 std::swap( item_[index], item_[ parent(index) ] ); 6 _heapifyUp( ________________ ); 7 } 8 } 9 }
heapi he pifyUp yUp 1 template <class T> 2 void Heap<T>::_heapifyUp( _________________ ) { 3 if ( index > _________ ) { 4 if ( item_[index] < item_[ parent(index) ] ) { 5 std::swap( item_[index], item_[ parent(index) ] ); 4 6 _heapifyUp( ________________ ); 7 } 8 } 9 } 5 6 15 9 7 20 16 25 14 12 11 7 4 5 6 15 9 7 20 16 25 14 12 11
re removeMin 4 5 6 15 9 7 20 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11
removeMin re 1 template <class T> 2 void Heap<T>::_removeMin() { 3 // Swap with the last value 4 T minValue = item_[1]; 5 item_[1] = item_[size_]; 6 size--; 4 7 8 // Restore the heap property 9 heapifyDown(); 5 6 10 11 // Return the minimum value 12 return minValue; 15 9 7 20 13 } 16 25 14 12 11 4 5 6 15 9 7 20 16 25 14 12 11
removeMin - he re heapi pifyD yDown wn 1 template <class T> 2 void Heap<T>::_removeMin() { 3 // Swap with the last value 4 T minValue = item_[1]; 5 item_[1] = item_[size_]; 6 size--; 7 8 // Restore the heap property 9 _heapifyDown(); 10 11 // Return the minimum value 12 return minValue; 1 template <class T> 13 } 2 void Heap<T>::_heapifyDown(int index) { 3 if ( !_isLeaf(index) ) { 4 T minChildIndex = _minChild(index); 5 if ( item_[index] ___ item_[minChildIndex] ) { 6 std::swap( item_[index], item_[minChildIndex] ); 7 _heapifyDown( ________________ ); 8 } 9 } 10 }
re removeMin 1 template <class T> 2 void Heap<T>::_heapifyDown(int index) { 3 if ( !_isLeaf(index) ) { 4 T minChildIndex = _minChild(index); 5 if ( item_[index] ___ item_[minChildIndex] ) { 6 std::swap( item_[index], item_[minChildIndex] ); 11 7 _heapifyDown( ________________ ); 8 } 9 } 5 6 10 } 15 9 7 20 16 25 14 12 4 5 6 15 9 7 20 16 25 14 12 11
bui buildHe dHeap B U I L D H E A P W N O B U I L D H E A P N O W
bui buildHe dHeap – so sorted d array B U I L D H E A P N O W A B D E H I L N O W P U A B D E H I L N O P U W
bui buildHe dHeap - he heapi pifyUp yUp B U I L D H E A P W N O B U I L D H E A P N O W
bui buildHe dHeap - he heapi pifyD yDown wn B U I L D H E A P W N O B U I L D H E A P N O W
bui buildHe dHeap B 1. Sort the array – it’s a heap! U I L D H E 2. 1 template <class T> 2 void Heap<T>::buildHeap() { 3 for (unsigned i = 2; i <= size_; i++) { A P N O W 4 heapifyUp(i); 5 } 6 } 3. 1 template <class T> 2 void Heap<T>::buildHeap() { 3 for (unsigned i = parent(size); i > 0; i--) { 4 heapifyDown(i); 5 } 6 } B U I L D H E A P N O W
Pr Proving bui buildHe dHeap Ru Running T Time Theorem: The running time of buildHeap on array of size n is: _________. Strategy: - - -
Pr Proving bui buildHe dHeap Ru Running T Time S(h) : Sum of the heights of all nodes in a complete tree of height h . B S(0) = U I L D H E S(1) = A P W N O S(h) =
Pr Proving bui buildHe dHeap Ru Running T Time Proof the recurrence: Base Case: General Case:
Pr Proving bui buildHe dHeap Ru Running T Time From S(h) to RunningTime(n): S(h): Since h ≤ lg(n): RunningTime(n) ≤
Heap Hea p Sort 4 1. 5 2. 6 15 9 7 20 3. 16 25 11 14 12 4 5 6 15 9 7 20 16 25 14 12 11 Running Time? Why do we care about another sort?
A( A(no nothe her) ) thr hrowba wback k to CS 173… Let R be an equivalence relation on us where (s, t) ∈ R if s and t have the same favorite among: { ___, ___, ____, ___, ____, }
Recommend
More recommend