Preorder Traversal Inorder Traversal Postorder Traversal

J.E.D.I root = n; } BinaryTreeBTNode n, BTNode left, BTNode right{ root = n; root.left = left; root.right = right; } }

4.5 Binary Tree Traversals

Computations in binary trees often involve traversals. Traversal is a procedure that visits the nodes of the binary tree in a linear fashion such that each node is visited exactly once, where visit is defined as performing computations local to the node. There are three ways to traverse a tree: preorder, inorder, postorder. The prefix pre, in and post refers to the order in which the root of every subtree is visited.

4.5.1 Preorder Traversal

In preorder traversal of a binary tree, the root is visited first. Then the children are traversed recursively in the same manner. This traversal algorithm is useful for applications requiring listing of elements where the parents must always come before their children. The Method: If the binary tree is empty, do nothing traversal done Otherwise: Visit the root. Traverse the left subtree in preorder. Traverse the right subtree in preorder. Figure 1.41 Preorder Traversal Figure 1.42 Data Structures 56 J.E.D.I In Java, add the following method to class BinaryTree: Outputs the preorder listing of elements in this tree void preorder{ if root = null { System.out.printlnroot.info.toString; new BinaryTreeroot.left.preorder; new BinaryTreeroot.right.preorder; } }

4.5.2 Inorder Traversal

The inorder traversal of a binary tree can be informally defined as the “left-to-right” traversal of a binary tree. That is, the left subtree is recursively traversed in inorder, followed by a visit to its parent, then finished with recursive traversal of the right subtree also in inorder. The Method: If the binary tree is empty, do nothing traversal done Otherwise: Traverse the left subtree in inorder. Visit the root. Traverse the right subtree in inorder. Figure 1.43 Inorder Traversal Figure 1.44 In Java, add the following method to class BinaryTree: Outputs the inorder listing of elements in this tree void inorder{ if root = null { new BinaryTreeroot.left.inorder; System.out.printlnroot.info.toString; new BinaryTreeroot.right.inorder; Data Structures 57 J.E.D.I } }

4.5.3 Postorder Traversal

Postorder traversal is the opposite of preorder, that is, it recursively traverses the children first before their parents. The Method: If the binary tree is empty, do nothing traversal done Otherwise: Traverse the left subtree in postorder. Traverse the right subtree in postorder. Visit the root. Figure 1.45 Postorder Traversal Figure 1.46 In Java, add the following method to class BinaryTree: Outputs the postorder listing of elements in this tree void postorder{ if root = null { new BinaryTreeroot.left.postorder; new BinaryTreeroot.right.postorder; System.out.printlnroot.info.toString; } } The following are some examples: Data Structures 58 J.E.D.I Figure 1.47 Example 1 Figure 1.48 Figure 1.49 Example 2 Figure 1.50 Data Structures 59 J.E.D.I

4.6 Applications of Binary Tree Traversals