|
| 1 | +package Tree; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +public class BinaryTree { |
| 8 | + BinaryTreeNode root; |
| 9 | + |
| 10 | + BinaryTree() { |
| 11 | + } |
| 12 | + |
| 13 | + BinaryTree(int d) { |
| 14 | + root = new BinaryTreeNode(d); |
| 15 | + } |
| 16 | + |
| 17 | + /* Recursive approach of inOrder Traversal of Tree |
| 18 | + * Time Complexity: O(n) |
| 19 | + * Space Complexity: O(n) (implicit) |
| 20 | + */ |
| 21 | + public void inOrder(BinaryTreeNode root) { |
| 22 | + if (root == null) return; |
| 23 | + inOrder(root.left); |
| 24 | + System.out.print(root.data + " "); |
| 25 | + inOrder(root.right); |
| 26 | + } |
| 27 | + |
| 28 | + /* Iterative approach of inOrder Traversal of Tree |
| 29 | + * Time Complexity: O(n) |
| 30 | + * Space Complexity: O(n) (explicit) |
| 31 | + */ |
| 32 | + public void inOrderWithoutRecursion(BinaryTreeNode root) { |
| 33 | + if (root == null) return; |
| 34 | + Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>(); |
| 35 | + |
| 36 | + while (root != null) { |
| 37 | + stack.push(root); |
| 38 | + root = root.left; |
| 39 | + } |
| 40 | + while (!stack.empty()) { |
| 41 | + BinaryTreeNode temp = stack.pop(); |
| 42 | + System.out.print(temp.data + " "); |
| 43 | + if (temp.right != null) |
| 44 | + root = temp.right; |
| 45 | + while (root != null) { |
| 46 | + stack.push(root); |
| 47 | + root = root.left; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /* inOrder Traversal of Tree with using any extra space, Morris Traversal |
| 53 | + * Time Complexity: O(n) |
| 54 | + * Space Complexity: O(1) |
| 55 | + */ |
| 56 | + public void inOrderMorrisTraversal(BinaryTreeNode root) { |
| 57 | + while (root != null) { |
| 58 | + if (root.left == null) { |
| 59 | + System.out.print(root.data + " "); |
| 60 | + root = root.right; |
| 61 | + } else { |
| 62 | + BinaryTreeNode prev = root.left; |
| 63 | + while (prev.right != null && prev.right != root) |
| 64 | + prev = prev.right; |
| 65 | + if (prev.right == null) { |
| 66 | + prev.right = root; |
| 67 | + root = root.left; |
| 68 | + } else { |
| 69 | + prev.right = null; |
| 70 | + System.out.print(root.data + " "); |
| 71 | + root = root.right; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + BinaryTree binaryTree = new BinaryTree(); |
| 79 | + binaryTree.root = new BinaryTreeNode(1); |
| 80 | + binaryTree.root.left = new BinaryTreeNode(2); |
| 81 | + binaryTree.root.right = new BinaryTreeNode(3); |
| 82 | + binaryTree.root.left.left = new BinaryTreeNode(4); |
| 83 | + binaryTree.root.left.right = new BinaryTreeNode(5); |
| 84 | + binaryTree.root.right.right = new BinaryTreeNode(6); |
| 85 | + binaryTree.root.left.left.right = new BinaryTreeNode(7); |
| 86 | + binaryTree.root.left.right.left = new BinaryTreeNode(8); |
| 87 | + binaryTree.root.right.right.left = new BinaryTreeNode(9); |
| 88 | + |
| 89 | + System.out.println("\nInOrder"); |
| 90 | + binaryTree.inOrder(binaryTree.root); |
| 91 | + |
| 92 | + System.out.println("\nInOrderWithoutRecurion"); |
| 93 | + binaryTree.inOrderWithoutRecursion(binaryTree.root); |
| 94 | + System.out.println("\nInOrderMorrisTraversal"); |
| 95 | + binaryTree.inOrderMorrisTraversal(binaryTree.root); |
| 96 | + |
| 97 | + } |
| 98 | +} |
0 commit comments