How to traverse through a binary tree

Contents

There are three methods for graph use traversing.

How to use IN-ORDER TRAVERSAL?

IN-ORDER TRAVERSAL L D R

  • Traverse the left subtree in Inorder Traversal
  • Process(visit) the Data node(root)
  • Traverse the Right subtree in Inorder Traversal
Inorder Traversal

Inorder traversal steps
Step-1: Start from node L, which means left of each node until the data not found, when we reached node A visit left subtree, then there is no other left subtree node combine with A, so we come with D means data node, we visited with Left(L) Data(D) now where the data is found we write that A and search R there is no Right subtree.

Step-2:  Step-1 is completed, after visited node A, there is no other Left or Right subtree, move back to the node, we reached slash(/) where L is visited from Left(L) Data(D) Right(R),
Visit the Data node D, then we found Data node, immediately right that node and go for traverse further nodes until the D not reached at data from LDR. Now, we reached B there no left or right subtree, we go with the arrow shown in step-2, now we right data B and traverse with R, there is no right subtree.

Step-3:  B node visited, that’s why? we go back to the node ^ as shown in the above figure, where L is visited but now D is visiting from LDR means to write the data ^ and go with the arrow sign shown in step-3 which is C.write the data C which is the highlight with the circle.
Step-4: After visited node C, take a similar process until all the data nodes have not reached. In the writing manner, we move with help of an arrow sign and write all the data highlighted with a circle. Now we reached E at the end

The algorithm used for in-order ?

FUNCTION
inorder(struct treenode *root)
{ if(root!=NULL)
inorder(root->left) { printf(“%d”, root->data)
inorder(root->right) } }

How to use PRE-ORDER TRAVERSAL?

2. PRE-ORDER TRAVERSAL  D L R

  • Process(visit) the Data node(root)
  • Traverse the left subtree in Preorder Traversal
  • Traverse the Right subtree in Preorder Traversal
preorder img
preorder traversal steps
preorder traversal steps
Step-1In Pre-order traversal, visited all D’s means to write all the data until there is no attached left or right subtree.we visited all the data nodes * / + A.
Step-2After reached at A go with the Left subtree of + which is already visited, now visit at the right subtree data node that is B, and come back with the arrow sign shown in step -2 figure visited at ^.
Step-3After visit node ^, we visit in the arrow direction there is a circle on the data node which we visited in that step which is D.

Which Algorithm used for pre-order ?

FUNCTION
preorder(struct treenode *root)
{ if(root!=NULL)
{ printf(“%d”,root->data)
preorder(root->left)
preorder(root->right) } }

How to use PRE-ORDER TRAVERSAL?

3.POST-ORDER TRAVERSAL L R D

  • Traverse the left subtree in Postorder Traversal
  • Traverse the Right subtree in Postorder Traversal
  •   Process(visit) the Data node(root)
Post order image
Post order traversal steps
Post order traversal steps
Step-1In Postorder traversal the data node is, at last, so start with the left subtree and traverse until the last left subtree, once we reached A which is the last node there is no left subtree for traversing means we traverse right and after that Data node.
Therefore, we got the data node now we write A, traverse back with arrow sign shows in fig. step-1 at + and go with right subtree, as you saw in step 1 we reached last node B.

Step-2: As shown in step 2, after reaching the B node we move back to the + it is the data node now we write that data and go with the arrows sign that shown in fig. step-2 we visited at data node C.

Step-3: step-2 completed, visited the C data node we move back with the arrow sign shown in step-3 there is a circle on the node / and D which is the data node in this step.
Step-4: We reach D need to move backside, because in post-order traversal the root node traverse at last, so we reached root node * in the final step.

What is the Algorithm used for post-order ?

FUNCTION
postorder(struct treenode *root)
{ if(root!=NULL)
postorder(root->left)
postorder(root->right)
{ printf(“%d”,root->data) } }

Important instruction for Traversing graph

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top