ece 242 data structures
play

ECE 242 Data Structures Lecture 19 Tree Traversal October 23, - PDF document

ECE 242 Data Structures Lecture 19 Tree Traversal October 23, 2009 ECE242 L19: Tree Traversal Overview Problem: How do we access the data located in trees Well-defined approaches to access data in an orderly fashion Trees can be


  1. ECE 242 Data Structures Lecture 19 Tree Traversal October 23, 2009 ECE242 L19: Tree Traversal Overview ° Problem: How do we access the data located in trees ° Well-defined approaches to access data in an orderly fashion ° Trees can be implemented in either arrays or with linked structures • The traversal approaches are implementation independent ° Recursive patterns can be developed and used October 23, 2009 ECE242 L19: Tree Traversal

  2. Tree Traversal ° Four meaningful orders in which to traverse a binary tree. • Preorder • Inorder • Postorder • Level order October 23, 2009 ECE242 L19: Tree Traversal Preorder traversal ° Preorder traversal is accomplished by visiting each node, followed by its children, starting with the root ° Given the complete binary tree on the next slide, a preorder traversal would produce the order: A B D E C ° Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is: Visit node Traverse(left child) Traverse(right child) October 23, 2009 ECE242 L19: Tree Traversal

  3. A complete tree October 23, 2009 ECE242 L19: Tree Traversal Preorder Traversal Note that this code is recursive October 23, 2009 ECE242 L19: Tree Traversal

  4. Preorder Traversal Without Recursion October 23, 2009 ECE242 L19: Tree Traversal Inorder traversal ° Inorder traversal is accomplished by visiting the left child of the node, then the node, then any remaining child nodes starting with the root ° An inorder traversal of the previous tree produces the order: D B E A C ° Stated in pseudocode, the algorithm for an inorder traversal of a binary tree is: Traverse(left child) Visit node Traverse(right child) October 23, 2009 ECE242 L19: Tree Traversal

  5. Inorder Traversal Print left tree, node, right tree October 23, 2009 ECE242 L19: Tree Traversal Postorder traversal ° Postorder traversal is accomplished by visiting the children, then the node starting with the root ° Given the same tree, a postorder traversal produces the following order: D E B C A • Stated in pseudocode, the algorithm for a postorder traversal of a binary tree is: Traverse(left child) Traverse(right child) Visit node October 23, 2009 ECE242 L19: Tree Traversal

  6. Postorder Traversal Note the recursive calls October 23, 2009 ECE242 L19: Tree Traversal Levelorder traversal ° Levelorder traversal is accomplished by visiting all of the nodes at each level, one level at at time, starting with the root ° Given the same tree, a levelorder traversal produces the order: A B C D E October 23, 2009 ECE242 L19: Tree Traversal

  7. Levelorder traversal • Stated in pseudocode, the algorithm for a level order traversal of a binary tree is: October 23, 2009 ECE242 L19: Tree Traversal Level Order Traversal October 23, 2009 ECE242 L19: Tree Traversal

  8. Tree Traversal Complexity ° Level order traversal is sometimes called breadth- first . ° The other traversals are called depth-first . ° Traversal takes O (n) in both breadth-first and depth-first. ° Memory usage in a perfect tree is O (log n) in depth-first and O (n) in breadth-first traversal. October 23, 2009 ECE242 L19: Tree Traversal Tree Traversal October 23, 2009 ECE242 L19: Tree Traversal

  9. Breadth-First vs. Depth-First Traversal Breadth-first Depth first October 23, 2009 ECE242 L19: Tree Traversal Summary ° Trees can be accessed in many different ways • Often the application dictates the implementation ° Depth-first and breadth-first access are popular ° Postorder and Preorder traversals are often recursive ° Recursion can be eliminated to make the methods iterative October 23, 2009 ECE242 L19: Tree Traversal

Recommend


More recommend