320 likes | 513 Views
Chapter 21. Binary Heap. Objective. To learn Basic properties of the binary heap Insert and deletemin operations Linear time heap-construction algorithm Heapsort. Topics. Review and analyze operations on Heap data structure Review definitions Tree, binary tree depth height
E N D
Chapter 21 Binary Heap
Objective • To learn • Basic properties of the binary heap • Insert and deletemin operations • Linear time heap-construction algorithm • Heapsort
Topics • Review and analyze operations on Heap data structure • Review definitions • Tree, binary tree • depth • height • full binary tree • complete binary tree • Heap property
Binary trees • A tree is an acyclic, connected, undirected graph. • Only one path exists between a pair of nodes • A leaf of a tree is a node with no children. • Binary tree - a tree where each node has 0,1 or 2 children
Depth and height of a tree • Depth of a node is: • Depth of the root of a tree is 0. • The depth of its parent +1 • Depth of a tree is maximum depth of its leaves. • Height of a node is: • Height of a leaf of a tree is 0. • The maximum height of its children +1 • Height of a tree is the height of the root.
Depth and height of a binary tree Depth Height 2 0 1 1 0 1 2 0 0 The depth of the tree = height of the tree = 2
A complete binary tree • A full binary tree (also called complete) is a binary tree such that • All internal nodes have 2 children • All leaves have depth d • A complete binary tree (also called essentially complete) is a binary tree such that • All internal nodes have 2 children except the last internal node which may have only 1 child. • All leaves have depth d or d -1 • Nodes with depth d are as far to the left as possible.
Full binary tree Complete binary tree 2h n 2h+1 -1 n = 2h+1 -1
The height of a complete binary tree • The number of nodes n of a complete binary tree satisfies: 2h n (2h+1-1) • Taking the log base 2 we get: h lg n and lg(n+1) h+1 or lg(n+1)-1 h lg n • Since h is integer h = lg(n + 1)-1= lg n
The height of a complete binary tree 2h n (2h+1-1) Complete binary tree with 1 node at depth h Full binary tree all leaves same depth
Heap Definition • A heap is a complete binary tree that satisfies the heap property. • Minimum Heap Property: The value stored at each node is less than or equal to the values stored at its children. • OR Maximum Heap Property: greater
Implementation of Heap • For simplicity we assume the complete binary tree is an array, and the root is stored at index 1. • For any element in array position i, its left child is at position 2i, and it’s right child is at 2i+1, its parent is at i/2 .
Last node in a heap • The last node of a heap is the rightmost internal node of on the last level 2 5 6 9 7 last node
1 1 3 2 3 2 4 7 5 6 8 6 7 29 8 9 10 18 14 9 1 3 2 8 7 29 6 18 14 9 1 2 3 4 5 6 7 8 9 10 Heap viewed as Binary tree implemented as an array. last
Item inserted as last item in the heap Heap property may be violated Do percolate to restore heap property insert(v ) 2 5 6 z 9 7 insertion node 2 5 6 z 9 7 1
Percolate up • After the insertion of a new key k, the heap-order property may be violated • Algorithm percolate up restores the heap-order property by swapping k along an upward path from the insertion node • percolate up terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k • Since a heap has height O(log n), upheap runs in O(log n) time 2 1 5 1 5 2 z z 9 7 6 9 7 6
deleteMin() 1 10 10 2 3 30 20 4 • Save root object O(1) • Remove last element and store in root O(1) • siftDown(1) 80 1 last 80 2 3 30 20 1 20 2 3 30 80
1 9 3 2 4 3 4 7 5 6 8 13 17 6 8 9 10 12 14 19 siftDown(1) New value at root. Right Child is smallerExchange root and right child Satisfy the Heap property.
1 3 3 2 4 9 4 7 5 6 8 13 17 6 8 9 10 12 14 19 ParentLeft Child is smaller Exchange parent and left child
1 3 3 2 4 6 4 7 5 6 8 13 17 9 8 9 10 12 14 19 The worst case run time to do siftDown(index) is O(h(index)) where h(index) is the height of node index When index=root=1, O(lg n)
Build heap---linear time //Build-heap 1. for i ¬ (last /2)downto 1 2. do siftDown( i ) Why we can start siftDown at last/2 ? because we : • need to siftDown only parents • the rest of the nodes are leaves and • leaves satisfy the heap property
5 8 12 9 7 10 21 6 14 4 1 2 3 4 5 6 7 8 9 10 1 5 3 2 8 12 4 7 5 6 9 21 7 10 8 9 10 6 14 4 siftDown makes it a min heap
1 5 3 2 8 12 4 7 5 6 9 21 4 10 8 9 10 6 14 7 i = 4 this is a heap siftDown makes this into heap
1 5 3 2 8 12 4 7 5 6 6 21 4 10 8 9 10 9 14 7 i = 3 siftDown makes heap These are heaps
5 3 2 10 8 7 4 6 5 21 6 12 4 8 9 10 9 14 7 2 4 2 4 5 4 4 6 5 7 6 8 8 9 10 8 9 9 14 8 10 9 14 7 1 i = 2 siftDown
1 5 3 2 4 10 4 7 5 6 6 21 7 12 1 8 9 10 4 9 14 8 3 2 10 4 7 5 6 6 21 7 12 8 9 10 9 14 8 i = 1 5
Running time • n/2 nodes • For each node, at most O(log(n)) • The running time is O(nlgn)
Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort Heap-Sort
59 97 53 53 58 59 26 26 41 41 58 36 0 0 1 1 2 2 3 3 4 4 5 5 97 53 59 58 31 26 41 16 31 16 21 36 36 21 6 7 8 9 10 59 53 58 36 31 26 41 16 21 97 31 16 21 97 6 7 8 9 10
HeapSort //build heap for i ¬ (last /2)downto 1 do siftDown( a, i, last) //shiftDown from i to last for j¬ last-1downto 1 swap(a[0], a[j]) //deleteMax do siftDown(a, 0, j) // shiftDown from 0 to j
Common errors (from book, page 786) • For heapsort data begins in position 0, so the children of the I are in positions 2i+1 and 2i+2. • STL priority queue is a max heap, not a min heap. • More errors are defined in the book.
In class exercises • 21.1 • Describe the structure and ordering properties of the binary heap • 21.2 • In a binary heap, for an item in position I where are the parent, left child, and right child located? • 21.3 • Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time, in an initially empty heap. Then show the result of using the linear-time buildHep algorithm instead