Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




TREE TRAVERSAL


We have already looked at the Binary Tree. Now, there should be a way to visit the Nodes of the Tree.


And Yes ! Visiting all the Nodes of the Tree in a specific order is 'Tree Traversal'.


There are three ways of visiting the Nodes of the Tree :


  1. Preorder Tree Traversal : In Preorder Tree Traversal, we visit the Root Node first, then visit all the Nodes of the Left Sub-Tree. And finally, visit all the Nodes of the Right Sub-Tree.

    So, Preorder Tree Traversal would be,

    Root --> Left --> Right
  2. Postorder Tree Traversal : In Postorder Tree Traversal, we visit the all the Nodes of the Left Sub-Tree first, then visit all the Nodes of the Right Sub-Tree. And finally, visit the Root Node.

    And, Postorder Tree traversal would be,

    Left --> Right --> Root
  3. Inorder Tree Traversal : In Inorder Tree Traversal, we visit all the Nodes in the Left Sub-Tree first, then we visit the Root Node. And finally, visit all the Nodes of the Right Sub-Tree.

    So, So, Inorder Tree Traversal would be,

    Left --> Root --> Right

Now, let us take the below Tree and understand how all the three ways of Tree Traversal works.


java_Collections

Preorder Traversal for the above Tree


So, Preorder Tree Traversal is,


Root --> Left --> Right

So, we would visit the Root element first, then visit all the elements of the Left Sub-Tree and finally, visit all the elements of the Right Sub-Tree.


Now, the Root element is Node 'A'.


The Right Sub-Tree is,


java_Collections

And the Left Sub-Tree is,


java_Collections

So, the first thing we will do is, print the root element(i.e. 'A') first. As the Preorder Traversal is 'Root --> Left --> Right'.


A

Then we come to the Left Sub-Tree as per the Preorder Traversal process. And if you look at the Left Sub-Tree,


java_Collections

'B' is the root of the Left Sub-Tree. So, we print 'B' next,


A   B

Then we come to the left Sub-Tree of 'B'.


java_Collections

And find 'D' is the root of the left Sub-Tree of 'B'. So, we print 'D'.


A   B  D

Then we try finding the left Sub-Tree of 'D'. But 'D' doesn't have any Left Sub-Tree. So, as per the traversal process 'Root --> Left --> Right', we will go to the Right Sub-Tree of 'D'. And find 'H' there.


So, we print 'H'.


A   B   D   H

Now, we find that 'H' doesn't have any child.


So, we travel up, and go to 'B'.


java_Collections

And we find that the left Sub-Tree of 'B' is already visited.


So, as per the traversal process 'Root --> Left --> Right', we will go to the Right Sub-Tree of 'B'. And find 'E' there.


So, we print 'E'.


A   B   D   H   E

Again we travel up and reach 'A'.


java_Collections

And we find that the Left Sub-Tree of 'A' is already visited. So, we visit the Right Sub-Tree of 'A'.


java_Collections

And find that 'C' is the root of the Right Sub-Tree.


So, we print 'C'.


A   B   D   H   E   C

And, as per the traversal process 'Root --> Left --> Right', we will go to the Left Sub-Tree of 'C'. And find 'F' there.


So, we print 'F'.


A   B   D   H   E   C   F

Then search for the Right Sub-Tree of 'C'. But we find it's empty.


And thus, all the Nodes of the Tree are visited and we complete the Pre-Order traversal with this.


And the final result for re-Order traversal is,


A   B   D   H   E   C   F

Next, let us see the Postorder traversal for the same tree.


java_Collections

Postorder Traversal for the above Tree


So, Postorder Tree Traversal is,


Left --> Right --> Root

So, we would visit all the elements of the Left Sub-Tree element first, then visit all the elements of the Right Sub-Tree and finally, visit the Root element.


Now, the Root element is Node 'A'.


And, as per the Postorder Traversal, 'Left --> Right --> Root'. We will visit the Left Sub-Tree.


And the Left Sub-Tree is,


java_Collections

Now, 'B' is the root of the Left Sub-Tree. And, as per the Postorder Traversal, 'Left --> Right --> Root'. We will visit the Left Sub-Tree of 'B'.


The Left Sub-Tree of 'B' is,


java_Collections

So, 'D' is the root of the Left Sub-Tree of 'B'. And, as per the Postorder Traversal, 'Left --> Right --> Root', we are supposed to visit the Left child of 'D'. But 'D' doesn't have any Left Child.


So, we visit the Right child of 'D'. Which is 'H'.


And we print 'H' first.


H

And since we have printed the Right element (i.e. H). Next, we print the root element (i.e. D) following the order 'Left --> Right --> Root'.


So, we print 'D'.


H   D

Now, we are done with the Left Sub-Tree of 'B',


java_Collections

So, it is time for us to travel upwards and reach 'B'.


java_Collections

Now, as per the Postorder Traversal, 'Left --> Right --> Root'. The Left Sub-Tree of 'B' is already visited. Now, it us time to go to the right element.


And find 'E' is the right element of 'B'.


And since, 'E' has no children. We print 'E' next.


H   D   E

Now, we are done with the right element of 'B'. So, we print 'B' next(As 'B'is the root).


H   D   E   B

So, we are done with all the left Sub-Tree of 'A'.


Now, it is time to deal with the right Sub-Tree of 'A'.


And the Right Sub-Tree of 'A' is,


java_Collections

So, 'C' is the root of the Right Sub-Tree of 'A'.


And, as per the Postorder Traversal, 'Left --> Right --> Root'. We come to the left element of 'C'. And find 'F' there.


So, we print 'F'.


H   D   E   B   F

Then try finding the right element of 'C'. But 'C' has no right child.


So, we print the root element i.e. 'C'.


H   D   E   B   F   C

And we are done with the right Sub-Tree of 'A'.


And since, we are done with the Left Subtree and Right Sub-Tree of 'A'. It's time to print 'A' and stop the execution.


H   D   E   B   F   C   A

Next, let us see the Inorder traversal for the same tree.


java_Collections

Inorder Traversal for the above Tree


So, Inorder Tree Traversal is,


Left --> Root --> Right

So, we would visit all the elements of the Left Sub-Tree element first, then visit the Root element and finally, visit all the elements of the Right Sub-Tree .


Now, the Root element is Node 'A'.


And, as per the Inorder Traversal, 'Left --> Root --> Right'. We will visit the Left Sub-Tree.


And the Left Sub-Tree is,


java_Collections

Now, 'B' is the root of the Left Sub-Tree. And, as per the Inorder Traversal, 'Left --> Root --> Right'. We will visit the Left Sub-Tree of 'B'.


The Left Sub-Tree of 'B' is,


java_Collections

So, 'D' is the root of the Left Sub-Tree of 'B'. And, as per the Inorder Traversal, 'Left --> Root --> Right', we are supposed to visit the Left child of 'D'. But 'D' doesn't have any Left Child.


So, we visit the root element i.e. 'D'.


And we print 'D' first.


D

And since, we have printed the Root element (i.e. D). Next, we print the Right element (i.e. H) following the order'Left --> Root --> Right'.


So, we print 'H'.


D   H

Now, we are done with the Left Sub-Tree of 'B',


java_Collections

So, it is time for us to travel upwards and reach 'B'.


java_Collections

Now, as per the Postorder Traversal, 'Left --> Root --> Right'. The Left Sub-Tree of 'B' is already visited. Now, it us time to print the Root element i.e. B.


D   H   B

Now, we are done with the root element i.e. 'B'. So, we print 'E' next(As 'E' is the only element in the right side of 'B').


D   H   B   E

So, we are done with all the left Sub-Tree of 'A'.


java_Collections

Now, it is time to print 'A'.


D   H   B   E   A

Now, it is time to deal with the Right Sub-Tree of 'A'.


And the right Sub-Tree of 'A' is,


java_Collections

So, 'C' is the root of the Right Sub-Tree of 'A'.


And, as per the Inorder Traversal, 'Left --> Root --> Right'. We come to the left element of 'C'. And find 'F' there.


So, we print 'F'.


D   H   B   E   A   F

Then we print the root element i.e. 'C'.


D   H   B   E   A   F   C

And we are done with the Inorder traversal of the above tree.