Download presentation
1
Chapter 21 Binary Heap
2
Objective To learn Basic properties of the binary heap
Insert and deletemin operations Linear time heap-construction algorithm Heapsort
3
Topics Review and analyze operations on Heap data structure
Review definitions Tree, binary tree depth height full binary tree complete binary tree Heap property
4
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
5
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.
6
Depth and height of a binary tree
2 1 1 1 2 The depth of the tree = height of the tree = 2
7
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.
8
Full binary tree Complete binary tree 2h n 2h+1 -1 n = 2h+1 -1
9
The height of a complete binary tree
The number of nodes n of a complete binary tree satisfies: h 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
10
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
11
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
12
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 .
13
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
14
Heap viewed as Binary tree implemented as an array.
3 2 8 7 18 14 9 29 6 1 4 5 10 last 1 3 2 8 7 29 6 4 5 9 18 14 10
15
insert(v ) Item inserted as last item in the heap
2 Item inserted as last item in the heap Heap property may be violated Do percolate to restore heap property 5 6 z 9 7 insertion node 2 5 6 z 9 7 1
16
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
17
deleteMin() Save root object O(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
18
4 3 8 17 12 14 19 6 13 1 siftDown(1) New value at root.
5 7 9 10 siftDown(1) New value at root. Right Child is smaller Exchange root and right child Satisfy the Heap property.
19
4 9 8 17 12 14 19 6 13 1 3 2 Parent Left Child is smaller
5 7 10 Parent Left Child is smaller Exchange parent and left child
20
The worst case run time to do siftDown(index) is
4 6 8 17 12 14 19 9 13 1 2 3 5 7 10 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)
21
Build heap---linear time
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
22
siftDown makes it a min heap
5 8 12 9 7 10 21 1 2 3 4 6 14 8 12 9 7 6 14 4 10 21 1 2 3 5 siftDown makes it a min heap
23
siftDown makes this into heap
8 12 9 4 6 14 7 10 21 1 2 3 5 this is a heap siftDown makes this into heap
24
8 12 6 4 9 14 7 10 21 1 2 3 5 i = 3 siftDown makes heap These are heaps
25
1 10 12 21 3 6 7 5 8 4 9 14 2 i = 2 4 6 7 9 14 8 2 5 10 4 6 8 9 14 7 2 5 10 siftDown
26
4 10 6 7 9 14 8 12 21 1 2 3 5 i = 1 10 6 7 9 14 8 12 21 1 2 3 4 5 5
27
Running time n/2 nodes For each node, at most O(log(n))
The running time is O(nlgn)
28
Heap-Sort 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
29
97 53 59 58 31 26 41 16 97 53 59 26 41 58 1 2 3 4 5 31 16 21 36 36 21 6 7 8 9 10 59 53 58 36 31 26 41 16 21 97 59 53 58 26 41 36 1 2 3 4 5 31 16 21 97 6 7 8 9 10
30
HeapSort //build heap for i ¬ (last /2) downto 1
do siftDown( a, i, last) //shiftDown from i to last for j ¬ last-1 downto 1 swap(a[0], a[j]) //deleteMax do siftDown(a, 0, j) // shiftDown from 0 to j
31
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.
32
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.