Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
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

  equation26414

where tex2html_wrap_inline65790 is the tex2html_wrap_inline57420 binary digit   or bit  in the representation of n. For example, n=27 is expressed as the binary number tex2html_wrap_inline65798 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_inline65804 if the tex2html_wrap_inline57420 bit in the binary representation of n is a one. That is, the forest tex2html_wrap_inline59440 which contains exactly n items is given by

displaymath65782

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

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

For example, consider n=27 and m=10. In this case, we need to merge tex2html_wrap_inline65842 with tex2html_wrap_inline65844. Recall that two binomial trees of order k can be combined to obtain a binomial tree of order k+1. For example, tex2html_wrap_inline65850. 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_inline65856 and tex2html_wrap_inline65858 is done in the same way:

tex2html_wrap_inline65570 tex2html_wrap_inline65706 tex2html_wrap_inline62608 tex2html_wrap_inline65564 tex2html_wrap_inline65562 tex2html_wrap_inline65856
+ tex2html_wrap_inline65706 tex2html_wrap_inline62608 tex2html_wrap_inline65564 tex2html_wrap_inline62608 tex2html_wrap_inline65882

tex2html_wrap_inline65884 tex2html_wrap_inline62608 tex2html_wrap_inline62608 tex2html_wrap_inline65890 tex2html_wrap_inline62608 tex2html_wrap_inline65562 tex2html_wrap_inline65896

Therefore, the result is tex2html_wrap_inline65898.


next up previous contents index

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