a heap is efficiently represented as an array
DESCRIPTION
9. 8. 7. 6. 7. 2. 6. 5. 1. A Heap Is Efficiently Represented As An Array. 9. 8. 7. 6. 7. 2. 6. 5. 1. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 1. 9. 2. 3. 8. 7. 4. 7. 5. 6. 6. 7. 2. 6. 5. 1. 8. 9. Moving Up And Down A Heap. 9. 8. 7. 6. 7. 2. 6. 5. 1. - PowerPoint PPT PresentationTRANSCRIPT
9 8 7 6 7 2 6 5 1
1 2 3 4 5 6 7 8 9 100
A Heap Is Efficiently Represented As An Array
9
8
6 7 2 6
5 1
7
1
Moving Up And Down A Heap9
8
6 7 2 6
5 1
7
1
2 3
4 5 6 7
8 9
2
Inserting An Element Into A Max Heap
Complete binary tree with 10 nodes.
9
8
6 7 2 6
5 1
7
7
3
Inserting An Element Into A Max Heap
New element is 5.
9
8
6 7 2 6
5 1
7
75
4
Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
5
Inserting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
77
6
Inserting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
7
Inserting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
20
8
Inserting An Element Into A Max Heap
Complete binary tree with 11 nodes.
9
86
7
2 6
5 1
7
77
20
9
Inserting An Element Into A Max Heap
New element is 15.
9
86
7
2 6
5 1
7
77
20
10
Inserting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
77
20
8
11
Inserting An Element Into A Max Heap
New element is 15.
8
6
7
2 6
5 1
7
77
20
8
9
15
12
Complexity of Inserting
Complexity is O(log n), where n is heap size.
8
6
7
2 6
5 1
7
77
20
8
9
15
13
Deleting the Max Element
Max element is in the root.
8
6
7
2 6
5 1
7
77
20
8
9
15
14
Deleting the Max Element
After max element is removed.
8
6
7
2 6
5 1
7
77 8
9
15
15
Deleting the Max Element
Heap with 10 nodes.
8
6
7
2 6
5 1
7
77 8
9
15
Reinsert 8 into the heap. 16
Deleting the Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
17
Deleting the Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
18
Deleting the Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
8
19
Deleting the Max Element
Max element is 15.
6
7
2 6
5 1
7
77
9
15
8
20
Deleting the Max Element
After max element is removed.
6
7
2 6
5 1
7
77
9
8
21
Deleting the Max Element
Heap with 9 nodes.
6
7
2 6
5 1
7
77
9
8
22
Deleting the Max Element
Reinsert 7.
6 2 6
5 1
79
8
23
Deleting the Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
24
Deleting the Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
7
25
Complexity of Remove Max Element
Complexity is O(log n).
6 2 6
5 1
7
9
8
7
26
Initializing a Max Heap
input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
8
4
7
6 7
8 9
3
710
1
11
5
2
27
Initializing a Max Heap
Start at rightmost array position that has a child.
8
4
7
6 7
8 9
3
710
1
11
5
2
Index is n/2.28
Initializing a Max Heap
Move to next lower array position.
8
4
7
6 7
8 9
3
710
1
5
11
2
29
Initializing a Max Heap
8
4
7
6 7
8 9
3
710
1
5
11
2
30
Initializing a Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
31
Initializing a Max Heap
8
9
7
6 7
8 4
3
710
1
5
11
2
32
Initializing a Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
33
Initializing a Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
2
34
Initializing a Max Heap
8
9
7
6 3
8 4
7
710
1
5
11
Find a home for 2.35
Initializing a Max Heap
8
9
7
6 3
8 4
7
75
1
11
Find a home for 2.
10
36
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
1
11
Done, move to next lower array position.
10
5
37
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
1
11
10
5
Find home for 1.38
11
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
10
5
Find home for 1.39
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.40
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Find home for 1.41
Initializing a Max Heap
8
9
7
6 3
8 4
7
72
11
10
5
Done.
1
42
Time Complexity
87
6 3
4
7
710
11
5
2
9
8
1
Height of heap = h.
Number of subtrees with root at level j is <= 2 j-1.
Time for each subtree is O(h-j+1). 43
Complexity
Time for level j subtrees is <= 2j-1(h-j+1) = t(j).
Total time is t(1) + t(2) + … + t(h-1) = O(n).
44
Leftist Trees
Linked binary tree.
Can do everything a heap can do and in the same asymptotic complexity.
Can meld two leftist tree priority queues in O(log n) time.
45
Extended Binary Trees
Start with any binary tree and add an external node wherever there is an empty subtree.
Result is an extended binary tree.
46
A Binary Tree
47
An Extended Binary Tree
number of external nodes is n+1 48
The Function s()
For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x.
49
s() Values Example
50
s() Values Example
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
51
Properties of s()
If x is an external node, then s(x) = 0.
Otherwise,s(x) = min {s(leftChild(x)),
s(rightChild(x))} + 1
52
Height Biased Leftist Trees
A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftChild(x)) >= s(rightChild(x))
53
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
54
Leftist Trees--Property 1
In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root).
55
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Length of rightmost path is 2. 56
Leftist Trees—Property 2
The number of internal nodes is at least2s(root) - 1
Because levels 1 through s(root) have no external nodes.
So, s(root) <= log(n+1)
57
A Leftist Tree
0 0 0 0
0 0
0 0
0
0
1 1 1
2 1 1
2 1
2
Levels 1 and 2 have no external nodes.58
Leftist Trees—Property 3
Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree.
Follows from Properties 1 and 2.
59
Leftist Trees as Priority Queues
Min leftist tree … leftist tree that is a min tree.
Used as a min priority queue.
Max leftist tree … leftist tree that is a max tree.
Used as a max priority queue.
60
A Min Leftist Tree
8 6 9
6 8 5
4 3
2
61
Some Min Leftist Tree Operations
insert()
delete()
meld()
initialize()
insert() and delete() use meld().
62
Insert Operation
insert(7)
8 6 9
6 8 5
4 3
2
63
Insert Operation
insert(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree. 7
64
Insert Operation
insert(7)
8 6 9
6 8 5
4 3
2
Create a single node min leftist tree.
Meld the two min leftist trees.
7
65
Delete Min
8 6 9
6 8 5
4 3
2
66
Delete Min
8 6 9
6 8 5
4 3
2
Delete the root.
67
Delete Min
8 6 9
6 8 5
4 3
2
Delete the root.
Meld the two subtrees.68
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Traverse only the rightmost paths so as to get logarithmic performance.
69
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of other tree.
70
Meld Two Min Leftist Trees
8 6 9
6 8 5
4 3
6
Meld right subtree of tree with smaller root and all of other tree.
71
Meld Two Min Leftist Trees
8 6
6 8
4 6
Meld right subtree of tree with smaller root and all of other tree.
72
Meld Two Min Leftist Trees
8 6
Meld right subtree of tree with smaller root and all of other tree.
Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree. 73
Meld Two Min Leftist Trees
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6
8
6
8 74
Meld Two Min Leftist Trees
8 6
6 6
4
88 6
6
46
8
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) < s(right).75
Meld Two Min Leftist Trees
9
5
3
Swap left and right subtree if s(left) < s(right).
Make melded subtree right subtree of smaller root.
8 6
6 6
4
8
76
Meld Two Min Leftist Trees
9
5
3
8 6
6 6
4
8
77
Initializing in O(n) Time• Create n single node min leftist trees
and place them in a FIFO queue
• Repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue
• The process terminates when only 1 min leftist tree remains in the queue
• Analysis is the same as for heap initialization
78