DS-Tree Traversal BSC CS III Sem
DS-Tree Traversal BSC CS III Sem
Contents
• Tree Traversal
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
• Applications of Binary Trees
Tree Traversal
• Tree Traversal refers to the process of visiting
each node in a tree data structure exactly
once.
Depth First Traversal
Following three traversal techniques fall under
Depth First Traversal-
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
1. Preorder Traversal
Algorithm-
• Visit the root
• Traverse the left sub tree i.e. call Preorder (left
sub tree)
• Traverse the right sub tree i.e. call Preorder
(right sub tree)
• Root → Left → Right
Example
• Applications-
• Preorder traversal is used to get prefix
expression of an expression tree.
• Preorder traversal is used to create a copy of
the tree.
2. Inorder Traversal
• Algorithm-
• Traverse the left sub tree i.e. call Inorder (left
sub tree)
• Visit the root
• Traverse the right sub tree i.e. call Inorder
(right sub tree)
• Left → Root → Right
Example-
• Application-
• Inorder traversal is used to get infix expression
of an expression tree.
3. Postorder Traversal-
• Algorithm-
• Traverse the left sub tree i.e. call Postorder (left
sub tree)
• Traverse the right sub tree i.e. call Postorder
(right sub tree)
• Visit the root