Professional Documents
Culture Documents
C-C Tsai
P.1
C-C Tsai
output
Priority Queue
insert(5,A)
{(5,A)}
insert(9,C)
{(5,A),(9,C)}
insert(3,B)
{(3,B),(5,A),(9,C)}
{(3,B),(5,A),(7,D),(9,C)}
insert(7,D)
minElement()
{(3,B),(5,A),(7,D),(9,C)}
minKey()
{(3,B),(5,A),(7,D),(9,C)}
removeMin()
{(5,A),(7,D),(9,C)}
size()
{(5,A),(7,D),(9,C)}
minElement()
{(5,A),(7,D),(9,C)}
deleteMin()
{(7,D),(9,C)}
deleteMin()
{(9,C)}
deleteMin()
{}
deleteMin()
error
{}
true
{}
isEmpty()
C-C Tsai
P.3
C-C Tsai
10
[1]
[1] 14
[5]
[3]
8
[6]
6
7
[4]
[3]
7
[5]
10
[6]
6
P.4
Primary operations
Insert
Delete Max
Delete Min
C-C Tsai
P.5
Leftist Trees
C-C Tsai
P.6
Internal node
External node
C-C Tsai
P.7
C-C Tsai
2
1
1
1
P.8
Is a leftist tree
2
1
C-C Tsai
P.9
shortest(x)
C-C Tsai
P.10
Min-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).
Two min-leftist trees:
2
7
11
13
50
12
80
20
10
18
15
C-C Tsai
P.11
C-C Tsai
P.12
1 7
1 50
1 11
1 9
1 80
2 12
1 13
1 20
2
5
1 8
1 10
1
1
18 15
combine
7
11
50
13
80
12
20
C-C Tsai
10
18
15
P.13
2
7
11
11
13
12
20
50
18
10
80
13
12
20
10
18
15
50
80
15
C-C Tsai
P.14
2
7
11
11
13
12
20
13
10
12
50
15
18
20
80
10
18
15
50
80
C-C Tsai
P.15
2 2
17
1 11
1 13
5 2
17
19
8 2
2 12
10 1
1 20 1 18
15 1
1 11
50 1
1 13
2 12
C-C Tsai
15 1
1 9
80 1
10 1
15 1
50 1
80 1
1 7
8 2
50 1
8 2
exchange2
5 2
10 1
19
1 20 1 18
80 1
exchange1
5 2
2 12
1 20 1 18
1 11
1 13
P.16
O(log n)
C-C Tsai
P.17
C-C Tsai
P.18
C-C Tsai
2
1
0
0
1
0
P.19
Binomial Heaps
Definition of Binomial trees:
k
A binomial tree Bk has 2 nodes with height be k.
B0
B1
B2
B3
Bk
Bk-1
Bk-1
B4
Depth 14 nodes.
Depth 26 nodes.
Depth 34 nodes.
C-C Tsai
B3
B2
B1 B0
P.20
10
1
3
8
10
12
4
15
30
16
20
C-C Tsai
P.21
12
16
pointer
Siblings
15
30
parent
child
C-C Tsai
20
P.22
11
40
10
12
15
30
16
20
C-C Tsai
P.23
40
10
C-C Tsai
12
15
20
30
16
P.24
12
10
12
15
30
20
40
16
20
C-C Tsai
P.25
Pairwise1 combine
a
20
10
12
16
40
6
C-C Tsai
15
20
30
P.26
13
20
10
12
16
40
15
30
20
C-C Tsai
P.27
Pairwise2 combine
a
20
10
12
15
30
16
40
C-C Tsai
20
P.28
14
20
10
12
15
30
16
40
20
C-C Tsai
P.29
Pairwise3 combine
a
20
40
C-C Tsai
10
12
15
20
30
16
P.30
15
a
1
20
C-C Tsai
10
12
15
30
16
20
P.31
40
Pairwise3 combine
a
1
20
C-C Tsai
40
10
12
15
30
16
20
P.32
16
C-C Tsai
P.33
Insert
O(n)
O(log n)
Meld
O(1)
O(1)
C-C Tsai
O(log n)
P.34
17
Fibonacci Heaps
A min (max) Fibonacci heap (F-heap) is a
collection of min (max) trees.
The min trees need not be Binomial trees.
B-heaps are a special case of F-heaps.
C-C Tsai
P.35
The deletion of 12
10
C-C Tsai
12
15
20
30
16
P.36
18
10
15
30
16
20
C-C Tsai
P.37
10
C-C Tsai
15
20
30
16
P.38
19
10
12
11 15
30
16
20
C-C Tsai
P.39
10
C-C Tsai
12
11
20
30
16
P.40
20
10
C-C Tsai
12
11
20
30
16
P.41
Cascading Cut
C-C Tsai
P.42
21
Decrease key by 2.
2 F
T 7
8
6
theNode
8
C-C Tsai
P.43
1
2 F
7
8
T 7
4
6
C-C Tsai
9
9
P.44
22
6
2 F
7
8
7
8
9
9
C-C Tsai
P.45
7
8
6
2 F
9
9
7
8
C-C Tsai
P.46
23
7
8
6
2 T
9
9
7
8
P.47
Actual
O(1)
Amortized
O(1)
O(n)
O(log n)
Meld
O(1)
O(1)
Delete
O(n)
O(log n)
O(n)
O(1)
C-C Tsai
P.48
24
Pairing Heaps
Insert
Fibonacci Pairing
O(1)
O(1)
O(n)
O(n)
Meld
O(1)
O(1)
Delete
O(n)
O(n)
O(n)
O(1)
C-C Tsai
P.49
Pairing Heaps
Insert
Fibonacci Pairing
O(1)
O(log n)
O(log n)
O(log n)
Meld
O(1)
O(log n)
Delete
O(log n)
O(log n)
O(1)
O(log n)
C-C Tsai
P.50
25
Pairing Heaps
Experimental results suggest that pairing heaps
are actually faster than Fibonacci heaps.
Simpler to implement.
Smaller runtime overheads.
Less space per node.
C-C Tsai
P.51
Definition
1
C-C Tsai
P.52
26
Node Structure
Child
Data
Note: No Parent, Degree, or ChildCut fields.
C-C Tsai
P.53
Compare roots.
Tree with smaller root becomes leftmost subtree.
+
6
P.54
27
Insert
+ insert(2) =
C-C Tsai
P.55
Insert
C-C Tsai
+ insert(14)=
P.56
28
Worst-Case Degree
Worst-case degree = n 1.
C-C Tsai
P.57
Worst-Case Height
Worst-case height = n.
3
2
1
C-C Tsai
P.58
29
IncreaseKey(theNode, theAmount)
C-C Tsai
P.59
IncreaseKey(theNode, theAmount)
9
theNode
4=>18
C-C Tsai
P.60
30
IncreaseKey(theNode, theAmount)
9
18
C-C Tsai
P.61
IncreaseKey(theNode, theAmount)
18
P.62
31
Delete Max
If empty => fail.
Otherwise, remove tree root and meld
subtrees into a single max tree.
How to meld subtrees?
C-C Tsai
P.63
C-C Tsai
P.64
32
Example
9
Delete max.
8
6
P.65
Example
C-C Tsai
P.66
33
Pass 2.
C-C Tsai
P.67
T2
S1
T3
T4
S2
T5
T6
S3
T7
T8
S4
R1
R2
C-C Tsai
R3
P.68
34
Multipass Scheme
Place the subtrees into a FIFO queue.
Repeat until 1 tree remains.
C-C Tsai
P.69
T2
T3
T4
T5
T6
T4
T5
T6
T7
C-C Tsai
T3
T7
T8
T5
T7
T8
S1
T6
T8
S1
S2
T7
T8
S1
S2
S3
P.70
35
Multipass Scheme--Example
T7
S1
T8
S1
S2
S2
S3
S3
S3
S4
R1
S4
R2
R1
C-C Tsai
P.71
Multipass Scheme--Example
R1
R2
Q1
P.72
36
C-C Tsai
P.73
Delete(theNode)
9
theNode
1
C-C Tsai
37
Delete(theNode)
9
4
3
C-C Tsai
P.75
Delete(theNode)
9
3
4
2
1
C-C Tsai
P.76
38
Delete(theNode)
9
1
C-C Tsai
P.77
MIN-MAX Heaps
min
70
40
30
45
C-C Tsai
9
50
30
10
20
12
max
15
min
max
P.78
39
70
30
45
50
max
40
30
10
20
12
15
min
max
C-C Tsai
P.79
70
30
45
C-C Tsai
50
max
40
30
20
12
15
10
min
max
P.80
40
70
30
45
50
max
40
30
20
12
15
10
min
max
C-C Tsai
P.81
C-C Tsai
P.82
41
parent=7;
We want to insertion 80 into the position 13
*n=13, item=80;
80>10 verify_max(heap,13,80)
min
7
40
70
30
45
9
50
30
15 min
10
20
12
max
80
max
C-C Tsai
P.83
min
7
40
70
30
45
9
50
C-C Tsai
30
12
verify_max(Heap,13,80)
grandparent=3
80>40
heap[13]=heap[3]
i=3
grandparent=0
15 min
10
20
max
Input:
80
max
P.84
42
min
7
80
70
30
45
9
50
30
15 min
10
20
12
max
40
Input
verify_max(Heap,3,80)
grandparent=null
break;
heap[3]=80;
max
C-C Tsai
P.85
The time complexity of insertion into a minmax heap with n elements is O(log n).
C-C Tsai
P.86
43
C-C Tsai
P.87
70
30
45
C-C Tsai
50
max
40
30
10
20
12
15
min
max
P.88
44
70
50
max
40
30
45
min
30
10
15
min
max
20
C-C Tsai
P.89
C-C Tsai
P.90
45
C-C Tsai
P.91
C-C Tsai
k
i
P.92
46
C-C Tsai
k
p
i
P.93
C-C Tsai
P.94
47
Delete 7
70
9
50
max
40
30
45
min
30
10
15
max
20
min
70
45
12
50
max
40
30
30
10
20
min
15
min
max
C-C Tsai
P.95
C-C Tsai
P.96
48
C-C Tsai
P.97
Example of a SMMH
Max-heap
Min-heap
5
45
10
15
C-C Tsai
19
25
30
40
20
P.98
49
SMMH
Let i be any node in left subtree and j be the
corresponding node in the right subtree. The relation
between i and j.
j = i + 2log2 i1;
if ( j > n)
j / = 2;
Ex1:
i = 4;
j = 4+2(2-1) = 6;
1
2
10
6
11
Ex2:
7
i = 9;
j = 9+2(3-1) = 13;
j =13 > 12 j = 6;
12
C-C Tsai
P.99
1.
2.
1.
2.
C-C Tsai
n 2log2 n1
(n + 2 log 2 n 1 ) / 2 )
P.100
50
45
10
15
19
25
30
20
40
i
C-C Tsai
P.101
45
10
15
C-C Tsai
25
30
20
40
19
P.102
51
45
15
10
25
30
20
40
19
C-C Tsai
P.103
The time complexity is O(log n) as the height of the SMMH is O(log n).
C-C Tsai
P.104
52
1.
2.
3.
C-C Tsai
P.105
45
10
15
C-C Tsai
19
25
30
20
40
temp
P.106
53
45
i
10
25
40
j
15
19
30
temp:20
C-C Tsai
P.107
45
10
25
40
i
15
19
30
temp:20
j
C-C Tsai
P.108
54
45
10
15
19
25
30
40
temp:20
i
C-C Tsai
P.109
45
10
15
C-C Tsai
19
20
25
40
30
P.110
55
The time complexity is O(log n) as the height of the SMMH is O(log n).
C-C Tsai
P.111
Interval Heaps
C-C Tsai
P.112
56
15,80
15,20
20,70
25,60
28,55
30,50
30,60
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
35
15,80
20,70
25,60
28,55
35
30,50
30,60
15,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
57
Insert An Element
10,90
15,80
15,20
20,70
25,60
28,55
30,50
27,35
35
30,60
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
Insert 27.
New element becomes a left end point.
Insert new element into min heap.
Another Insert
10,90
15,80
20,70
25,60
28,55
30,50
35
18,35
30,60
15,20
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
Insert 18.
New element becomes a left end point.
Insert new element into min heap.
58
Another Insert
10,90
15,80
15,20
20,70
25,60
18,60
28,55
30,50
25,35
30,60
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
Insert 18.
New element becomes a left end point.
Insert new element into min heap.
Another Insert
10,90
15,80
20,70
18,70
,70
20,60
28,55
30,50
25,35
30,60
15,20
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
Insert 18.
New element becomes a left end point.
Insert new element into min heap.
59
15,80
15,20
20,70
25,60
28,55
30,50
30,60
16,19
35,50
45,60
17,17
50,55
47,58
40,45
40,43
Insert 82.
New element becomes a right end point.
35
35,82
After 82 Inserted
10,90
15,82
20,80
25,70
28,55
30,50
30,60
15,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
35,60
60
15,82
15,20
20,80
25,70
30,50
16,19
17,17
35,50
45,60
50,55
47,58
40,45
40,43
Insert 8.
New element becomes both a left and a
right end point.
Insert new element into min heap.
28,55
30,60
After 8 Is Inserted
8,90
10,82
15,80
20,70
28,55
30,50
30,60
15,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
25
61
Min element
0 => fail.
1 => heap becomes empty.
2 => only one node, take out left end point.
2 => not as simple.
C-C Tsai
P.123
35
15,82
20,80
25,70
28,55
30,50
35,60
30,60
15,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
62
20,80
25,70
28,55
30,50
60
30,60
16,19
35,50
45,60
17,17
50,55
47,58
40,45
40,43
15,82
30,60
35
20,80
25,70
28,55
60
30,50
15,20
,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
63
15,82
30,60
16,35
20,80
35,50
45,60
20
25,70
28,55
30,50
60
16,19
,19
17,17
50,55
47,58
40,45
40,43
15,82
20,80
25,70
28,55
30,50
30,60
16,35
19,20
17,17
45,60
50,55
47,58
35,50
40,45
40,43
60
64
P.129
15,80
20,70
25,60
28,55
35
30,50
30,60
15,20
16,19
17,17
45,60
50,55
47,58
35,50
40,45
40,43
65