|
| 1 | +public class BinaryTree { |
| 2 | + static class Node{ |
| 3 | + int data; |
| 4 | + Node left; |
| 5 | + Node right; |
| 6 | + |
| 7 | + public Node(int data){ |
| 8 | + this.data = data; |
| 9 | + this.left = null; |
| 10 | + this.right = null; |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + static class tree{ |
| 15 | + static int idx = -1; |
| 16 | + public Node createBinaryTree(int [] nodes){ |
| 17 | + idx++; |
| 18 | + if (nodes[idx]==-1){ |
| 19 | + return null; |
| 20 | + } |
| 21 | + Node newNode = new Node(nodes[idx]); |
| 22 | + newNode.left = createBinaryTree(nodes); |
| 23 | + newNode.right = createBinaryTree(nodes); |
| 24 | + |
| 25 | + return newNode; |
| 26 | + } |
| 27 | + |
| 28 | + public static void preOrder(Node root){ |
| 29 | + if(root == null){ |
| 30 | + System.out.print(" " + -1 + " "); |
| 31 | + return; |
| 32 | + } |
| 33 | + System.out.print(" " + root.data + " "); |
| 34 | + preOrder(root.left); |
| 35 | + preOrder(root.right); |
| 36 | + } |
| 37 | + |
| 38 | + public static void inOrder(Node root){ |
| 39 | + if (root == null){ |
| 40 | + System.out.print(" "+ -1 + " "); |
| 41 | + return; |
| 42 | + } |
| 43 | + inOrder(root.left); |
| 44 | + System.out.print(" "+ root.data +" "); |
| 45 | + inOrder(root.right); |
| 46 | + } |
| 47 | + |
| 48 | + public static void postOrder(Node root){ |
| 49 | + if (root == null){ |
| 50 | + System.out.print(" " + -1 + " "); |
| 51 | + return; |
| 52 | + } |
| 53 | + postOrder(root.left); |
| 54 | + postOrder(root.right); |
| 55 | + System.out.print(" "+ root.data + " "); |
| 56 | + } |
| 57 | + |
| 58 | + /* public static void levelOrder(Node root){ |
| 59 | + if (root == null){ |
| 60 | + System.out.println(-1 + " "); |
| 61 | + return; |
| 62 | + } |
| 63 | + Queue<Node> queue = new LinkedList<>(); |
| 64 | + queue.add(root); |
| 65 | + queue.add(null); |
| 66 | + while (queue != null){ |
| 67 | + Node currentNode = queue.remove(); |
| 68 | + if (currentNode == null){ |
| 69 | + System.out.println(); |
| 70 | + if(queue == null){ |
| 71 | + break; |
| 72 | + } |
| 73 | + else { |
| 74 | + queue.add(null); |
| 75 | + } |
| 76 | + }else { |
| 77 | + System.out.println(currentNode.data+ " "); |
| 78 | + if () |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + */ |
| 84 | + |
| 85 | + public static int heightOfTree(Node root){ |
| 86 | + if (root == null){ |
| 87 | + return 0; |
| 88 | + } |
| 89 | + int leftHeight = heightOfTree(root.left); |
| 90 | + int rightHeight= heightOfTree(root.right); |
| 91 | + return 1 + Math.max(leftHeight, rightHeight); |
| 92 | + } |
| 93 | + |
| 94 | + public static int countOfNodes(Node root){ |
| 95 | + if (root == null){ |
| 96 | + return 0; |
| 97 | + } |
| 98 | + int leftNode = countOfNodes(root.left); |
| 99 | + int rightNode = countOfNodes(root.right); |
| 100 | + return rightNode + leftNode + 1; |
| 101 | + } |
| 102 | + |
| 103 | + public static int sumOfNodes(Node root){ |
| 104 | + if (root == null){ |
| 105 | + return 0; |
| 106 | + } |
| 107 | + int leftSum = sumOfNodes(root.left); |
| 108 | + int rightSum = sumOfNodes(root.right); |
| 109 | + return leftSum + rightSum + root.data; |
| 110 | + } |
| 111 | + |
| 112 | + public static int diameter(Node root){ |
| 113 | + if (root == null){ |
| 114 | + return 0; |
| 115 | + } |
| 116 | + int diam1 = heightOfTree(root.right) + heightOfTree(root.left) + 1; |
| 117 | + int diam2 = diameter(root.left); |
| 118 | + int diam3 = diameter(root.right); |
| 119 | + return Math.max(Math.max(diam1, diam2), diam3); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + public static void main(String[] args) { |
| 127 | + int [] nodes = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1}; |
| 128 | + tree bt = new tree(); |
| 129 | + Node root = bt.createBinaryTree(nodes); |
| 130 | + System.out.println(root.data); |
| 131 | + tree.preOrder(root); |
| 132 | + System.out.println(); |
| 133 | + tree.postOrder(root); |
| 134 | + System.out.println(); |
| 135 | + tree.inOrder(root); |
| 136 | + System.out.println(); |
| 137 | + System.out.println(tree.countOfNodes(root)); |
| 138 | + System.out.println(tree.sumOfNodes(root)); |
| 139 | + System.out.println(tree.heightOfTree(root)); |
| 140 | + System.out.println(tree.diameter(root)); |
| 141 | + } |
| 142 | +} |
0 commit comments