Upload
moazzam-hussain
View
216
Download
2
Embed Size (px)
Citation preview
Data Structures and Algorithms(CS210/ESO207/ESO211)
Lecture 25• Heap : an important tree data structure• Implementing some special binary tree using an array !• Binary heap
1
HeapDefinition: a tree data structure satisfying the following property:• value stored in a node is smaller than the value stored in its children.
2
11
4
18 23
4721
7
71 913
37
43
19
Operations on a heap
Query Operations• Find-min: report the smallest key stored in the heap.
Update Operations• CreateHeap(H): Create an empty heap H.• Insert(x,H): Insert a new key with value x into the heap H.• Extract-min(H): delete the smallest key from H.• Decrease-key(p, ∆, H): decrease the value of the key p by amount ∆. • Merge(H1,H2): Merge two heaps H1 and H2.
3
Why heaps when we can use a binary search tree ?
Compared to binary search trees, a heap is usually -- much simpler and -- more efficient
Applications: -- Priority queues -- Applications in various algorithms (Shortest paths, Minimum spanning trees)
4
Existing heap data structures
• Binary heap
• Binomial heap
• Fibonacci heap
• Soft heap
5
Implementing a special binary tree using an array
6
An important question(one must strive to answer it on his own)
What does the implementation of a tree data structure require ?
Answer: a mechanism to access parent and children of any node.
7
A complete binary tree
A complete binary of 12 nodes.
8
A complete binary tree
The label of the leftmost node at level = ?? The label of a node v occurring at kth place from left at level = ?? The label of the left child of v is= ??The label of the right child of v is= ??
9
0 1
Level nodes
1 2
2 4
0
1 2
3 4 5 6
7 8 9 10 11 -1
+k-2+2k-3
+2k-2
Now can you find a relationship between label of
a node and the labels of its children ?
A complete binary tree
Let v be a node with label .Label of left child(v) = Label of right child(v) = Label of parent(v) = 10
0 1
Level nodes
1 2
2 4
0
1 2
3 4 5 6
7 8 9 10 11
2 +1
2+2
⌊( 𝒋−1)/2 ⌋
A complete binary tree and array
Question: What is the relation between a complete binary trees and an array ?Answer: A complete binary tree can be implemented by an array.
Exercise: State the advantages of this array based implementation over the link based implementation for compete binary trees?
11
41
17
57
91
9
133 70
37 25 88 35
41 17 9 57 33 1 70 91 37 25 88 35
Binary heap
12
Binary heapa complete binary tree satisfying heap property at each node.
13
4
14
17
91
9
2123 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33H
Implementation of a Binary heap
14
If n is the maximum number of keys in the binary heap at any moment of time, then we keep • H : an array of size n used for storing the binary heap.• size: a variable for the total number of keys currently in the heap.
Find_min(H)Report H[0].
15
4
14
17
91
9
2123 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33H
Extract_min(H)Think hard on designing efficient algorithm for this operation.The challenge is: how to preserve the complete binary tree structure as well as the heap property ?
16
4
14
17
91
9
2123 29
37 25 88 33
4 14 9 17 23 21 29 91 37 25 88 33H
Extract_min(H)
17
33
14
17
91
9
2123 29
37 25 88 4
33 14 9 17 23 21 29 91 37 25 88 H 4
Extract_min(H)
18
9
14
17
91
33
2123 29
37 25 88
9 14 33 17 23 21 29 91 37 25 88 H
Extract_min(H)We are done.The no. of operations performed is of the order of no. of levels in binary heap.= O(log n) …show it as an exercise.
19
9
14
17
91
21
3323 29
37 25 88
9 14 21 17 23 33 29 91 37 25 88 H
Points to ponder
• Write a neat pseudocode for Extract-min.
• With the inspiration from the algorithm for Extract-min, try to design an algorithm for Insert.
• Try to design an O(n) time algorithm to build a binary heap on n keys.
In the next class, we shall discuss the above questions and many more.
20