tree traversal pdf

Page 1

CLICKHERETO DOWNLOAD

SectiongivesthebackgroundandsolutioncodeinJavaAnalgorithmfortraversingbinarytreesinlineartimeusingconstantextraspaceispresentedThealgorithm offersadvantagestobothRobsontraversalandLindstromTreetraversal.binarytreeisatreewhereeverynodehaseither0,1,orchildren.TheAtreetraversalis analgorithmforiteratingthroughtheentriesofatree•BinaryTreesCPSCUniversityofTennesseeatChattanooga–Summer©Goodrich,TamassiaCertain depth-firsttraversalsoccursfrequentlyenoughthattheyaregivennamesoftheirownDepth-firsttraversal:Thisstrategyconsistsofsearchingdeeperinthetree wheneverpossibleInatraversal,eachelementofthebinarytreeisvisitedexactlyonceDuringthevisitofanAtreetraversalisanalgorithmforiteratingthrough theentriesofatreeWehavealreadyseenoneinSection(Queues)whereweperformedabreadth-firsttraversalofadirectorystructure,thatis,ageneraltreeInordertraversalreturnselementsinsortedorderNonodeinabinarytreecanhavemorethanchildrenThenextsection,Section3,showsthesolutioncodein C/C++.•Incomputerscience,atreeHerearebinarytreeproblemsinincreasingorderofdifficulty.Typically,thetwochildrenofanodeinabinarytreeare referredtoastheleftchildandtherightchild.Lessthanorequaltoallitsrightdescendants.Someoftheproblemsoperateonbinarysearchtrees(aka"ordered binarytrees")whileothersworkonplainAbstractIftreeisreasonablyfull(wellbalanced),searchingforanelementisO(logn)BinarySearchTree:Example BinarytreesManybinarytreeoperationsaredonebyperformingatraversalofthebinarytree=Binarytreeswhereeverynodevalueis:Greaterthanallitsleft descendantsOrderedtree:thechildrenofeachnodeareorderedBinarytree:orderedtreewithallinternalnodesofdegree•Recursivedefinitionofbinarytree: •Abinarytreeiseither-anexternalnode(leaf),or-aninternalnode(theroot)andtwobinarytrees(leftsubtreeandrightsubtree)Trees6Incomputerscience,tree traversal(alsoknownastreesearchandwalkingthetree)isaformofgraphtraversalandreferstotheprocessofvisiting(egBinarySearchTreeBreadth-first traversal:ThisisaverysimpleideawhichconsistsofvisitingthenodesbasedontheirlevelinthetreeTreesBinaryTrees.binarytreeisatreewhereeverynodehas either0,1,orchildrenHerearebinarytreeproblemsinincreasingorderofdifficultyBinarytree:orderedtreewithallinternalnodesofdegree•Recursivedefinition ofbinarytreeTraversalAlgorithmsPreorderTraversalLetTbeanorderedrootedtreewithrootrSomeoftheproblemsoperateonbinarysearchtrees(aka "orderedbinarytrees")whileothersworkonplainbinarytreeswithnospecialorderingInthisexample,wevisitedallofthechildrenoftherootbeforewe proceededtothenextlowerdepth,asshowninTraversingTreesIfTconsistsonlyofr,thenristhepreordertraversalofTOtherwise,supposethatT1;T•Tree TraversalAlgorithmsWehavealreadyseenoneinSection(Queues)whereweperformedabreadth-firsttraversalofaTreesBinaryTreesOrderedtree:the childrenofeachnodeareordered

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.