计算机科学外文翻译.docx
《计算机科学外文翻译.docx》由会员分享,可在线阅读,更多相关《计算机科学外文翻译.docx(11页珍藏版)》请在冰豆网上搜索。
计算机科学外文翻译
Binomialheap
Incomputerscience,abinomialheapisaheapsimilartoabinaryheapbutalsosupportsquickmergingoftwoheaps.Thisisachievedbyusingaspecialtreestructure.Itisimportantasanimplementationofthemergeableheapabstractdatatype(alsocalledmeldableheap),whichisapriorityqueuesupportingmergeoperation.
Binomialtree
Abinomialheapisimplementedasacollectionofbinomialtrees(comparewithabinaryheap,whichhasashapeofasinglebinarytree).Abinomialtreeisdefinedrecursively:
∙Abinomialtreeoforder0isasinglenode
∙Abinomialtreeoforderkhasarootnodewhosechildrenarerootsofbinomialtreesofordersk−1,k−2,...,2,1,0(inthisorder).
Binomialtreesoforder0to3:
Eachtreehasarootnodewithsubtreesofalllowerorderedbinomialtrees,whichhavebeenhighlighted.Forexample,theorder3binomialtreeisconnectedtoanorder2,1,and0(highlightedasblue,greenandredrespectively)binomialtree.
Abinomialtreeoforderkhas2knodes,heightk.
Becauseofitsuniquestructure,abinomialtreeoforderkcanbeconstructedfromtwotreesoforderk−1triviallybyattachingoneofthemastheleftmostchildofrootoftheotherone.Thisfeatureiscentraltothemergeoperationofabinomialheap,whichisitsmajoradvantageoverotherconventionalheaps.
Thenamecomesfromtheshape:
abinomialtreeoforder
has
nodesatdepth
.
Structureofabinomialheap
Abinomialheapisimplementedasasetofbinomialtreesthatsatisfythebinomialheapproperties:
∙Eachbinomialtreeinaheapobeystheminimum-heapproperty:
thekeyofanodeisgreaterthanorequaltothekeyofitsparent.
∙Therecanonlybeeitheroneorzerobinomialtreesforeachorder,includingzeroorder.
Thefirstpropertyensuresthattherootofeachbinomialtreecontainsthesmallestkeyinthetree,whichappliestotheentireheap.
Thesecondpropertyimpliesthatabinomialheapwithnnodesconsistsofatmostlogn+1binomialtrees.Infact,thenumberandordersofthesetreesareuniquelydeterminedbythenumberofnodesn:
eachbinomialtreecorrespondstoonedigitinthebinaryrepresentationofnumbern.Forexamplenumber13is1101inbinary,
andthusabinomialheapwith13nodeswillconsistofthreebinomialtreesoforders3,2,and0(seefigurebelow).
Exampleofabinomialheapcontaining13nodeswithdistinctkeys.
Theheapconsistsofthreebinomialtreeswithorders0,2,and3.
Implementation
Becausenooperationrequiresrandomaccesstotherootnodesofthebinomialtrees,therootsofthebinomialtreescanbestoredinalinkedlist,orderedbyincreasingorderofthetree.
Merge
Asmentionedabove,thesimplestandmostimportantoperationisthemergingoftwobinomialtreesofthesameorderwithintwobinomialheaps.Duetothestructureofbinomialtrees,theycanbemergedtrivially.Astheirrootnodeisthesmallestelementwithinthetree,bycomparingthetwokeys,thesmallerofthemistheminimumkey,andbecomesthenewrootnode.Thentheothertreebecomeasubtreeofthecombinedtree.Thisoperationisbasictothecompletemergingoftwobinomialheaps.
functionmergeTree(p,q)
ifp.root.key<=q.root.key
returnp.addSubTree(q)
else
returnq.addSubTree(p)
Tomergetwobinomialtreesofthesameorder,firstcomparetherootkey.Since7>3,theblacktreeontheleft(withrootnode7)isattachedtothegreytreeontheright(withrootnode3)asasubtree.Theresultisatreeoforder3.
Theoperationofmergingtwoheapsisperhapsthemostinterestingandcanbeusedasasubroutineinmostotheroperations.Thelistsofrootsofbothheapsaretraversedsimultaneously,similarlyasinthemergealgorithm
Ifonlyoneoftheheapscontainsatreeoforderj,thistreeismovedtothemergedheap.Ifbothheapscontainatreeoforderj,thetwotreesaremergedtoonetreeoforderj+1sothattheminimum-heappropertyissatisfied.Notethatitmaylaterbenecessarytomergethistreewithsomeothertreeoforderj+1presentinoneoftheheaps.Inthecourseofthealgorithm,weneedtoexamineatmostthreetreesofanyorder(twofromthetwoheapswemergeandonecomposedoftwosmallertrees).
Becauseeachbinomialtreeinabinomialheapcorrespondstoabitinthebinaryrepresentationofitssize,thereisananalogybetweenthemergingoftwoheapsandthebinaryadditionofthesizesofthetwoheaps,fromright-to-left.Wheneveracarryoccursduringaddition,thiscorrespondstoamergingoftwobinomialtreesduringthemerge.
EachtreehasorderatmostlognandthereforetherunningtimeisO(logn).
functionmerge(p,q)
whilenot(p.end()andq.end())
tree=mergeTree(p.currentTree(),q.currentTree())
ifnotheap.currentTree().empty()
tree=mergeTree(tree,heap.currentTree())
heap.