A binomial queue is not one heap-ordered tree, but rather a collection of heap-ordered trees, known as a forest. Each heap-ordered tree is a binomial tree.
A binomial tree of height 0 is a one-node tree.
A binomial tree, Bk, of height k is formed by attaching a binomial tree,Bk–1, to the root of another binomial tree, Bk–1.
Bk consists of a root with k children, which are B0,B1,⋯,Bk−1.
Bk has exactly 2k nodes.
The number of nodes at depth d is Ckd, which is binomial coefficient.
A priority queue of any size can be uniquely represented by a collection of binomial trees.
Operations
FindMin
The minimum key is in one of the roots.
There are at most ⌈logN⌉ roots, hence Tp=O(logN).
Note: We can remember the minimum and update whenever it is changed. Then this operation will take O(1).
Merge
Tp=O(logN), but must keep the trees in the binomial queue sorted by height.
Insert
a special case for merging
Note:
If the smallest nonexistent binomial tree is Bi , then Tp=Const⋅(i+1).
Performing NInserts on an initially empty binomial queue will take O(N) worst-case time. Hence the average time is constant.
DeleteMin
Step 1: FindMin in Bk
Tp=O(logN)
Step 2: Remove Bk from H
Tp=O(1)
Step 3: Remove root from Bk
Tp=O(logN)
Step 4: Merge(H′,H′′)
Tp=O(logN)
Implementation
Binomial queue = array of binomial trees
Operation
Property
Solution
DeleteMin
Find all the subtrees quickly
Left-child-next-sibling with linked lists
Merge
The children are ordered by their sizes
The next tree will be the largest. Hence maintain the subtrees in decreasing sizes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
typedefstructBinNode *Position; typedefstructCollection *BinQueue; typedefstructBinNode *BinTree;/*missing from p.176*/
structBinNode { ElementType Element; Position LeftChild; Position NextSibling; } ;
structCollection { int CurrentSize; /*total number of nodes*/ BinTree TheTrees[ MaxTrees ]; } ;
Combine trees
1 2 3 4 5 6 7 8 9 10
BinTree CombineTrees( BinTree T1, BinTree T2 )/*merge equal-sized T1 and T2*/ { if ( T1->Element > T2->Element ) /*attach the larger one to the smaller one*/ return CombineTrees( T2, T1 ); /*insert T2 to the front of the children list of T1*/ T2->NextSibling = T1->LeftChild; T1->LeftChild = T2; return T1; }
ElementType DeleteMin( BinQueue H ) { BinQueue DeletedQueue; Position DeletedTree, OldRoot; ElementType MinItem = Infinity; /*the minimum item to be returned*/ int i, j, MinTree; /*MinTree is the index of the tree with the minimum item*/
if ( IsEmpty(H) ) { PrintErrorMessage(); return –Infinity; }