210 likes | 329 Views
Binomial Heaps. i. B k-1. B k-1. Binomial trees: B 0 has a single node B k : Ex. B 1. B 2. B 3. Lemma1 For the binomial tree B k , 1. there are 2 k nodes, 2. the height if the tree is k , 3. there are exactly nodes at depth i for i= 0, 1, … ., k,
E N D
i Bk-1 Bk-1 • Binomial trees: B0 has a single node Bk: Ex B1 B2 B3
Lemma1 For the binomial tree Bk, 1. there are 2k nodes, 2. the height if the tree is k, 3. there are exactly nodes at depth i for i= 0, 1, …., k, 4. the root has degree k, which is greater than that of any other node; moreover if the children of the root are numbered from left to right by k-1, k-2, …, 0, child I is the root of a subtree Bi Proof: 1) By induction, 2k-1 + 2k-1 = 2k 2) By induction, 1 + (k-1) = k 3) , by induction
Corollary: The maximum degree of any node in an n-node binomial tree is lg(n) • Binomial heaps: H: a set of binomial trees satisfying the following: 1. Each binomial tree in H is heap-ordered: the key of a node is greater than or equal to the key of its parent 2. There is at most one binomial tree in H whose root has a given degree Note: By 2. an n-node binomial heap H consists of at most binomial trees
Representation of binomial heaps 6 1 (a) head[H] 10 12 25 8 14 29 11 18 17 38 27
/ / / 10 1 6 0 2 3 / / 12 25 8 14 29 1 2 1 0 / / / / 18 11 17 38 0 1 0 0 / / / / / 27 0 / / (b) head[H] p key degree child sibling
Operations on binomial heaps • Creating a new binomial heap • Finding the minimum key • Uniting 2 binomial heaps Time: O( lg n)
Binomial-Heap-Merge Bk-1 Bk-1 Y, z : Bk-1 trees
6 (a) head[H1] head[H2] 3 18 7 15 12 37 29 10 44 8 25 28 33 48 22 17 31 30 23 41 50 24 32 45 55 Binomial-Heap-Merge Output of sorted degree x next-x 7 6 12 (b) head[H] 18 15 3 25 29 10 44 8 37 28 33 48 22 17 31 30 23 41 Case 3 50 24 32 45 55
sibling[next-x] next-x x 6 7 (c) head[H] 15 12 3 29 10 44 8 37 25 28 33 18 48 22 17 31 30 23 41 Case 2 50 24 32 45 55 x prev-x next-x 7 6 (d) head[H] 15 12 3 29 10 44 8 37 28 33 18 25 48 22 17 31 30 23 41 Case 4 50 24 32 45 55
x next-x prev-x 6 (d) head[H] 3 15 12 29 10 44 8 28 33 7 37 18 48 22 17 31 30 23 41 25 Case 3 50 24 32 45 55 prev-x x next-x (d) head[H] 12 3 6 29 10 44 8 18 7 37 15 48 22 17 31 30 23 28 33 25 Case 1 50 24 32 45 41 55
Case 3 Case 4
next-x (a) prev-x x sibling[next-x] prev-x x next-x …. …. …. …. b a c d b a c d Case 1 Bk Bl Bk Bl next-x (b) prev-x x sibling[next-x] prev-x x next-x …. …. …. b a c d b a c d Case 2 Bk Bk Bk Bk Bk Bk
c b b c next-x (c) prev-x x sibling[next-x] prev-x x next-x …. …. …. b a c d a d Case 3 Bk Bk Bl Bk Bl Bk Bk+1 next-x prev-x x (d) prev-x x sibling[next-x] …. …. …. b a c d a d Case 4 Bk Bk Bk Bk Bl Bk Bk+1
Insert a node • Extracting the node with minimum key z x y x
(a) head[H] 1 37 10 16 12 25 6 41 28 13 26 29 18 23 8 14 77 42 38 17 11 27 x 1 (b) head[H] 37 10 16 12 25 6 41 28 13 26 29 18 23 8 14 77 42 38 17 11 27
12 12 18 18 16 16 6 6 26 26 29 29 23 23 8 8 14 14 37 10 42 42 38 38 17 17 11 11 27 27 41 28 13 (d)head[H] 77 25 (c) head[H] 37 head[H’] 10 25 41 28 13 77
Decreasing a key • Deleting a key
12 12 18 18 6 6 29 29 8 8 14 14 37 37 10 38 38 17 17 11 11 27 27 41 41 28 13 77 (a)head[H] 25 z 16 y 7 23 42 (b)head[H] 25 z 10 y 28 13 7 16 23 77 42
12 18 37 41 (c)head[H] z 25 6 y 29 8 14 7 38 17 11 28 13 10 27 16 23 77 42