Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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 Breadth First search Level order

3 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

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

5 private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.

6 + */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} preOrder( + ) Start the execution of preOrder function with localRoot as “ + “. Pushes the method call into stack.

7 + */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} Check if localRoot value is null or not. preOrder( + )

8 + */ ABCD private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + Printing the localRoot value using diplayNode() method. preOrder( + )

9 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. + preOrder( + ) preOrder ( *)

10 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ * “. + preOrder( + ) preOrder ( *)

11 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. + preOrder( + ) preOrder ( *)

12 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. + * preOrder( + ) preOrder ( *)

13 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ A “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( A ) preOrder ( * ) + *

14 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ A “. preOrder( + ) preOrder( A ) preOrder ( * ) + *

15 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( A ) preOrder ( * ) + *

16 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( A ) preOrder ( * ) + * A

17 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

18 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

19 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

20 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then it executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( A ) preOrder ( * ) + * A

21 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

22 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

23 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true, call returns to previous stage in the stack. preOrder( + ) preOrder( ) preOrder( A ) preOrder ( * ) + * A

24 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements at this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( A ) preOrder ( * ) + * A

25 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that stage as indicated here by arrow. preOrder( + ) preOrder ( * ) + * A

26 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ B “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A

27 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ B “. preOrder( + ) preOrder( B ) preOrder ( * ) + * A

28 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( B ) preOrder ( * ) + * A

29 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B

30 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B

31 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B

32 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B

33 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B

34 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ (NULL) as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B

35 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “ (NULL). preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B

36 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( B ) preOrder ( * ) + * A B

37 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true, call returns to previous stage in the stack. preOrder( + ) preOrder( B ) preOrder ( * ) + * A B

38 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in the previous stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder ( * ) + * A B

39 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in the previous stage also completed, call returns to the previous stage in the stack. preOrder( + ) + * A B

40 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that stage as indicated here by arrow. preOrder( + )

41 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ / “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( / ) + * A B

42 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ / “. preOrder( + ) preOrder( / ) + * A B

43 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( / ) + * A B

44 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( / ) + */ ABCD + * A B /

45 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ C “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /

46 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ C “. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /

47 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( C ) preOrder( / ) + * A B /

48 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C

49 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

50 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

51 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

52 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C

53 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

54 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

55 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( C ) preOrder( / ) + * A B / C

56 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( C ) preOrder( / ) + * A B / C

57 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( / ) + * A B / C

58 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ * “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C

59 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ D “. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C

60 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C

61 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Printing the localRoot value using diplayNode () method. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D

62 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

63 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

64 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

65 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D

66 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Calling the preOrder function with “ “ as the localRoot. Pushes the method call into stack. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

67 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Start the execution of preOrder function with localRoot as “ “. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

68 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. preOrder( + ) preOrder( ) preOrder( D ) preOrder( / ) + * A B / C D

69 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD Check if localRoot value is null or not. As the results evaluates to true call returns to previous stage in the stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D

70 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( D ) preOrder( / ) + * A B / C D

71 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) preOrder( / ) + * A B / C D

72 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As all the statements in this stage also completed, call returns to the previous stage in the stack. preOrder( + ) + * A B / C D

73 private void preOrder(node localRoot){ if(localRoot != null) { localRoot.displayNode(); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); }} + */ ABCD As the stack is empty the call comes out of the algorithm. + * A B / C D

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

75 private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.

76 private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } + */ ABCD Start the execution of inOrder function with localRoot as “ + “. Pushes the method call into stack. inOrder( + )

77 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + )

78 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ * “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( * )

79 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of preOrder function with localRoot as “ * “. inOrder( + ) inOrder( * )

80 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( * )

81 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ A “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder (A ) inOrder( * )

82 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ A “. inOrder( + ) inOrder( A ) inOrder( * )

83 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( A ) inOrder( * )

84 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )

85 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )

86 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * )

87 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( A ) inOrder( * )

88 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( A ) inOrder( * ) A

89 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A

90 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A

91 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( A ) inOrder( * ) A

92 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( A ) inOrder( * ) A

93 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( * ) A

94 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( * ) A *

95 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ B “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( B ) inOrder( * ) A *

96 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ B “. inOrder( + ) inOrder( B ) inOrder( * ) A *

97 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( B ) inOrder( * ) A *

98 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *

99 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *

100 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A *

101 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( B ) inOrder( * ) A *

102 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( B ) inOrder( * ) A *

103 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( B ) inOrder( * ) A * B

104 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B

105 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B

106 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( B ) inOrder( * ) A * B

107 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( B ) inOrder( * ) A * B

108 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) inOrder( * ) A * B

109 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) A * B

110 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) A * B +

111 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ / “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( / ) A * B +

112 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ / “. inOrder( + ) inOrder( / ) A * B +

113 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( / ) A * B +

114 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ C “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B +

115 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ C “. inOrder( + ) inOrder( C ) inOrder( / ) A * B +

116 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( C ) inOrder( / ) A * B +

117 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +

118 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +

119 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B +

120 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B +

121 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( C ) inOrder( / ) A * B +

122 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( C ) inOrder( / ) A * B + C

123 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C

124 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C

125 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( C ) inOrder( / ) A * B + C

126 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( C ) inOrder( / ) A * B + C

127 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( / ) A * B + C

128 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( / ) A * B + C /

129 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ D “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /

130 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ D “. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /

131 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /

132 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /

133 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /

134 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C /

135 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /

136 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Then executes remaining statements in that method call as indicated here by arrow. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C /

137 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Printing the localRoot value using diplayNode () method. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C / D

138 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Calling the inOrder function with “ “ as the localRoot. Pushes the method call into stack. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D

139 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Start the execution of inOrder function with localRoot as “ “. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D

140 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } Check if localRoot value is null or not. inOrder( + ) inOrder( ) inOrder( D ) inOrder( / ) A * B + C / D

141 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the results evaluates to true call returns to previous stage in the stack. inOrder( + ) inOrder( D ) inOrder( / ) A * B + C / D

142 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) inOrder( / ) A * B + C / D

143 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. inOrder( + ) A * B + C / D

144 + */ ABCD private void inOrder(node localRoot){ if(localRoot != null){ inOrder(localRoot.leftChild); localRoot.displayNode(); inOrder(localRoot.rightChild); } } As the stack is empty the call comes out of the algorithm. A * B + C / D

145 Post Order Binary Tree In Post-order Traversal the order of traversing the nodes is as follows.  Left sub-tree  Right sub-tree  Root

146 private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD Consider this tree for traversal and follow the algorithm on the right side for traversal.

147 private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } + */ ABCD postOrder( + ) Start the execution of inOrder function with localRoot as “ + “. Pushes the method call into stack.

148 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + )

149 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ * “as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( * )

150 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ * “. postOrder( + ) postOrder( * )

151 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( * )

152 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ A “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( A ) postOrder( * )

153 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ A “. postOrder( + ) postOrder( A ) postOrder( * )

154 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( A ) postOrder( * )

155 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ * “as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

156 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

157 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

158 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( A ) postOrder( * )

159 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( A ) postOrder( * )

160 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

161 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

162 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( A ) postOrder( * )

163 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( A ) postOrder( * )

164 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( A ) postOrder( * )

165 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( A ) postOrder( * ) A

166 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( * ) A

167 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( * ) A

168 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ B“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( B ) postOrder( * ) A

169 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ B “. postOrder( + ) postOrder( B ) postOrder( * ) A

170 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( B ) postOrder( * ) A

171 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

172 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

173 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

174 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( B ) postOrder( * ) A

175 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( B ) postOrder( * ) A

176 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

177 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

178 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( B ) postOrder( * ) A

179 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( B ) postOrder( * ) A

180 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( B ) postOrder( * ) A

181 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( B ) postOrder( * ) A B

182 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( * ) A B

183 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( * ) A B

184 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( * ) A B *

185 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) A B *

186 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) A B *

187 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ / “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( / ) A B *

188 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ / “. postOrder( + ) postOrder( / ) A B *

189 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( / ) A B *

190 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ C“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *

191 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ C “. postOrder( + ) postOrder( C ) postOrder( / ) A B *

192 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( C ) postOrder( / ) A B *

193 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

194 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

195 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

196 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *

197 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( C ) postOrder( / ) A B *

198 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

199 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

200 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( C ) postOrder( / ) A B *

201 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( C ) postOrder( / ) A B *

202 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( C ) postOrder( / ) A B *

203 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( C ) postOrder( / ) A B * C

204 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( / ) A B * C

205 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( / ) A B * C

206 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ D“ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

207 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ D “. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

208 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

209 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

210 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

211 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

212 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

213 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

214 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Calling the postOrder function with “ “ as the localRoot. Pushes the method call into stack. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

215 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Start the execution of postOrder function with localRoot as “ “. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

216 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Check if localRoot value is null or not. postOrder( + ) postOrder( ) postOrder( D ) postOrder( / ) A B * C

217 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As the results evaluates to true call returns to previous stage in the stack. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

218 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( D ) postOrder( / ) A B * C

219 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( D ) postOrder( / ) A B * C D

220 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) postOrder( / ) A B * C D

221 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) postOrder( / ) A B * C D

222 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) postOrder( / ) A B * C D /

223 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. postOrder( + ) A B * C D /

224 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Then executes remaining statements in that method call as indicated here by arrow. postOrder( + ) A B * C D /

225 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } Printing the localRoot value using diplayNode () method. postOrder( + ) A B * C D / +

226 + */ ABCD private void postOrder(node localRoot){ if(localRoot != null){ postOrder(localRoot.leftChild); postOrder(localRoot.rightChild); localRoot.displayNode(); } } As all the statements in this stage also completed, call returns to the previous stage in the stack. A B * C D / +

227


Download ppt "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."

Similar presentations


Ads by Google