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.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
1 8/16/2015 MATH 224 – Discrete Mathematics A rooted tree is a tree that has a distinguished node called the root. Just as with all trees a rooted tree.
Binary Trees. 2 Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
Binary Trees. Node structure Data A data field and two pointers, left and right.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Trees – Part 2 CS 367 – Introduction to Data Structures.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Compiled by: Dr. Mohammad Omar Alhawarat
Tree Traversal.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Binary Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Printing a search tree in order
Lecture No.14 Data Structures Dr. Sohail Aslam
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
Trees ---- Soujanya.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
Binary Trees.
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Tree data structure.
Binary Trees.
Binary Trees.
Binary Tree Traversal Methods
Binary Tree Chapter 8 (cont’) Part2.
2018, Fall Pusan National University Ki-Joune Li
if the tree is empty, do nothing,
Chapter 20: Binary Trees.
Trees.
Binary Tree Traversal.
Data Structures Using C++ 2E
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order Breadth First search Level order

A binary tree (not a binary search tree) can also be used to represent an algebraic expression that involves the binary arithmetic operators +, -, /, and *. The root node holds an operator, and each of its subtrees represents either a variable name (like A, B, or C) or another expression. The following slides depicts the Tree traversals using  Preorder Traversal  Inorder Traversal  Postorder Traversal

In Pre-order Traversal the order of traversing the nodes is as follows.  Root  Left sub-tree  Right sub-tree Note : Dotted lines and Dotted Circles in the BST diagram represent NULL Nodes.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

In Order Binary Tree In In-order Traversal the order of traversing the nodes is as follows.  Left sub-tree  Root  Right sub-tree An In-order traversal is most commonly used in binary search tree because it visits all the nodes in ascending order, based on their key values. If you want to create a sorted list of the data in a binary tree, this is one way to do it.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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