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.
递归定义
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, \(B_{k–1}\), to the root of another binomial tree, \(B_{k–1}\).
性质
Bk consists of a root with \(k\) children, which are \(B_0, B_1, B_2, \cdots , B_{k-1}\). \(B_k\) has exactly \(2^k\) nodes. The number of nodes at depth \(d\) is
\(\binom{k}{d}\).
A priority queue of any size can be uniquely represented by a collection of binomial trees. 因为每个数都可以转换成唯一的二进制数。
The new tree will be the largest. Hence maintain the subtrees in decreasing sizes.
C
1 2 3 4 5 6 7 8 910111213141516
typedefstructBinNode*Position;typedefstructCollection*BinQueue;typedefstructBinNode*BinTree;/* missing from p.176 */structBinNode{ElementTypeElement;PositionLeftChild;PositionNextSibling;};structCollection{intCurrentSize;/* total number of nodes */BinTreeTheTrees[MaxTrees];};
C
1 2 3 4 5 6 7 8 910
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;}
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;}