Download presentation
Presentation is loading. Please wait.
2
heaps1 Trees IV: The Heap
3
heaps2 Heap Like a binary search tree, a heap is a binary tree in which the data entries can be compared using total order semantics Defining qualities of a heap: –the data entry contained in any node is greater than or equal to the data entry contained in either of its children –the tree is always complete
4
heaps3 Heap Example 45 3241 1972840 1012
5
heaps4 Heap Applications Heap can be used to implement a sorting algorithm called heapsort Heap is also a handy way to implement a priority queue -- we’ll use this application to illustrate how heaps work
6
heaps5 Heap Implementation Because a heap is by definition a complete tree, it is easy to use an array-based implementation reHeapUp reHeapDownHeap operations reHeapUp and reHeapDown are used to maintain the other defining quality of a heap -- each node’s data entry >= data entries of its children
7
heaps6 ReHeapUp operation 45 3241 1972840 1012 When a new node is added to the tree, it is always added at the leftmost open position in the bottom row 35 This maintains completeness of tree, but can violate the other defining characteristic of a heap
8
heaps7 ReHeapUp operation 45 3241 1972840 101235 ReHeapUp restores the second heap condition: a parent node’s data is always greater than or equal to the data in either of its child nodes The operation is accomplished by swapping the new node’s data entry with that of its parent 35 7 Parent/child data swapping continues until the heap condition is restored 32 35
9
heaps8 ReHeapDown operation 45 3241 1972840 101235 7 32 35 In a priority queue, the highest-priority item is always dequeued first -- this item would be the top item of the heap Since this is going to be an array implementation, we’ll perform the usual trick of swapping the last array entry with the first, so we can minimize the amount of copying to be done 7 7 45
10
heaps9 ReHeapDown operation 45 3241 1972840 1012 3532 35 7 Now we can effectively remove the item by simply diminishing the “used” portion of our array by 1 (already done here) We are once again faced with the same problem -- the heap is the right shape, but the data values are in the wrong positions We solve this problem, as before, by swapping data between nodes. In this case, we swap the parent node’s data with the largest data entry of the two children, continuing until the heap is restored. 7 41 40 7
11
heaps10 Example: Priority Queue with heap implementation Priority queue has all operations associated with a queue: enqueue, dequeue and helper functions (e.g. size( ), is_empty( ), etc.) Heap is a good fit for priority queue because highest-priority item should always be dequeued first, and this item would always be found at the top of a heap
12
heaps11 Code for Priority Queue template class PQheap { public: PQheap( ); enum {SIZE=30}; void enqueue (const thing& entry); thing dequeue( ); size_t size( )const {return numItems;}...
13
heaps12 Code for Priority Queue private: thing data[SIZE]; size_t numItems; void reHeapUp(size_t n); void reHeapDown(size_t n); size_t parent(size_t n) const {return (n-1)/2;} size_t left_child(size_t n) const {return (2*n)+1;} size_t right_child(size_t n) const {return (2*n)+2;} void Swap (thing& x, thing& y); };
14
heaps13 Code for Priority Queue template PQheap ::PQheap() { numItems=0; }
15
heaps14 Code for Priority Queue template void PQheap ::enqueue (const thing& entry) { assert (size( ) < SIZE); data[numItems] = entry; reHeapUp(numItems); numItems++; }
16
heaps15 Code for Priority Queue template void PQheap ::reHeapUp(size_t n) { size_t x = n; while (x>0 && data[x]> data[parent(x)]) { Swap(data[x], data[parent(x)]); x=parent(x); } }
17
heaps16 Code for Priority Queue template void PQheap ::Swap (thing& x, thing& y) { thing temp = x; x = y; y = temp; }
18
heaps17 Code for Priority Queue template thing PQheap ::dequeue() { thing value=data[0]; // save return value numItems--; data[0]=data[numItems]; // swap top & bottom reHeapDown(numItems); // restore heap return value; }
19
heaps18 Code for Priority Queue template void PQheap ::reHeapDown(size_t n) { size_t current = 0, big_child, heap_ok = 0; while ((!heap_ok) && (left_child(current) < n)) { if (right_child(current) >= n) big_child = left_child(current); else if (data[left_child(current)] > data[right_child(current)]) big_child = left_child(current); else big_child = right_child(current);...
20
heaps19 Code for Priority Queue if (data[current] < data[big_child]) { Swap(data[current], data[big_child]); current = big_child; } else heap_ok = 1; } // end of while loop } // end of function
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.