Professional Documents
Culture Documents
Chapter 9 - Heap Structures
Chapter 9 - Heap Structures
MIN-MAX Heaps
Definition
MIN-MAX Heaps
Complete binary tree.
Set root on a min level.
A node x in min level would have
smaller key value than all its
descendents. (x is a min node.)
MIN-MAX Heaps
7 min
70 40 max
30 9 10 15 min
45 50 30 20 12 max
MIN-MAX Heaps
70 40 max
30 9 10 15 min
45 50 30 20 12 5 max
Insertion into a min-max heap
7 min
70 40 max
30 9 5 15 min
45 50 30 20 12 10 max
Insertion into a min-max heap
5 min
70 40 max
30 9 7 15 min
45 50 30 20 12 10 max
Insertion into a min-max heap
80>10
→verify_max(heap,13,80)
Insertion into a min-max heap
7 min
Input
70 40 max
verify_max(Heap,13,80)
30 9 10 15 min grandparent=3
80>40
45 50 30 20 12 80 max
→heap[13]=heap[3]
i=3
grandparent=0
Insertion into a min-max heap
7 min
Input
70 80 max
verify_max(Heap,3,80)
30 9 10 15 min → grandparent=null
break;
45 50 30 20 12 40 max
heap[3]=80;
Insertion into a min-max heap
The time complexity of insertion into a
min-max heap with n elements is
O(log n).
A min-max heap with n elements has O(log
n) levels.
MIN-MAX Heaps
70 40 max
30 9 10 15 min
45 50 30 20 12 max
Deletion of min element
12 min
70 40 max
30 9 10 15 min
45 50 30 20 max
Deletion of min element
The reinsertion may have 2 cases:
1. No child. (Only one node in the heap)
i i
i k
k i
p p
k i
Definition
Deaps
Complete binary tree.
Either empty or satisfies the properties
1. The root contains no element.
2. The left subtree is a min-heap.
3. The right subtree is a max-heap.
4. If the right subtree is not empty. Let i be any
node in left subtree, and j be the corresponding
node in the right subtree. If no, choose the
parent one. i_key≦ j_key.
Deaps
The relation between i and j.
j i 2 log 2 i 1 ;
if ( j n)
j / 2;
1 Ex1:
i=4;
2 3 j=4+2^(2-1)=6;
Ex2:
4 5 6 7
i=9;
j=9+2^(3-1)=13;
8 9 10 11
j>12
12
j=6;
Deaps
5 45
10 8 25 40
15 19 9 30 20
Deaps
5 45
10 8 25 40
15 19 9 30 20 4
j
Insertion into a deap
5 45
10 8 25 40
15 19 9 30 20 4
j
i
Insertion into a deap
5 45
10 8 25 40
15 4 9 30 20 19
Insertion into a deap
4 45
5 8 25 40
15 10 9 30 20 19
Insertion into a deap
Step 1. max_heap.
Step 2. min_partner.
Step 4. min_insert or
max_insert.
Insertion into a deap
The time complexity is O(log n) as the
height of the deap is O(log n).
Deaps
5 45
10 8 25 40
15 19 9 30 20
temp
Deletion of min element
45
i
10 8 25 40
j
15 19 9 30 temp:20
Deletion of min element
8 45
10 25 40
i
15 19 9 30 temp:20
j
Deletion of min element
8 45
10 9 25 40
15 19 30 temp:20
i
Deletion of min element
8 45
10 9 25 40
15 19 30 temp:20
i
Deletion of min element
8 45
10 9 25 40
15 19 20 30
Deletion of min element
Definition
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.
For any node x in an extended binary tree,
let shortest(x) be the length of a shortest
path from x to an external node in the
subtree rooted at x.
shortest ( x) 10ifmin{
x is an external node
shortest( left _ child ( x )), shortest( right _ child ( y ))} otherwise
Leftist Trees
Two binary trees
A G
B C H I
D E F J
Leftist Trees
Extended binary trees
A G
B C H I
D E F J
Leftist Trees
The number inside each internal node x
is shortest(x).
2 2
2 1 1 1
1 1 1 1
Leftist Trees
Definition
A leftist tree is a binary tree such that if it
is not empty, then for every internal node
x:shortest (left _ child ( x)) shortest (right _ child ( x))
2 2
2 1 1 1
1 1 1 1
Leftist Trees
By the definition
Let x be the root of a leftist tree that has n
internal nodes.
a) n 2 shortest ( x ) 1
x A binary tree whose shortest
height is shortest(x) means for
every path from root to leaf has at
shortest(x least shortest(x) nodes.
) Such that it has at least 2shortest(x)-1
nodes.
Leftist Trees
b) The rightmost root to external node path
is the shortest root to external node path.
x
Base on the definition of leftist tree.
shortest (left _ child ( x)) shortest (right _ child ( x))
shortest(x
)
Leftist Trees
Definition
A min-leftist tree (max leftist tree) is a
leftist tree in which the key value in each
node is no larger (smaller) than the key
values in its children (if any).
2
7 50
11 80
13
Leftist Trees
Step 2. If a has no
right_chlid, set b as a’s
right_child. Else,
recursively combine a’s
right_child and b.
2 5
7 50 9 8
11 80 12 10
13
20 18 15
Combination of leftist trees
2
11
50 5
13 80
9 8
12 10
20 18 15
Combination of leftist trees
2
7
5
11
9
13 50
12 8
80
10
20 18
15
Combination of leftist trees
2
7
5
11
9 8
13
12 10 50
20 18 15 80
Combination of leftist trees
2
2
1 7 5 2
1 11 1 9 8 2
1 13 2 12 10 1 50 1
1 20 1 18 15 1 80 1
Combination of leftist trees
2
2
5 2 1 7
8 2 1 9 1 11
10 1 50 1 2 12 1 13
15 1 80 1 1 20 1 18
Combination of leftist trees
Both insert and delete min operations
can be implemented by using the
combine operation.
Insert: Treat the inserting node as a single
node binary tree. Combine with the original
one.
Delete: Remove the node can get two
separate subtrees. Combine the two trees.
Binomial Heaps
Definition
Binomial Trees
[Definition] Binomial trees
A binomial tree Bk has 2k nodes with height
be k.
B0 B1 B2 B3 Bk
Bk-1
Bk-1
Binomial Trees
k
It has i ) nodes at depth i.
(
The ith child of root is the root of subtree
Bi-1. B4
Depth 1 : 4 nodes.
Depth 2 : 6 nodes.
Depth 3 : 4 nodes.
B3 B2 B1 B 0
Binomial Heaps
Collection of min (max) trees.
The min trees should be Binomial trees.
1
3 12 7 16
8 5 4 15 30 9
10 6 20
Binomial Heaps
The representation of B-heap:
Degree: number of children a node has.
Child: point to any one of its children.
Left_link, Right_link: maintain doubly
linked circular list of siblings.
The position of pointer a is the min
element.
Binomial Heaps
a
8 3 1
10 a pointer
5 4 12 7 16
Siblings
6 15 30 9 parent
child
20
Binomial Heaps
40 8 3 1
10
5 4 12 7 16
6 15 30 9
20
Combination of binomial
heaps
a
40 8 3 1
10
5 4 12 7 16
6 15 30 9
20
Combination of binomial
heaps
Pairwise combine a
8 3 1 20
10 40
5 4 12 7 16
6 15 30 9
20
Combination of binomial
heaps
Pairwise combine a
8 3 1 20
10 40
5 4 12 7 16
6 15 30 9
20
Combination of binomial
heaps
Pairwise combine a
8 3 1
20 10
5 4 12 7 16
40
6 15 30 9
20
Combination of binomial
heaps
Pairwise combine a
8 3 1
20 10
5 4 12 7 16
40
6 15 30 9
20
Combination of binomial
heaps
Pairwise combine a
3 1
8 5 4 12 7 16
20 10 15 30 9
6
40
20
Combination of binomial
heaps
Pairwise combine a
3 1
8 5 4 12 7 16
20 10 6 15 30 9
40
20
Combination of binomial
heaps
Pairwise combine a
3 1
8 5 4 12 7 16
20 10 6 15 30 9
40
20
Combination of binomial
heaps
Pairwise combine a
1
3 12 7 16
8 5 4 15 30 9
20 10 6 20
40
Combination of binomial
heaps
a
1
3 12 7 16
8 5 4 15 30 9
20 10 6 20
40
Combination of binomial
heaps
The insertion is to combine a single
vertex b-heap to original b-heap.
After we delete the min element , we
get several b-heaps that originally
subtrees of the removed vertex. Then
combine them together.
Time complexity
Definition
Fibonacci Heaps
Collection of min (max) trees.
The min trees need not be Binomial
trees.
B-heaps are a special case of F-heaps.
So that what B-heaps can do can be done
in F-heaps.
More than that, F-heap may delete an
arbitrary node and decrease key.
Fibonacci Heaps
The size of a subtree rooted in a node of
degree k is at least Fk + 2, where Fk is the kth
Fibonacci number.
merge is implemented simply by
concatenating the lists of tree roots of the
two heaps.
Operation insert works by creating a new
heap with one element and doing merge.
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
8 3 1
10
5 4 12 7 16
6 15 30 9
20
Deletion and Decrease key
Deletion
8 3 1
10
5 4 7 16
6 15 30 9
20
Deletion and Decrease key
Deletion
8 3 1
10
5 4 7 16
6 15 30 9
20
Deletion and Decrease key
Decrease key
8 3 1
10
5 4 12 7 16
6 11 15 30 9
20
Deletion and Decrease key
Decrease key
8 3 1
10
5 4 12 7 16
6 11 30 9
20
Deletion and Decrease key
Decrease key
8 3 1
10
5 4 12 7 16
6 11 30 9
20
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Fibonacci Heaps
Time complexity
Actual Amortized
Insert O(1) O(1)
Delete min (or max) O(n) O(log n)
Meld O(1) O(1)
Delete O(n) O(log n)
Decrease key (or O(n) O(1)
increase)
Skew Heaps
Skew Heap
• No need for null path length
• Definition of skew heap
4 6
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
6
4
27 20 12 14
43 15 25
7
6
4
19 8 8 7
y
27 20 12 14
null
43 15 25
7
6
4
19 8 8 7
27 20 12 14
43 15 25
8
7
6
4
19 Refers to node 8 7 x
27 20 8 14 8
43 12
15 25
8
7
6
4
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
8
7
6
4
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
6
4
Return node 7
Merging Skew Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
6
4
19 Refers to node 7 8
27 20 8 8 14
43 12
15 25
6
4
19 Refers to node 7 8
27 20 8 8 14
43 12
15 25
Return node 6
Merging Skew Heaps
4
19 6
27 20 7 8
8 14
43
12
15 25
7 8 27 20
8 14 43
12
15 25
7 8 27 20
8 14 43
12
15 25
Return node 4
Analysis
Height of a skew heap ≈ O(log n)
Maximum number of values stored in
Stack ≈ 2 * O(log n) ≈ O(log n)
Total cost of merge ≈ O(log n)
Inserts and Deletes
• To insert a node into a skew heap,
merge the leftist heap with the node
• After deleting root X from a skew heap,
merge its left and right subheaps
• In summary, there is only one
operation, a merge.