|
| 1 | +// Java program for different tree traversals |
| 2 | + |
| 3 | +/* Class containing left and right child of current |
| 4 | +node and key value*/ |
| 5 | +class Node |
| 6 | +{ |
| 7 | + int key; |
| 8 | + Node left, right; |
| 9 | + |
| 10 | + public Node(int item) |
| 11 | + { |
| 12 | + key = item; |
| 13 | + left = right = null; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +class BinaryTree |
| 18 | +{ |
| 19 | + // Root of Binary Tree |
| 20 | + Node root; |
| 21 | + |
| 22 | + BinaryTree() |
| 23 | + { |
| 24 | + root = null; |
| 25 | + } |
| 26 | + |
| 27 | + /* Given a binary tree, print its nodes according to the |
| 28 | + "bottom-up" postorder traversal. */ |
| 29 | + void printPostorder(Node node) |
| 30 | + { |
| 31 | + if (node == null) |
| 32 | + return; |
| 33 | + |
| 34 | + // first recur on left subtree |
| 35 | + printPostorder(node.left); |
| 36 | + |
| 37 | + // then recur on right subtree |
| 38 | + printPostorder(node.right); |
| 39 | + |
| 40 | + // now deal with the node |
| 41 | + System.out.print(node.key + " "); |
| 42 | + } |
| 43 | + |
| 44 | + /* Given a binary tree, print its nodes in inorder*/ |
| 45 | + void printInorder(Node node) |
| 46 | + { |
| 47 | + if (node == null) |
| 48 | + return; |
| 49 | + |
| 50 | + /* first recur on left child */ |
| 51 | + printInorder(node.left); |
| 52 | + |
| 53 | + /* then print the data of node */ |
| 54 | + System.out.print(node.key + " "); |
| 55 | + |
| 56 | + /* now recur on right child */ |
| 57 | + printInorder(node.right); |
| 58 | + } |
| 59 | + |
| 60 | + /* Given a binary tree, print its nodes in preorder*/ |
| 61 | + void printPreorder(Node node) |
| 62 | + { |
| 63 | + if (node == null) |
| 64 | + return; |
| 65 | + |
| 66 | + /* first print data of node */ |
| 67 | + System.out.print(node.key + " "); |
| 68 | + |
| 69 | + /* then recur on left sutree */ |
| 70 | + printPreorder(node.left); |
| 71 | + |
| 72 | + /* now recur on right subtree */ |
| 73 | + printPreorder(node.right); |
| 74 | + } |
| 75 | + |
| 76 | + // Wrappers over above recursive functions |
| 77 | + void printPostorder() { printPostorder(root); } |
| 78 | + void printInorder() { printInorder(root); } |
| 79 | + void printPreorder() { printPreorder(root); } |
| 80 | + |
| 81 | + // Driver method |
| 82 | + public static void main(String[] args) |
| 83 | + { |
| 84 | + BinaryTree tree = new BinaryTree(); |
| 85 | + tree.root = new Node(1); |
| 86 | + tree.root.left = new Node(2); |
| 87 | + tree.root.right = new Node(3); |
| 88 | + tree.root.left.left = new Node(4); |
| 89 | + tree.root.left.right = new Node(5); |
| 90 | + |
| 91 | + System.out.println("Preorder traversal of binary tree is "); |
| 92 | + tree.printPreorder(); |
| 93 | + |
| 94 | + System.out.println("\nInorder traversal of binary tree is "); |
| 95 | + tree.printInorder(); |
| 96 | + |
| 97 | + System.out.println("\nPostorder traversal of binary tree is "); |
| 98 | + tree.printPostorder(); |
| 99 | + } |
| 100 | +} |
0 commit comments