14.2.1 Binheap
14.2.1 Binheap
Binomial Trees
Definition is inductive.
These are ordered trees, i.e., order of children is important.
1 1
Different Trees
2 3 3 2
4 5 4 5
Base Case: B0 = single node is a binomial tree.
Inductive Step:
B1
B2
B3
depth # nodes
0 1
B4
1 4
2 6
3 4
4 1
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 3
Another Way to Look at Bk
B0
B1
B2
Bk-2
Bk-1
Bk
child
childof ofroot
rootisisroot
rootof
ofsubtree
subtreeBBi,i,where
whereii==k–1,
k–1,k–2,
k–2,…,…,
00[k–1 [k–1isisLM,
LM,00isisRM].
RM].
Proof:
1. Induction on k. 2. Induction on k.
# nodes in B k height of Bk
2(# nodes in B k -1 ) 1 height of Bk -1
2(2 k 1 ) 1 (k 1)
2 k k
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 5
Proof (Continued)
k
3. Let D(k,i) = # nodes at depth i in Bk. Want to show D(k,i) =
i
.
Bk
Bk-1 depth i in Bk
Bk-1
depth i–1 in this Bk-1
So,
D(k, i) (k 1)!(k i) i(k 1)!
D(k 1, i) D(k 1, i 1) i!(k i)!
k 1 k 1 k! i(k 1)!i(k 1)!
, by ind. hyp.
i i 1 i!(k i)!
(k 1)! (k 1)! k
i!(k 1 i)! (i 1)!(k i)! i
Jim Anderson Comp 750, Fall 2009 Binomial Heaps - 6
Proof (Continued)
4. Root degree of Bk = 1 + root degree of Bk-1
= 1 + k–1 , induction hypothesis
=k
Corollary
Corollary20.2:
20.2:The
Themaximum
maximumdegree
degreein
inan
ann-node
n-nodebinomial
binomialtree
treeisis
lg
lgn.n.
i 1
18 17 38
11
27
10 1 6
12 25 14 29
8
18 17 38
11
27
z
y z y
Link Bk-1
Bk-1 Bk-1
Bk-1
H1 = H2 =
First, simply merge the two root lists by root degree (like merge sort).
Remaining Problem: Can have two trees with the same root degree.
prev-x x next-x
Note: We may temporarily create three trees with the same root
degree.
25 28 33 37 10 44
8 29
41 48 31 17
23 22
30
24 50
45 32
Union
55
18
15 7 37
10 44 1 1 1 (carries)
8 29
00111
25
28 33
30 23 22 48 31 17
10011
41
24 50
11010
45 32
Bk Bl Bk Bl
Bk Bk Bk Bk Bk Bk
prev-x x next-x sibling[next-x] prev-x x next-x
a b c d Case 3 a b d
c
Bk Bk Bl Bk Bl
key[x] key[next[x]]
Bk
Bk+1
prev-x x next-x sibling[next-x] prev-x x next-x
a d Case 4 a d
b c c
b
Bk Bk Bl Bk Bl
key[x] > key[next[x]] Bk
Jim Anderson Comp 750, Fall 2009 Bk+1 Binomial Heaps - 16
Union Example
head[H1] 12 7 15 head[H2] 18 3 6
25 28 33 37 10 44
8 29
41 48 31 17
23 22
30
24 50
45 32
55
Merge
x next-x
head[H] 12 18 7 3 15 6
25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
55
25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
55
Case 3
x next-x
head[H] 12 7 3 15 6
18 25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
55
18 25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
55
Case 2
prev-x x next-x
head[H] 12 7 3 15 6
18 25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
55
18 25 37 28 33 44
29 10
8
41
22 48 31 17
30 23
24 50
45 32
Case 4 55
prev-x x next-x
head[H] 12 3 15 6
18 7 37 28 33 44
29 10
8
25 41
22 48 31 17
30 23
24 50
45 32
55
18 7 37 28 33 44
29 10
8
25 41
22 48 31 17
30 23
24 50
45 32
Case 3 55
prev-x x next-x
head[H] 12 3 6
18 7 37 44
15 29 10
8
33 25 17
28 22 48 31
30 23
41 50
32 24
45
55
18 7 37 44
15 29 10
8
33 25
28 22 48 31 17
30 23
41 50
32 24
45
Case 1 55
33 25 17
28 22 48 31
30 23
41
45 32 24 50 Note: Union is
O(lg n).
55
41 28 13 25
16 12
6
77
29 26 23 18
8 14
38 42
11 17
27
x
head[H] 37 10 1
41 28 13 25
16 12
6
77
29 26 23 18
8 14
38 42
11 17
27
41 28 13 18 26 23 14 29
8
77 42 38
11 17
27
head[H] 25 12 6
37 18 29
8 14
10
41
13 11 17 38
16 28
77 27
26 23
42
O(lg n)
37 18 29
8 14
10
41
13 11 17 38
16 28
77 27
26 23
42
Decrease key 26 to 7
head[H] 25 12 6
37 18 29
8 14
10
41
17 38
z 16 28 13 11
27
y 7 23 77
42
37 18
z 10 8 14 29
41
y 7 28 13 11 17 38
77 27
16 23
42
z
head[H] 25 12 6
37 18 y 8 14 29
7
41
13 11 17 38
10 28
77 27
16 23
42
Time is O(lg n)