Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous 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

  equation26590

where tex2html_wrap_inline66201 is the tex2html_wrap_inline57847 binary digit   or bit  in the representation of n. For example, n=27 is expressed as the binary number tex2html_wrap_inline66209 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_inline66215 if the tex2html_wrap_inline57847 bit in the binary representation of n is a one. That is, the forest tex2html_wrap_inline59871 which contains exactly n items is given by

displaymath66193

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

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

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

tex2html_wrap_inline65981 tex2html_wrap_inline66117 tex2html_wrap_inline63013 tex2html_wrap_inline65975 tex2html_wrap_inline65973 tex2html_wrap_inline66267
+ tex2html_wrap_inline66117 tex2html_wrap_inline63013 tex2html_wrap_inline65975 tex2html_wrap_inline63013 tex2html_wrap_inline66293

tex2html_wrap_inline66295 tex2html_wrap_inline63013 tex2html_wrap_inline63013 tex2html_wrap_inline66301 tex2html_wrap_inline63013 tex2html_wrap_inline65973 tex2html_wrap_inline66307

Therefore, the result is tex2html_wrap_inline66309.


next up previous index

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