1 / 11

Binary Tree Traversal Techniques

Binary Tree Traversal Techniques. Three recursive techniques for binary tree traversal In each technique, the left subtree is traversed recursively, the right subtree is traversed recursively, and the root is visited

kita
Download Presentation

Binary Tree Traversal Techniques

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Tree Traversal Techniques • Three recursive techniques for binary tree traversal • In each technique, the left subtree is traversed recursively, the right subtree is traversed recursively, and the root is visited • What distinguishes the techniques from one another is the order of those 3 tasks CS 103

  2. Preorder Traversal CS 103

  3. 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Visit the root, Traverse the left subtree, Traverse the right subtree. CS 103

  4. Inorder Traversal CS 103

  5. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 •  (i) Traverse the left most subtree starting at the left external node, • (ii) Visit the root, and • (iii) Traverse the right subtree starting at the left external node. CS 103

  6. Postorder CS 103

  7. 0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7 • (i) Traverse all the left external nodes starting with the left most subtree • (ii) Traverse the right subtree starting at the left external node • (iii) • Visit the root. CS 103

  8. Preoder, Inorder, Postorder • Preorder Traversal: • Visit the root • Traverse left subtree • Traverse right subtree • In Preorder, the root is visited before (pre) the subtrees traversals • In Inorder, the root is visited in-between left and right subtree traversal • In Preorder, the root is visited after (pre) the subtrees traversals • Inorder Traversal: • Traverse left subtree • Visit the root • Traverse right subtree • Postorder Traversal: • Traverse left subtree • Traverse right subtree • Visit the root CS 103

  9. 1 3 7 10 5 8 9 4 6 11 12 Illustrations for Traversals Visit the root, Traverse the left subtree, Traverse the right Subtree • Preorder: 1 3 5 4 6 7 8 9 10 11 12 CS 103

  10. 1 3 7 10 5 8 9 4 6 11 12 Illustrations for Traversals Traverse the left most subtree starting at the left external node, Visit the root, and Traverse the right subtree starting at the left external node. • Inorder: 4 5 6 3 1 8 7 9 11 10 12 CS 103

  11. 1 3 7 10 5 8 9 4 6 11 12 Illustrations for Traversals Traverse all the left external nodes starting with the left most subtree Traverse the right subtree starting at the left external node Visit the root. • Postorder: 4 6 5 3 8 11 12 10 9 7 1 CS 103

More Related