Tree Traversal

Trees in Java:



Depth First Traversals:
(a) Inorder (Left, Root, Right) : 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
(b) Preorder (Root, Left, Right) : 8, 5, 9, 7, 1, 12, 2, 4, 11, 3
(c) Postorder (Left, Right, Root) :  9, 1, 2, 12, 7, 5, 3, 11, 4, 8



Algorithms:
In-Order:
Algorithm In-order(tree)
   1. Traverse the left subtree, i.e., call In-order(left-subtree)
   2. Visit the root.
   3. Traverse the right subtree, i.e., call In-order(right-subtree)

Pre-Order:
Algorithm Preorder(tree)
   1. Visit the root.
   2. Traverse the left subtree, i.e., call Preorder(left-subtree)
   3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Post-Order:
Algorithm Postorder(tree)
   1. Traverse the left subtree, i.e., call Postorder(left-subtree)
   2. Traverse the right subtree, i.e., call Postorder(right-subtree)
   3. Visit the root.

No comments:

Post a Comment