1 Binomial Tree Binomial tree. n Recursive definition: B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4
2 Binomial Tree Useful properties of order k binomial tree B k. n Number of nodes = 2 k. n Height = k. n Degree of root = k. n Deleting root yields binomial trees B k-1, …, B 0. Proof. n By induction on k. B0B0 B1B1 B2B2 B3B3 B4B4 B1B1 BkBk B k+1 B2B2 B0B0
3 Binomial Tree A property useful for naming the data structure. n B k has nodes at depth i. B4B4 depth 2 depth 3 depth 4 depth 0 depth 1
4 Binomial Heap Binomial heap. Vuillemin, n Sequence of binomial trees that satisfy binomial heap property. – each tree is min-heap ordered – 0 or 1 binomial tree of order k B4B4 B0B0 B1B
5 Binomial Heap: Implementation Implementation. n Represent trees using left-child, right sibling pointers. – three links per node (parent, left, right) n Roots of trees connected with singly linked list. – degrees of trees strictly decreasing from left to right heap 29 Leftist Power-of-2 HeapBinomial Heap Parent Left Right
6 Binomial Heap: Properties Properties of N-node binomial heap. n Min key contained in root of B 0, B 1,..., B k. n Contains binomial tree B i iff b i = 1 where b n b 2 b 1 b 0 is binary representation of N. n At most log 2 N + 1 binomial trees. n Height log 2 N . B4B4 B0B0 B1B N = 19 # trees = 3 height = 4 binary = 10011
7 Binomial Heap: Union Create heap H that is union of heaps H' and H''. n "Mergeable heaps." n Easy if H' and H'' are each order k binomial trees. – connect roots of H' and H'' – choose smaller key to be root of H H'' H'
8 Binomial Heap: Union =
9 Binomial Heap: Union
10 Binomial Heap: Union
15 Binomial Heap: Union Create heap H that is union of heaps H' and H''. n Analogous to binary addition. Running time. O(log N) n Proportional to number of trees in root lists 2( log 2 N + 1) = 26
H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H' broken binomial trees n H Union(H', H) Running time. O(log N)
17 Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H' broken binomial trees n H Union(H', H) Running time. O(log N) H H'
x H Binomial Heap: Decrease Key Decrease key of node x in binomial heap H. n Suppose x is in binomial tree B k. n Bubble node x up the tree if x is too small. Running time. O(log N) n Proportional to depth of node x log 2 N . depth = 3
19 Binomial Heap: Delete Delete node x in binomial heap H. n Decrease key of x to - . n Delete min. Running time. O(log N)
20 Binomial Heap: Insert Insert a new node x into binomial heap H. n H' MakeHeap(x) n H Union(H', H) Running time. O(log N) H x H'
21 Binomial Heap: Sequence of Inserts Insert a new node x into binomial heap H. If N = , then only 1 steps. If N = , then only 2 steps. If N = , then only 3 steps. If N = , then only 4 steps. Inserting 1 item can take (log N) time. If N = , then log 2 N steps. But, inserting sequence of N items takes O(N) time! n (N/2)(1) + (N/4)(2) + (N/8)(3) +... 2N n Amortized analysis. n Basis for getting most operations down to constant time x
22 Priority Queues make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary log N 1 N 1 Binomial log N 1 Fibonacci * 1 1 log N Relaxed 1 1 log N Linked List 1 N N 1 1 N is-empty11111 Heaps just did this