Tree Traversal
- Rooted at one vertex.
- Tree contains no cycle.
- Elements are arranged in layers.
- A unique path travers from root to any node.
Tree Traversals
(Inorder, Preorder and Postorder)
Unlikelinear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways. Following are the generally used ways for traversing trees.
Depth First Traversals:
Algorithm Inorder(tree)
1.
Traverse the left subtree.
2. Visit
the root.
3.
Traverse the right subtree.
Uses of Inorder :
Algorithm Preorder(tree):
1. Visit
the root.
2.
Traverse the left subtree.
3.
Traverse the right subtree.
Uses of Preorder
Preorder traversal is
used to create a copy of the tree. Preorder traversal is also used to get
prefix expression on of an expression tree.
Example: Preorder
traversal for the above given figure is 1 2 4 5 3.
Postorder Traversal : Postorder (Left, Right, Root)
Algorithm Postorder(tree):
1.
Traverse the left subtree.
2.
Traverse the right subtree.
3. Visit
the root.

Comments
Post a Comment