Traversing a tree means visiting each node in a specified order. There are different ways to...

227
BINARY TREE TRAVERSAL

description

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

Transcript of Traversing a tree means visiting each node in a specified order. There are different ways to...

Page 1: 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.

BINARY TREE TRAVERSAL

Page 2: 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.

TRAVERSING A BINARY TREE• Traversing a tree means visiting each node in a specified

order. • There are different ways to traverse a tree.

Page 3: 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.

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

Page 4: 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.

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.

Page 5: 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.

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.

Page 6: 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.

+

* /

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.

Page 7: 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.

+

* /

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( + )

Page 8: 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.

+

* /

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( + )

Page 9: 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.

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( *)

Page 10: 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.

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( *)

Page 11: 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.

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( *)

Page 12: 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.

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( *)

Page 13: 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.

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( * )

+ *

Page 14: 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.

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( * )

+ *

Page 15: 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.

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( * )

+ *

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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

Page 25: 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.

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

Page 26: 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.

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

Page 27: 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.

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

Page 28: 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.

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

Page 29: 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.

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

Page 30: 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.

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

Page 31: 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.

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

Page 32: 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.

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

Page 33: 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.

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

Page 34: 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.

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

Page 35: 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.

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

Page 36: 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.

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

Page 37: 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.

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

Page 38: 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.

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

Page 39: 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.

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

Page 40: 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.

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( + )

Page 41: 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.

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

Page 42: 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.

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

Page 43: 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.

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

Page 44: 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.

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 /

Page 45: 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.

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 /

Page 46: 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.

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 /

Page 47: 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.

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 /

Page 48: 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.

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

Page 49: 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.

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

Page 50: 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.

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

Page 51: 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.

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

Page 52: 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.

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

Page 53: 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.

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

Page 54: 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.

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

Page 55: 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.

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

Page 56: 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.

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

Page 57: 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.

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

Page 58: 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.

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

Page 59: 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.

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

Page 60: 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.

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

Page 61: 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.

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

Page 62: 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.

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

Page 63: 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.

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

Page 64: 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.

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

Page 65: 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.

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

Page 66: 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.

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

Page 67: 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.

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

Page 68: 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.

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

Page 69: 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.

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

Page 70: 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.

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

Page 71: 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.

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

Page 72: 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.

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

Page 73: 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.

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

Page 74: 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.

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.

Page 75: 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.

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.

Page 76: 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.

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( + )

Page 77: 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.

+

* /

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( + )

Page 78: 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.

+

* /

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( * )

Page 79: 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.

+

* /

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( * )

Page 80: 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.

+

* /

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( * )

Page 81: 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.

+

* /

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( * )

Page 82: 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.

+

* /

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( * )

Page 83: 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.

+

* /

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( * )

Page 84: 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.

+

* /

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( * )

Page 85: 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.

+

* /

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( * )

Page 86: 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.

+

* /

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( * )

Page 87: 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.

+

* /

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( * )

Page 88: 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.

+

* /

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

Page 89: 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.

+

* /

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

Page 90: 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.

+

* /

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

Page 91: 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.

+

* /

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

Page 92: 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.

+

* /

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

Page 93: 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.

+

* /

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

Page 94: 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.

+

* /

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 *

Page 95: 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.

+

* /

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 *

Page 96: 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.

+

* /

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 *

Page 97: 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.

+

* /

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 *

Page 98: 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.

+

* /

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 *

Page 99: 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.

+

* /

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 *

Page 100: 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.

+

* /

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 *

Page 101: 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.

+

* /

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 *

Page 102: 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.

+

* /

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 *

Page 103: 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.

+

* /

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

Page 104: 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.

+

* /

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

Page 105: 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.

+

* /

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

Page 106: 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.

+

* /

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

Page 107: 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.

+

* /

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

Page 108: 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.

+

* /

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

Page 109: 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.

+

* /

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

Page 110: 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.

+

* /

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 +

Page 111: 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.

+

* /

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 +

Page 112: 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.

+

* /

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 +

Page 113: 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.

+

* /

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 +

Page 114: 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.

+

* /

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 +

Page 115: 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.

+

* /

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 +

Page 116: 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.

+

* /

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 +

Page 117: 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.

+

* /

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 +

Page 118: 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.

+

* /

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 +

Page 119: 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.

+

* /

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 +

Page 120: 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.

+

* /

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 +

Page 121: 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.

+

* /

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 +

Page 122: 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.

+

* /

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

Page 123: 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.

+

* /

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

Page 124: 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.

+

* /

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

Page 125: 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.

+

* /

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

Page 126: 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.

+

* /

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

Page 127: 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.

+

* /

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

Page 128: 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.

+

* /

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 /

Page 129: 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.

+

* /

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 /

Page 130: 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.

+

* /

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 /

Page 131: 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.

+

* /

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 /

Page 132: 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.

+

* /

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 /

Page 133: 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.

+

* /

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 /

Page 134: 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.

+

* /

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 /

Page 135: 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.

+

* /

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 /

Page 136: 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.

+

* /

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 /

Page 137: 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.

+

* /

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

Page 138: 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.

+

* /

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

Page 139: 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.

+

* /

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

Page 140: 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.

+

* /

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

Page 141: 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.

+

* /

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

Page 142: 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.

+

* /

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

Page 143: 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.

+

* /

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

Page 144: 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.

+

* /

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

Page 145: 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.

Post Order Binary Tree

• In Post-order Traversal the order of traversing the nodes is as follows.

Left sub-tree Right sub-tree Root

Page 146: 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.

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.

Page 147: 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.

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.

Page 148: 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.

+

* /

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( + )

Page 149: 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.

+

* /

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( * )

Page 150: 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.

+

* /

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( * )

Page 151: 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.

+

* /

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( * )

Page 152: 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.

+

* /

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( * )

Page 153: 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.

+

* /

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( * )

Page 154: 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.

+

* /

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( * )

Page 155: 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.

+

* /

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( * )

Page 156: 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.

+

* /

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( * )

Page 157: 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.

+

* /

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( * )

Page 158: 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.

+

* /

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( * )

Page 159: 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.

+

* /

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( * )

Page 160: 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.

+

* /

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( * )

Page 161: 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.

+

* /

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( * )

Page 162: 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.

+

* /

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( * )

Page 163: 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.

+

* /

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( * )

Page 164: 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.

+

* /

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( * )

Page 165: 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.

+

* /

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

Page 166: 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.

+

* /

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

Page 167: 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.

+

* /

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

Page 168: 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.

+

* /

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

Page 169: 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.

+

* /

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

Page 170: 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.

+

* /

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

Page 171: 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.

+

* /

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

Page 172: 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.

+

* /

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

Page 173: 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.

+

* /

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

Page 174: 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.

+

* /

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

Page 175: 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.

+

* /

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

Page 176: 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.

+

* /

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

Page 177: 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.

+

* /

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

Page 178: 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.

+

* /

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

Page 179: 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.

+

* /

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

Page 180: 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.

+

* /

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

Page 181: 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.

+

* /

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

Page 182: 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.

+

* /

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

Page 183: 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.

+

* /

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

Page 184: 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.

+

* /

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 *

Page 185: 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.

+

* /

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 *

Page 186: 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.

+

* /

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 *

Page 187: 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.

+

* /

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 *

Page 188: 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.

+

* /

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 *

Page 189: 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.

+

* /

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 *

Page 190: 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.

+

* /

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 *

Page 191: 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.

+

* /

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 *

Page 192: 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.

+

* /

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 *

Page 193: 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.

+

* /

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 *

Page 194: 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.

+

* /

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 *

Page 195: 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.

+

* /

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 *

Page 196: 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.

+

* /

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 *

Page 197: 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.

+

* /

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 *

Page 198: 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.

+

* /

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 *

Page 199: 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.

+

* /

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 *

Page 200: 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.

+

* /

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 *

Page 201: 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.

+

* /

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 *

Page 202: 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.

+

* /

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 *

Page 203: 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.

+

* /

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

Page 204: 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.

+

* /

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

Page 205: 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.

+

* /

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

Page 206: 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.

+

* /

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

Page 207: 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.

+

* /

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

Page 208: 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.

+

* /

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

Page 209: 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.

+

* /

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

Page 210: 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.

+

* /

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

Page 211: 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.

+

* /

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

Page 212: 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.

+

* /

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

Page 213: 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.

+

* /

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

Page 214: 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.

+

* /

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

Page 215: 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.

+

* /

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

Page 216: 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.

+

* /

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

Page 217: 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.

+

* /

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

Page 218: 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.

+

* /

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

Page 219: 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.

+

* /

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

Page 220: 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.

+

* /

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

Page 221: 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.

+

* /

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

Page 222: 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.

+

* /

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 /

Page 223: 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.

+

* /

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 /

Page 224: 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.

+

* /

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 /

Page 225: 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.

+

* /

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 / +

Page 226: 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.

+

* /

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 / +

Page 227: 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.

THANK YOU