Title: A Heap Is Efficiently Represented As An Array
1A Heap Is Efficiently Represented As An Array
9
8
7
6
7
2
6
5
1
1
2
3
4
5
6
7
8
9
10
0
2Moving Up And Down A Heap
3Inserting An Element Into A Max Heap
- Complete binary tree with 10 nodes.
4Inserting An Element Into A Max Heap
9
8
7
6
7
2
6
5
1
7
5
5Inserting An Element Into A Max Heap
9
8
7
6
2
6
7
7
5
1
7
6Inserting An Element Into A Max Heap
9
8
7
6
2
6
7
5
1
7
7
7Inserting An Element Into A Max Heap
9
7
8
6
2
6
7
5
1
7
7
8Inserting An Element Into A Max Heap
20
9
7
8
6
2
6
7
5
1
7
7
9Inserting An Element Into A Max Heap
20
9
7
8
6
2
6
7
5
1
7
7
- Complete binary tree with 11 nodes.
10Inserting An Element Into A Max Heap
20
9
7
8
6
2
6
7
5
1
7
7
11Inserting An Element Into A Max Heap
20
9
7
6
2
6
8
8
7
5
1
7
7
12Inserting An Element Into A Max Heap
20
7
15
6
2
6
9
8
8
7
5
1
7
7
13Complexity of Inserting
- Complexity is O(log n), where n is heap size.
14Deleting the Max Element
- Max element is in the root.
15Deleting the Max Element
7
15
6
2
6
9
8
8
7
5
1
7
7
- After max element is removed.
16Deleting the Max Element
7
15
6
2
6
9
8
8
7
5
1
7
7
Reinsert 8 into the heap.
17Deleting the Max Element
7
15
6
2
6
9
7
5
1
7
7
- Reinsert 8 into the heap.
18Deleting the Max Element
15
7
6
2
6
9
7
5
1
7
7
- Reinsert 8 into the heap.
19Deleting the Max Element
15
7
9
6
2
6
8
7
5
1
7
7
- Reinsert 8 into the heap.
20Deleting the Max Element
15
7
9
6
2
6
8
7
5
1
7
7
21Deleting the Max Element
7
9
6
2
6
8
7
5
1
7
7
- After max element is removed.
22Deleting the Max Element
7
9
6
2
6
8
7
5
1
7
7
23Deleting the Max Element
7
9
6
2
6
8
5
1
24Deleting the Max Element
9
7
6
2
6
8
5
1
25Deleting the Max Element
9
8
7
6
2
6
7
5
1
26Complexity of Remove Max Element
27Initializing a Max Heap
- input array -, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11
28Initializing a Max Heap
1
3
2
4
6
7
5
11
8
7
8
9
7
10
- Start at rightmost array position that has a
child.
Index is n/2.
29Initializing a Max Heap
1
3
2
4
6
7
11
5
8
7
8
9
7
10
- Move to next lower array position.
30Initializing a Max Heap
1
3
2
4
6
7
11
5
8
7
8
9
7
10
31Initializing a Max Heap
1
3
2
9
6
7
11
5
8
7
8
4
7
10
32Initializing a Max Heap
1
3
2
9
6
7
11
5
8
7
8
4
7
10
33Initializing a Max Heap
1
7
2
9
6
3
11
5
8
7
8
4
7
10
34Initializing a Max Heap
1
7
2
9
6
3
11
5
8
7
8
4
7
10
35Initializing a Max Heap
1
7
11
9
6
3
5
8
7
8
4
7
10
Find a home for 2.
36Initializing a Max Heap
1
7
11
9
6
3
10
8
7
8
4
7
5
Find a home for 2.
37Initializing a Max Heap
1
7
11
9
6
3
10
8
7
8
4
7
5
2
Done, move to next lower array position.
38Initializing a Max Heap
1
7
11
9
6
3
10
2
8
7
8
4
7
5
Find home for 1.
39Initializing a Max Heap
11
7
9
6
3
10
2
8
7
8
4
7
5
Find home for 1.
40Initializing a Max Heap
11
7
10
9
6
3
2
8
7
8
4
7
5
Find home for 1.
41Initializing a Max Heap
11
7
10
9
6
3
5
2
8
7
8
4
7
Find home for 1.
42Initializing a Max Heap
11
7
10
9
6
3
5
2
8
7
8
4
7
1
Done.
43Time Complexity
11
7
9
6
3
5
8
8
7
4
7
2
1
10
Height of heap h. Number of subtrees with root
at level j is lt 2 j-1. Time for each subtree is
O(h-j1).
44Complexity
Time for level j subtrees is lt 2j-1(h-j1)
t(j). Total time is t(1) t(2) t(h-1)
O(n).
45Leftist 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.
46Extended Binary Trees
- Start with any binary tree and add an external
node wherever there is an empty subtree. - Result is an extended binary tree.
47A Binary Tree
48An Extended Binary Tree
number of external nodes is n1
49The 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.
50s() Values Example
51s() Values Example
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
52Properties of s()
- If x is an external node, then s(x) 0.
- Otherwise,
- s(x) min s(leftChild(x)),
- s(rightChild(x)) 1
53Height Biased Leftist Trees
- A binary tree is a (height biased) leftist tree
iff for every internal node x, s(leftChild(x)) gt
s(rightChild(x))
54A Leftist Tree
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
55Leftist 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).
56A Leftist Tree
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
Length of rightmost path is 2.
57Leftist TreesProperty 2
- The number of internal nodes is at least
- 2s(root) - 1
- Because levels 1 through s(root) have no external
nodes. - So, s(root) lt log(n1)
58A Leftist Tree
2
2
1
2
1
1
0
1
0
0
1
1
0
0
0
0
0
0
0
Levels 1 and 2 have no external nodes.
59Leftist TreesProperty 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.
60Leftist 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.
61A Min Leftist Tree
2
4
3
6
8
5
9
8
6
62Some Min Leftist Tree Operations
insert() delete() meld() initialize() insert()
and delete() use meld().
63Insert Operation
2
4
3
6
8
5
9
8
6
64Insert Operation
2
4
3
6
8
5
9
8
6
Create a single node min leftist tree.
7
65Insert Operation
2
4
3
6
8
5
9
8
6
Create a single node min leftist tree. Meld the
two min leftist trees.
7
66Delete Min
2
4
3
6
8
5
9
8
6
67Delete Min
2
4
3
6
8
5
9
8
6
Delete the root.
68Delete Min
2
4
3
6
8
5
9
8
6
Delete the root. Meld the two subtrees.
69Meld Two Min Leftist Trees
Traverse only the rightmost paths so as to get
logarithmic performance.
70Meld Two Min Leftist Trees
4
3
6
8
5
6
9
8
6
Meld right subtree of tree with smaller root and
all of other tree.
71Meld Two Min Leftist Trees
4
3
6
8
5
6
9
8
6
Meld right subtree of tree with smaller root and
all of other tree.
72Meld Two Min Leftist Trees
6
4
6
8
8
6
Meld right subtree of tree with smaller root and
all of other tree.
73Meld Two Min Leftist Trees
6
8
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.
74Meld Two Min Leftist Trees
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt s(right).
75Meld Two Min Leftist Trees
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt s(right).
76Meld Two Min Leftist Trees
Make melded subtree right subtree of smaller root.
Swap left and right subtree if s(left) lt s(right).
77Meld Two Min Leftist Trees
3
4
5
6
6
9
8
6
8
78Initializing 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