Professional Documents
Culture Documents
BinaryTreeOverview
FormalDefinitionofaBinaryTree
Abinarytreeconsistsofafinitesetofnodesthatiseitherempty,orconsistsofonespeciallydesignated
nodecalledtherootofthebinarytree,andtheelementsoftwodisjointbinarytreescalledtheleftsubtreeand
rightsubtreeoftheroot.
Notethatthedefinitionaboveisrecursive:wehavedefinedabinarytreeintermsofbinarytrees.Thisis
appropriatesincerecursionisaninnatecharacteristicoftreestructures.
Diagram1:Abinarytree
BinaryTreeTerminology
Treeterminologyisgenerallyderivedfromtheterminologyoffamilytrees(specifically,thetypeoffamily
treecalledalinealchart).
Eachrootissaidtobetheparentoftherootsofitssubtrees.
Twonodeswiththesameparentaresaidtobesiblingstheyarethechildrenoftheirparent.
Therootnodehasnoparent.
Agreatdealoftreeprocessingtakesadvantageoftherelationshipbetweenaparentanditschildren,
andwecommonlysayadirectededge(orsimplyanedge)extendsfromaparenttoitschildren.Thus
edgesconnectarootwiththerootsofeachsubtree.Anundirectededgeextendsinbothdirections
betweenaparentandachild.
Grandparentandgrandchildrelationscanbedefinedinasimilarmannerwecouldalsoextendthis
terminologyfurtherifwewished(designatingnodesascousins,asanuncleoraunt,etc.).
OtherTreeTerms
Thenumberofsubtreesofanodeiscalledthedegreeofthenode.Inabinarytree,allnodeshave
degree0,1,or2.
Anodeofdegreezeroiscalledaterminalnodeorleafnode.
Anonleafnodeisoftencalledabranchnode.
Thedegreeofatreeisthemaximumdegreeofanodeinthetree.Abinarytreeisdegree2.
Adirectedpathfromnoden1 tonkisdefinedasasequenceofnodesn1 ,n2 ,...,nksuchthatniisthe
parentofni+1 for1<=i<k.Anundirectedpathisasimilarsequenceofundirectededges.Thelength
ofthispathisthenumberofedgesonthepath,namelyk1(i.e.,thenumberofnodes1).Thereisa
pathoflengthzerofromeverynodetoitself.Noticethatinabinarytreethereisexactlyonepathfrom
theroottoeachnode.
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/bintree.html 1/4
5/5/2017 BinaryTreeOverview
Thelevelordepthofanodewithrespecttoatreeisdefinedrecursively:theleveloftherootiszero
andthelevelofanyothernodeisonehigherthanthatofitsparent.Ortoputitanotherway,thelevel
ordepthofanodeniisthelengthoftheuniquepathfromtheroottoni.
Theheightofniisthelengthofthelongestpathfromnitoaleaf.Thusallleavesinthetreeareat
height0.
Theheightofatreeisequaltotheheightoftheroot.Thedepthofatreeisequaltothelevelordepth
ofthedeepestleafthisisalwaysequaltotheheightofthetree.
Ifthereisadirectedpathfromn1 ton2 ,thenn1 isanancestorofn2 andn2 isadescendantofn1 .
SpecialFormsofBinaryTrees
Thereareafewspecialformsofbinarytreeworthmentioning.
Ifeverynonleafnodeinabinarytreehasnonemptyleftandrightsubtrees,thetreeistermedastrictly
binarytree.Or,toputitanotherway,allofthenodesinastrictlybinarytreeareofdegreezeroortwo,never
degreeone.AstrictlybinarytreewithNleavesalwayscontains2N1nodes.
Sometextscallthisa"full"binarytree.
Acompletebinarytreeofdepthdisthestrictlybinarytreeallofwhoseleavesareatleveld.
Thetotalnumberofnodesinacompletebinarytreeofdepthdequals2d+1 1.Sinceallleavesinsuchatree
areatleveld,thetreecontains2d leavesand,therefore,2d 1internalnodes.
Diagram2:Acompletebinarytree
Abinarytreeofdepthdisanalmostcompletebinarytreeif:
Eachleafinthetreeiseitheratleveldoratleveld1.
Foranynodend inthetreewitharightdescendantatleveld,alltheleftdescendantsofnd thatare
leavesarealsoatleveld.
Diagram3:Analmostcompletebinarytree
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/bintree.html 2/4
5/5/2017 BinaryTreeOverview
AnalmostcompletestrictlybinarytreewithNleaveshas2N1nodes(asdoesanyotherstrictlybinary
tree).AnalmostcompletebinarytreewithNleavesthatisnotstrictlybinaryhas2Nnodes.Therearetwo
distinctalmostcompletebinarytreeswithNleaves,oneofwhichisstrictlybinaryandoneofwhichisnot.
ThereisonlyasinglealmostcompletebinarytreewithNnodes.ThistreeisstrictlybinaryifandonlyifNis
odd.
RepresentingBinaryTreesinMemory
ArrayRepresentation
Foracompleteoralmostcompletebinarytree,storingthebinarytreeasanarraymaybeagoodchoice.
Onewaytodothisistostoretherootofthetreeinthefirstelementofthearray.Then,foreachnodeinthe
treethatisstoredatsubscriptk,thenode'sleftchildcanbestoredatsubscript2k+1andtherightchildcanbe
storedatsubscript2k+2.Forexample,thealmostcompletebinarytreeshowninDiagram2canbestoredin
anarraylikeso:
However,ifthisschemeisusedtostoreabinarytreethatisnotcompleteoralmostcomplete,wecanendup
withagreatdealofwastedspaceinthearray.
Forexample,thefollowingbinarytree
wouldbestoredusingthistechinquelikeso:
LinkedRepresentation
Ifabinarytreeisnotcompleteoralmostcomplete,abetterchoiceforstoringitistousealinked
representationsimilartothelinkedliststructurescoveredearlierinthesemester:
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/bintree.html 3/4
5/5/2017 BinaryTreeOverview
Eachtreenodehastwopointers(usuallynamedleftandright).Thetreeclasshasapointertotherootnode
ofthetree(labeledrootinthediagramabove).
AnypointerinthetreestructurethatdoesnotpointtoanodewillnormallycontainthevalueNULL.Alinked
treewithNnodeswillalwayscontainN+1nulllinks.
http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/bintree.html 4/4