Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order Breadth First search Level order
A binary tree (not a binary search tree) can also be used to represent an algebraic expression that involves the binary arithmetic operators +, -, /, and *. The root node holds an operator, and each of its subtrees represents either a variable name (like A, B, or C) or another expression. The following slides depicts the Tree traversals using Preorder Traversal Inorder Traversal Postorder Traversal
In Pre-order Traversal the order of traversing the nodes is as follows. Root Left sub-tree Right sub-tree Note : Dotted lines and Dotted Circles in the BST diagram represent NULL Nodes.
private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.
+ */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} preOrder( + ) Start the execution of preOrder function with localRoot as “ + “. Pushes the method call into stack.
+ */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} Check if localRoot value is null or not. preOrder( + )
+ */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + Printing the localRoot value using diplayNode() method. preOrder( + )
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. + preOrder( + ) preOrder ( *)
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ * “. + preOrder( + ) preOrder ( *)
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. + preOrder( + ) preOrder ( *)
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. + * preOrder( + ) preOrder ( *)
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ A “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( A ) preOrder ( * ) + *
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ A “. preOrder( + ) preOrder( A ) preOrder ( * ) + *
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( A ) preOrder ( * ) + *
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then it executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true, call returns to previous stage in the stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements at this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( A ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that stage as indicated here by arrow. preOrder( + ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ B “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ B “. preOrder( + ) preOrder( B ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( B ) preOrder ( * ) + * A
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true, call returns to previous stage in the stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in the previous stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder ( * ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in the previous stage also completed, call returns to the previous stage in the stack. preOrder( + ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that stage as indicated here by arrow. preOrder( + )
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ / “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( / ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ / “. preOrder( + ) preOrder( / ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( / ) + * A B
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( / ) + */ ABCD + * A B /
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ C “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ C “. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ D “. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( / ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) + * A B / C D
private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the stack is empty the call comes out of the algorithm. + * A B / C D
In Order Binary Tree In In-order Traversal the order of traversing the nodes is as follows. Left sub-tree Root Right sub-tree An In-order traversal is most commonly used in binary search tree because it visits all the nodes in ascending order, based on their key values. If you want to create a sorted list of the data in a binary tree, this is one way to do it.
private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.
private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } + */ ABCD Start the execution of inOrder function with localRoot as “ + “. Pushes the method call into stack. inOrder( + )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ * “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of preOrder function with localRoot as “ * “. inOrder( + ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ A “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder (A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ A “. inOrder( + ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( A ) inOrder( * )
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( A ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( A ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( * ) A
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ B “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ B “. inOrder( + ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( B ) inOrder( * ) A *
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( B ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( B ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) inOrder( * ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) A * B
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ / “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ / “. inOrder( + ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ C “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ C “. inOrder( + ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( C ) inOrder( / ) A * B +
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( C ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( / ) A * B + C
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ D “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ D “. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) inOrder( / ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) A * B + C / D
+ */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the stack is empty the call comes out of the algorithm. A * B + C / D
Post Order Binary Tree In Post-order Traversal the order of traversing the nodes is as follows. Left sub-tree Right sub-tree Root
private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.
private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD postOrder( + ) Start the execution of inOrder function with localRoot as “ + “. Pushes the method call into stack.
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ * “as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ * “. postOrder( + ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ A “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ A “. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ * “as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( A ) postOrder( * )
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( A ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ B“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ B “. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( B ) postOrder( * ) A
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( B ) postOrder( * ) A B
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( * ) A B
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( * ) A B
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( * ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ / “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ / “. postOrder( + ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ C“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ C “. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( C ) postOrder( / ) A B *
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( C ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ D“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ D “. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( D ) postOrder( / ) A B * C
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( D ) postOrder( / ) A B * C D
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( / ) A B * C D
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( / ) A B * C D
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( / ) A B * C D /
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) A B * C D /
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) A B * C D /
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) A B * C D / +
+ */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. A B * C D / +