Documente Academic
Documente Profesional
Documente Cultură
Rabea Aden
• No structural constraint
If X is root
Parent (X) = root
Left Right
Data
Pointer Pointer
• X is light if
w(X) > w(Parent(X)) / 2
• It is a fundamental operation
– Both Insert and DeleteMin are based on Merge
– Insert:
• The key to be inserted is a one-node heap
• Merge this one-node heap with the given min leftist heap
– DeleteMin
• Delete root, left and right child of root are two min leftist heaps
• Merge the two min leftist heaps
1 2
5 8 4 7
6 12 16 36 14 10 18 22
40 28 25
5 8 4 7
6 12 16 36 14 10 18 22
40 28 25
1 H1
2
5 8 4 7
6 12 16 36 14 10 18 22
40 28 25
5 8 4
4 7
7
6 12 16 36 14
14 10
10 18
18 22
22
40 28
28 25
25
1 8
H1
5 2 16 36
6 12 4 7 40
14 10 18 22
29
28 26
25
5 2 16 36
H1
6 12 4 7 40
14 10 18 22
28 25
1 8
5 2 H1 16 36
6 12 4 7 40
14 10 18 22
28 25
5 2 16 36
6 12 4 7 40
H1
14 10 18 22
8
2828 1625 36
40
1 22 0 091H2
17 37410
5 2 25
6 12 4 7 H1
14 10 18 8
28 16 36
40
5 2 25
6 12 4 7
14 10 18 8 H1
28 16 36
40
1 36 0 091H2
17 37410
5 2 40
6 12 4 7
14 10 18 8 H1
28 16 22
25
NULL
Since, rightChild(H1) is NULL
rightChild(H1) = H2
H1 is NULL
1 36
5 2 40
6 12 4 7
14 10 18 8
28 16 22 H1
25
NULL
Merge (H1, H2)
H2 is NULL
1
NULL
5 2
28 16 22 H1
25 36
40
NULL
rightChild(H1) is NULL
NUCES-FAST • Advanced Data Structures • Spring 2007
Merge H1 & H2
swap( leftChild(H1), rightChild(H1) )
5 2
6 12 4 7
H1 moves up to '22'
14 10 18 8
28 16 22 H1
25
36 36
25
40
40 40
5 2
6 12 4 7 H1 moves up to '8'
H1
14 10 18 8
28 16
16 22
22
25
36 25
40
40
5 2
H1
6 12 4 7 H1 moves up to '7'
14 10 18
18 8
28
28 22 16
16
36 25
40
H1
5 2
6 4
12 7 4
7 H1 moves up to '2'
814
14 1018 814 1018
22 16 28 22 16 28
36 25 36 25
40 40
11 H1
52 25
7 6 12 74 6 12 4 H1 moves up to '1'
8 18 8 14 1018 14 10
22 16 28 22 16 28
36 25 36 25
40 40
2 5
7 4 6 12 H1 is returned
Root pointer now points to H1
8 18 14 10
22 16 28
36 25
40
1 2
5 8
6 12 16 36
40
H1 — existing Heap
H2 — one-node heap containing node to be inserted
Both H1 and H2 are Min Leftist Heaps
NUCES-FAST • Advanced Data Structures • Spring 2007
Insert 2
H1
H1 H2
5 8
6 12 16 36
0
40
1 2
H1
5 8
6 12 16 36
40
5 8
6 12 16 36
40
1 8
H1
5 22 16 36
6 12 8 40
NULL
16 36
40
H2 is NULL
11 H1
NULL
55 22 H1
66 12
12 88 H1 Swap children
H1 of each node
on the Rightmost Path
16
16 36
36 except the last node
40
40 NULL
rightChild(H1) is NULL
5 22
6 12 8 H1 H1 moves up to '8'
16
36
16 36
16
36
40 40
40
11
55 22 H1
66 12
12 8
NULL 88 H1 moves up to '2'
36 36
16
36 16
16
40 40
40
1 H1
5
2 5
2
8
6 12
12 6
8 12
NULL H1 moves up to '1'
36 16 36 16
40 40
2 5
8 6 12 H1 is returned
Root pointer now points to H1
36 16
40
2
2
1
1 1
6
5 9
8
0 0 0 0
7
6 13
12 17
16 37
36
0
41
40
H1
11 H2
5 8
6 12 16 36
40
H1
1
H2
6
5 Both H1 and H2 are not NULL 8
7
6 13
12 16 36
40
41
H1
1
H2
5 H1 8
6 12 16 36
40
1
H2
6 12 16 36
40
1
H2
5 H1 12
6 8
16 36
40
1
H2
6 8 H1
0
17 36
40
1
H2
5 36
6 8 H1 40
16 12
36
rightChild(H1) is NULL 40
1
H2
5 36
NULL
6 8 H1 40
16 12
36
NULL
40
H2 is NULL
1
H2
5
NULL
6 8 H1
6 88 H1 moves up to '12'
16
16 12 H1
36
NULL 36
NULL
40 40
40
6 88 H1
H1 moves up to '8'
16
12
16 12
16
12
36 36
36 NULL
40 40
40
55 H1
6
8 6
88 H1 moves up to '5'
12 16
12 16
16
36 36
40 40
8 6 H1 is returned
Root pointer now points to H1
12 16
36
40