A binomial queue is not a heap-ordered tree, but rather a collection of heap-ordered trees, known as a forest. Each heap-ordered tree is a binomial tree.
\(B_k\) consists of a root with \(k\) children, which are \(B_0\), \(B_1\), ..., \(B_{k-1}\). \(B_k\) has exactly \(2^k\) nodes. The number of nodes at depth \(d\) is \(C_k^d\).
typedefstructBinNode*Position;typedefstructCollection*BinQueue;typedefstructBinNode*BinTree;/* missing from p.176 */structBinNode{ElementTypeElement;PositionLeftChild;PositionNextSibling;};structCollection{intCurrentSize;/* total number of nodes */BinTreeTheTrees[MaxTrees];};BinTreeCombineTrees(BinTreeT1,BinTreeT2){/* merge equal-sized T1 and T2 */if(T1->Element>T2->Element)/* attach the larger one to the smaller one */returnCombineTrees(T2,T1);/* insert T2 to the front of the children list of T1 */T2->NextSibling=T1->LeftChild;T1->LeftChild=T2;returnT1;}// combine 操作时间复杂度为 O(1)BinQueueMerge(BinQueueH1,BinQueueH2){BinTreeT1,T2,Carry=NULL;inti,j;if(H1->CurrentSize+H2->CurrentSize>Capacity)ErrorMessage();H1->CurrentSize+=H2->CurrentSize;for(i=0,j=1;j<=H1->CurrentSize;i++,j*=2){T1=H1->TheTrees[i];T2=H2->TheTrees[i];/*current trees */switch(4*!!Carry+2*!!T2+!!T1){case0:/* 000 */;case1:/* 001 */break;case2:/* 010 */H1->TheTrees[i]=T2;H2->TheTrees[i]=NULL;break;case4:/* 100 */H1->TheTrees[i]=Carry;Carry=NULL;break;case3:/* 011 */Carry=CombineTrees(T1,T2);H1->TheTrees[i]=H2->TheTrees[i]=NULL;break;case5:/* 101 */Carry=CombineTrees(T1,Carry);H1->TheTrees[i]=NULL;break;case6:/* 110 */Carry=CombineTrees(T2,Carry);H2->TheTrees[i]=NULL;break;case7:/* 111 */H1->TheTrees[i]=Carry;Carry=CombineTrees(T1,T2);H2->TheTrees[i]=NULL;break;}/* end switch */}/* end for-loop */returnH1;}ElementTypeDeleteMin(BinQueueH){BinQueueDeletedQueue;PositionDeletedTree,OldRoot;ElementTypeMinItem=Infinity;/* the minimum item to be returned */inti,j,MinTree;/* MinTree is the index of the tree with the minimum item */if(IsEmpty(H)){PrintErrorMessage();return–Infinity;}for(i=0;i<MaxTrees;i++){/* Step 1: find the minimum item */if(H->TheTrees[i]&&H->TheTrees[i]->Element<MinItem){MinItem=H->TheTrees[i]->Element;MinTree=i;}/* end if */}/* end for-i-loop */DeletedTree=H->TheTrees[MinTree];H->TheTrees[MinTree]=NULL;/* Step 2: remove the MinTree from H => H’ */OldRoot=DeletedTree;/* Step 3.1: remove the root */DeletedTree=DeletedTree->LeftChild;free(OldRoot);DeletedQueue=Initialize();/* Step 3.2: create H” */DeletedQueue->CurrentSize=(1<<MinTree)–1;/* 2MinTree – 1 */for(j=MinTree–1;j>=0;j––){DeletedQueue->TheTrees[j]=DeletedTree;DeletedTree=DeletedTree->NextSibling;DeletedQueue->TheTrees[j]->NextSibling=NULL;}/* end for-j-loop */H->CurrentSize–=DeletedQueue->CurrentSize+1;H=Merge(H,DeletedQueue);/* Step 4: merge H’ and H” */returnMinItem;}