Traversing a tree means visiting each node in a specified order. There are different ways to...
-
Upload
jemimah-baker -
Category
Documents
-
view
223 -
download
0
description
Transcript of Traversing a tree means visiting each node in a specified order. There are different ways to...
BINARY TREE TRAVERSAL
TRAVERSING A BINARY TREE• Traversing a tree means visiting each node in a specified
order. • There are different ways to traverse a tree.
BINARY TREE• 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
PRE-ORDER BINARY TREE• 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();
} }
+
* /
A B C D
POST-ORDER
• Consider this tree for traversal and follow the algorithm on the right side for traversal.
+
* /
A B C D
PRE-ORDER
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.
+
* /
A B C D
PRE-ORDER
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( + )
+
* /
A B C D
private void preOrder(node localRoot){
if(localRoot != null) {
localRoot.displayNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}}
+
PRE-ORDER
• Printing the localRoot value using diplayNode() method.
preOrder( + )
private void preOrder(node localRoot){
if(localRoot != null) {
localRoot.displayNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• Printing the localRoot value using diplayNode () method.
preOrder( + )preOrder( / )
+
* /
A B C D
+ * A B /
private void preOrder(node localRoot){
if(localRoot != null) {
localRoot.displayNode();
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER • 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
•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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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);
}}
+
* /
A B C D
PRE-ORDER
• 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();
} }
+
* /
A B C D
POST-ORDER
• 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);
} } +
* /
A B C D
IN-ORDER
• Start the execution of inOrder function with localRoot as “ + “.• Pushes the method call into stack.
inOrder( + )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ * “ as the localRoot.• Pushes the method call into stack.
inOrder( + )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of preOrder function with localRoot as “ * “.
inOrder( + )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ A “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder (A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ A “.
inOrder( + )
inOrder( A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER • 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( * )
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )
inOrder( A )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( A )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( A )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + )inOrder( * )
A
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ B “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ B “.
inOrder( + )
inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + )
inOrder( B )inOrder( * )
A *
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )
inOrder( B )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( B )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
•As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( B )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
inOrder( + )inOrder( * )
A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + ) A * B
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + ) A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ / “ as the localRoot.• Pushes the method call into stack.
inOrder( + )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ / “.
inOrder( + )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ C “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ C “.
inOrder( + )
inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
Calling the inOrder function with “ “ as the localRoot.Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + )
inOrder( C )inOrder( / )
A * B +
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )
inOrder( C )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
•Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( C )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( C )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + )inOrder( / )
A * B + C
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ D “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ D “.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C /
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Printing the localRoot value using diplayNode () method.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Calling the inOrder function with “ “ as the localRoot.• Pushes the method call into stack.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Start the execution of inOrder function with localRoot as “ “.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• Check if localRoot value is null or not.
inOrder( + )
inOrder( )inOrder( D )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
inOrder( + )
inOrder( D )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
•As all the statements in this stage also completed, call returns to the previous stage in the stack.
inOrder( + )inOrder( / )
A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
•As all the statements in this stage also completed, call returns to the previous stage in the stack.
inOrder( + ) A * B + C / D
+
* /
A B C D
private void inOrder(node localRoot){
if(localRoot != null){
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
} }
IN-ORDER
• 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();
} }
+
* /
A B C D
POST-ORDER
• 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();
} }
+
* /
A B C D
POST-ORDER
postOrder( + )
• Start the execution of inOrder function with localRoot as “ + “.• Pushes the method call into stack.
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ * “as the localRoot.• Pushes the method call into stack.
postOrder( + )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ * “.
postOrder( + )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ A “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ A “.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ * “as the localRoot. • Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( A )postOrder( * )
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )
postOrder( A )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ B“ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ B “.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( B )postOrder( * )
A
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )
postOrder( B )postOrder( * )
A B
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
•As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + )postOrder( * )
A B
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )postOrder( * )
A B
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )postOrder( * )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + ) A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + ) A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ / “ as the localRoot.• Pushes the method call into stack.
postOrder( + )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ / “.
postOrder( + )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ C“ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ C “.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
•Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( C )postOrder( / )
A B *
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )
postOrder( C )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ D“ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ D “.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Calling the postOrder function with “ “ as the localRoot.• Pushes the method call into stack.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Start the execution of postOrder function with localRoot as “ “.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Check if localRoot value is null or not.
postOrder( + )
postOrder( )postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As the results evaluates to true call returns to previous stage in the stack.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )
postOrder( D )postOrder( / )
A B * C
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )
postOrder( D )postOrder( / )
A B * C D
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + )postOrder( / )
A B * C D
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + )postOrder( / )
A B * C D
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + )postOrder( / )
A B * C D /
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
•As all the statements in this stage also completed, call returns to the previous stage in the stack.
postOrder( + ) A B * C D /
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Then executes remaining statements in that method call as indicated here by arrow.
postOrder( + ) A B * C D /
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• Printing the localRoot value using diplayNode () method.
postOrder( + ) A B * C D / +
+
* /
A B C D
private void postOrder(node localRoot){
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
localRoot.displayNode();
} }
POST-ORDER
• As all the statements in this stage also completed, call returns to the previous stage in the stack.
A B * C D / +
THANK YOU