Binary Tree Traversals

Binary Tree Traversals

 

Tree Traversals: The process of visiting each node in a tree data structure exactly once is called Traversal. Trees can be traversed either depth first or breadth first.

 

There are 3 types of traversals in binary trees.

  • Pre-order
  • Post-order
  • In-order.

 

Pre-order traversal :

First check if the root node is empty and then display the data and traverse left subtree  and then right subtree by calling pre-order function. In the following diagram the pre-order is represented in blue where the traversal is started by root node A.

 

In-Order traversal:

First the current node is checked for null and then traversed to the left subtree by calling in-order function and display the data then traverse the right subtree by recursively calling in-order function. In the following diagram the In-order traversal  is represented by green where the root node is in between both subtrees.

 

Post-Order traversal:

First the current node is checked for null and traversed from left subtree by calling Post-Order traversal function and followed by right subtree and then display the data. In the following diagram  Post-Order traversal is represented by orange color where the root node is at the end.

 

The path of traversals In Binary Tree Data

Try here>>>