19-2 Binomial trees and binomial heaps

The binomial tree BkB_k is an ordered tree (see Section B.5.2) defined recursively. As shown in Figure 19.6(a), the binomial tree B0B_0 consists of a single node. The binomial tree BkB_k consists of two binomial trees Bk1B_{k - 1} that are linked together so that the root of one is the leftmost child of the root of the other. Figure 19.6(b) shows the binomial trees B0B_0 through B4B_4.

a. Show that for the binomial tree BkB_k,

  1. there are 2k2^k nodes,
  2. the height of the tree is kk,
  3. there are exactly (ki)\binom{k}{i} nodes at depth ii for i=0,1,,ki = 0, 1, \ldots, k, and
  4. the root has degree kk, which is greater than that of any other node; moreover, as Figure 19.6(c) shows, if we number the children of the root from left to right by k1,k2,,0k - 1, k - 2, \ldots, 0, then child ii is the root of a subtree BiB_i.

A binomial heap HH is a set of binomial trees that satisfies the following properties:

  1. Each node has a keykey (like a Fibonacci heap).
  2. Each binomial tree in HH obeys the min-heap property.
  3. For any nonnegative integer kk, there is at most one binomial tree in HH whose root has degree kk.

b. Suppose that a binomial heap HH has a total of nn nodes. Discuss the relationship between the binomial trees that HH contains and the binary representation of nn. Conclude that HH consists of at most lgn+1\lfloor \lg n \rfloor + 1 binomial trees.

Suppose that we represent a binomial heap as follows. The left-child, right-sibling scheme of Section 10.4 represents each binomial tree within a binomial heap. Each node contains its key; pointers to its parent, to its leftmost child, and to the sibling immediately to its right (these pointers are NIL\text{NIL} when appropriate); and its degree (as in Fibonacci heaps, how many children it has). The roots form a singly linked root list, ordered by the degrees of the roots (from low to high), and we access the binomial heap by a pointer to the first node on the root list.

c. Complete the description of how to represent a binomial heap (i.e., name the attributes, describe when attributes have the value NIL\text{NIL}, and define how the root list is organized), and show how to implement the same seven operations on binomial heaps as this chapter implemented on Fibonacci heaps. Each operation should run in O(lgn)O(\lg n) worst-case time, where nn is the number of nodes in the binomial heap (or in the case of the UNION\text{UNION} operation, in the two binomial heaps that are being united). The MAKE-HEAP\text{MAKE-HEAP} operation should take constant time.

d. Suppose that we were to implement only the mergeable-heap operations on a Fibonacci heap (i.e., we do not implement the DECREASE-KEY\text{DECREASE-KEY} or DELETE\text{DELETE} operations). How would the trees in a Fibonacci heap resemble those in a binomial heap? How would they differ? Show that the maximum degree in an nn-node Fibonacci heap would be at most lgn\lfloor \lg n\rfloor.

e. Professor McGee has devised a new data structure based on Fibonacci heaps. A McGee heap has the same structure as a Fibonacci heap and supports just the mergeable-heap operations. The implementations of the operations are the same as for Fibonacci heaps, except that insertion and union consolidate the root list as their last step. What are the worst-case running times of operations on McGee heaps?

a.

  1. BkB_k consists of two binomial trees Bk1B_{k - 1}.
  2. The height of one Bk1B_{k - 1} is increased by 11.
  3. For i=0i = 0, (k0)=1\binom{k}{0} = 1 and only root is at depth 00. Suppose in Bk1B_{k - 1}, the number of nodes at depth ii is (k1i)\binom{k - 1}{i}, in BkB_k, the number of nodes at depth ii is (k1i)+(k1i1)=(ki)\binom{k - 1}{i} + \binom{k - 1}{i - 1} = \binom{k}{i}.
  4. The degree of the root increase by 11.

b. Let n.bn.b denote the binary expansion of nn. The fact that we can have at most one of each binomial tree corresponds to the fact that we can have at most 11 as any digit of n.bn.b. Since each binomial tree has a size which is a power of 22, the binomial trees required to represent n nodes are uniquely determined. We include BkB_k if and only if the kkth position of n.bn.b is 11. Since the binary representation of nn has at most lgn+1\lfloor \lg n \rfloor+ 1 digits, this also bounds the number of trees which can be used to represent nn nodes.

c. Given a node xx, let x.keyx.key, x.px.p, x.cx.c, and x.sx.s represent the attributes key, parent, left-most child, and sibling to the right, respectively. The pointer attributes have value NIL\text{NIL} when no such node exists. The root list will be stored in a singly linked list.

  • MAKE-HEAP initialize an empty list for the root list and return a pointer to the head of the list, which contains NIL\text{NIL}. This takes constant time. To insert: Let xx be a node with key kk, to be inserted. Scan the root list to find the first mm such that BmB_m is not one of the trees in the binomial heap. If there is no B0B_0, simply create a single root node xx. Otherwise, union x,B0,B1,,Bm1x, B_0, B_1, \ldots, B_{m - 1} into a BmB_m tree. Remove all root nodes of the unioned trees from the root list, and update it with the new root. Since each join operation is logarithmic in the height of the tree, the total time is O(lgn)O(\lg n). MINIMUM\text{MINIMUM} just scans the root list and returns the minimum in O(lgn)O(\lg n), since the root list has size at most O(lgn)O(\lg n).
  • EXTRACT-MIN: finds and deletes the minimum, then splits the tree Bm which contained the minimum into its component binomial trees B0,B1,,Bm1B_0, B_1, \ldots, B_{m - 1} in O(lgn)O(\lg n) time. Finally, it unions each of these with any existing trees of the same size in O(lgn)O(\lg n) time.
  • UNION: suppose we have two binomial heaps consisting of trees Bi1,Bi2,,BikB_{i_1}, B_{i_2}, \ldots, B_{i_k} and Bj1,Bj2,,BjmB_{j_1}, B_{j_2}, \ldots, B_{j_m} respectively. Simply union orresponding trees of the same size between the two heaps, then do another check and join any newly created trees which have caused additional duplicates. Note: we will perform at most one union on any fixed size of binomial tree so the total running time is still logarithmic in nn, where we assume that nn is sum of the sizes of the trees which we are unioning.
  • DECREASE-KEY: simply swap the node whose key was decreased up the tree until it satisfies the min-heap property. This method requires that we swap the node with its parent along with all their satellite data in a brute-force manner to avoid updating pp attributes of the siblings of the node. When the data stored in each node is large, we may want to update pp instead, which, however, will increase the running time bound to O(lg2n)O(\lg^2 n).
  • DELETE: note that every binomial tree consists of two copies of a smaller binomial tree, so we can write the procedure recursively. If the tree is a single node, simply delete it. If we wish to delete from BkB_k, first split the tree into its constituent copies of Bk1B_{k - 1}, and recursively call delete on the copy of Bk1B_{k - 1} which contains xx. If this results in two binomial trees of the same size, simply union them.

d. The Fibonacci heap will look like a binomial heap, except that multiple copies of a given binomial tree will be allowed. Since the only trees which will appear are binomial trees and BkB_k has 2k2k nodes, we must have 2kn2k \le n, which implies klgnk \le \lfloor \lg n \rfloor. Since the largest root of any binomial tree occurs at the root, and on BkB_k it is degree kk, this also bounds the largest degree of a node.

e. INSERT\text{INSERT} and UNION\text{UNION} will no longer have amortized O(1)O(1) running time because CONSOLIDATE\text{CONSOLIDATE} has runtime O(lgn)O(\lg n). Even if no nodes are consolidated, the runtime is dominated by the check that all degrees are distinct.

Since calling UNION\text{UNION} on a heap and a single node is the same as insertion, it must also have runtime O(lgn)O(\lg n). The other operations remain unchanged.