Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Binomial Queues

If binomial trees only come in sizes that are powers of two, how do we implement a container which holds an arbitrary number number of items n using binomial trees? The answer is related to the binary representation of the number n. Every non-negative integer n can be expressed in binary form as

  equation27169

where tex2html_wrap_inline66822 is the tex2html_wrap_inline58387 binary digit   or bit  in the representation of n. For example, n=27 is expressed as the binary number tex2html_wrap_inline66830 because 27=16+8+2+1.

To make a container which holds exactly n items we use a collection of binomial trees. A collection of trees is called a forest . The forest contains binomial tree tex2html_wrap_inline66836 if the tex2html_wrap_inline58387 bit in the binary representation of n is a one. I.e., the forest tex2html_wrap_inline60413 which contains exactly n items is given by

displaymath66814

where tex2html_wrap_inline66846 is determined from Equation gif. For example, the forest which contains 27 items is tex2html_wrap_inline66848

The analogy between tex2html_wrap_inline60413 and the binary representation of n carries over to the merge operation. Suppose we have two forests, say tex2html_wrap_inline60413 and tex2html_wrap_inline66856. Since tex2html_wrap_inline60413 contains n items and tex2html_wrap_inline66856 contains m items, the combination of the two contains n+m items. Therefore, the resulting forest is tex2html_wrap_inline66868.

For example, consider n=27 and m=10. In this case, we need to merge tex2html_wrap_inline66874 with tex2html_wrap_inline66876. Recall that two binomial trees of order k can be combined to obtain a binomial tree of order k+1. E.g., tex2html_wrap_inline66882. But this is just like adding binary digits! In binary notation, the sum 27+10 is calculated like this:

11011
+ 1010

100101

The merging of tex2html_wrap_inline66888 and tex2html_wrap_inline66890 is done in the same way:

tex2html_wrap_inline66602 tex2html_wrap_inline66738 tex2html_wrap_inline63650 tex2html_wrap_inline66596 tex2html_wrap_inline66594 tex2html_wrap_inline66888
+ tex2html_wrap_inline66738 tex2html_wrap_inline63650 tex2html_wrap_inline66596 tex2html_wrap_inline63650 tex2html_wrap_inline66914

tex2html_wrap_inline66916 tex2html_wrap_inline63650 tex2html_wrap_inline63650 tex2html_wrap_inline66922 tex2html_wrap_inline63650 tex2html_wrap_inline66594 tex2html_wrap_inline66928

Therefore, the result is tex2html_wrap_inline66930.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.