|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction; |
| 5 | + |
| 6 | +public class BinaryTreeUse { |
| 7 | + |
| 8 | + public static void printTree(BinaryTreeNode<Integer> root) { |
| 9 | + if (root == null) { |
| 10 | + return; |
| 11 | + } |
| 12 | + String toBePrinted = root.data + ""; |
| 13 | + if (root.left != null) { |
| 14 | + toBePrinted += "L:" + root.left.data + ","; |
| 15 | + } |
| 16 | + |
| 17 | + if (root.right != null) { |
| 18 | + toBePrinted += "R:" + root.right.data; |
| 19 | + } |
| 20 | + System.out.println(toBePrinted); |
| 21 | + printTree(root.left); |
| 22 | + printTree(root.right); |
| 23 | + } |
| 24 | + |
| 25 | + public static BinaryTreeNode<Integer> takeInput(Scanner s) { |
| 26 | + int rootData; |
| 27 | + System.out.println("Enter root data"); |
| 28 | + rootData = s.nextInt(); |
| 29 | + if (rootData == -1) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData); |
| 33 | + root.left = takeInput(s); |
| 34 | + root.right = takeInput(s); |
| 35 | + return root; |
| 36 | + } |
| 37 | + |
| 38 | + public static BinaryTreeNode<Integer> takeInputLevelWise() { |
| 39 | + Scanner s = new Scanner(System.in); |
| 40 | + QueueUsingLL<BinaryTreeNode<Integer>> pendingNodes = new QueueUsingLL<>(); |
| 41 | + System.out.println("Enter root data"); |
| 42 | + int rootData = s.nextInt(); |
| 43 | + if (rootData == -1) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData); |
| 47 | + pendingNodes.enqueue(root); |
| 48 | + |
| 49 | + while (!pendingNodes.isEmpty()) { |
| 50 | + BinaryTreeNode<Integer> front; |
| 51 | + try { |
| 52 | + front = pendingNodes.dequeue(); |
| 53 | + } catch (QueueEmptyException e) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + System.out.println("Enter left child of " + front.data); |
| 57 | + int leftChild = s.nextInt(); |
| 58 | + if (leftChild != -1) { |
| 59 | + BinaryTreeNode<Integer> child = new BinaryTreeNode<Integer>(leftChild); |
| 60 | + pendingNodes.enqueue(child); |
| 61 | + front.left = child; |
| 62 | + } |
| 63 | + |
| 64 | + System.out.println("Enter right child of " + front.data); |
| 65 | + int rightChild = s.nextInt(); |
| 66 | + if (rightChild != -1) { |
| 67 | + BinaryTreeNode<Integer> child = new BinaryTreeNode<Integer>(rightChild); |
| 68 | + pendingNodes.enqueue(child); |
| 69 | + front.right = child; |
| 70 | + } |
| 71 | + } |
| 72 | + return root; |
| 73 | + } |
| 74 | + |
| 75 | + public static int countNodes(BinaryTreeNode<Integer> root) { |
| 76 | + if (root == null) { |
| 77 | + return 0; |
| 78 | + } |
| 79 | + int ans = 1; |
| 80 | + ans += countNodes(root.left); |
| 81 | + ans += countNodes(root.right); |
| 82 | + return ans; |
| 83 | + } |
| 84 | + |
| 85 | + public static void mirror(BinaryTreeNode<Integer> root) { |
| 86 | + if (root == null) { |
| 87 | + return; |
| 88 | + } |
| 89 | + mirror(root.left); |
| 90 | + mirror(root.right); |
| 91 | + BinaryTreeNode<Integer> temp = root.left; |
| 92 | + root.left = root.right; |
| 93 | + root.right = temp; |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + public static int diameter(BinaryTreeNode<Integer> root) { |
| 98 | + if (root == null) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + int option1 = height(root.left) + height(root.right); |
| 103 | + int option2 = diameter(root.left); |
| 104 | + int option3 = diameter(root.right); |
| 105 | + return Math.max(option1, Math.max(option2, option3)); |
| 106 | + } |
| 107 | + |
| 108 | + public static int height(BinaryTreeNode<Integer> root) { |
| 109 | + if (root == null) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + int lh = height(root.left); |
| 113 | + int rh = height(root.right); |
| 114 | + return 1 + Math.max(lh, rh); |
| 115 | + } |
| 116 | + |
| 117 | + public static Pair<Integer, Integer> heightDiameter(BinaryTreeNode<Integer> root) { |
| 118 | + if (root == null) { |
| 119 | + Pair<Integer,Integer> output = new Pair<>(); |
| 120 | + output.first = 0; |
| 121 | + output.second = 0; |
| 122 | + return output; |
| 123 | + } |
| 124 | + Pair<Integer, Integer> lo = heightDiameter(root.left); |
| 125 | + Pair<Integer, Integer> ro = heightDiameter(root.right); |
| 126 | + int height = 1 + Math.max(lo.first, ro.first); |
| 127 | + int option1 = lo.first + ro.first; |
| 128 | + int option2 = lo.second; |
| 129 | + int option3 = ro.second; |
| 130 | + int diameter = Math.max(option1, Math.max(option2, option3)); |
| 131 | + Pair<Integer,Integer> output = new Pair<>(); |
| 132 | + output.first = height; |
| 133 | + output.second = diameter; |
| 134 | + return output; |
| 135 | + } |
| 136 | + |
| 137 | + public static void inorder(BinaryTreeNode<Integer> root) { |
| 138 | + if (root == null) { |
| 139 | + return; |
| 140 | + } |
| 141 | + inorder(root.left); |
| 142 | + System.out.print(root.data + " "); |
| 143 | + inorder(root.right); |
| 144 | + } |
| 145 | + |
| 146 | + public static BinaryTreeNode<Integer> buildTreeHelper(int in[], int pre[], int inS, int inE, int preS, int preE) { |
| 147 | + if (inS > inE) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + int rootData = pre[preS]; |
| 151 | + BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(rootData); |
| 152 | + int rootInIndex = -1; |
| 153 | + for (int i = inS; i <= inE; i++) { |
| 154 | + if (in[i] == rootData) { |
| 155 | + rootInIndex = i; |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + if (rootInIndex == -1) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + int leftInS = inS; |
| 164 | + int leftInE = rootInIndex - 1; |
| 165 | + int leftPreS = preS + 1; |
| 166 | + int leftPreE = leftInE - leftInS + leftPreS; |
| 167 | + int rightInS = rootInIndex + 1; |
| 168 | + int rightInE = inE; |
| 169 | + int rightPreS = leftPreE + 1; |
| 170 | + int rightPreE = preE; |
| 171 | + root.left = buildTreeHelper(in, pre, leftInS, leftInE, leftPreS, leftPreE); |
| 172 | + root.right = buildTreeHelper(in, pre, rightInS, rightInE, rightPreS, rightPreE); |
| 173 | + return root; |
| 174 | + } |
| 175 | + |
| 176 | + public static BinaryTreeNode<Integer> buildTree(int in[], int pre[]) { |
| 177 | + return buildTreeHelper(in, pre, 0, in.length - 1, 0, pre.length -1); |
| 178 | + } |
| 179 | + |
| 180 | + public static BinaryTreeNode<Integer> searchInBST(BinaryTreeNode<Integer> root, int q) { |
| 181 | + if (root == null) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + if (root.data == q) { |
| 185 | + return root; |
| 186 | + } else if (root.data > q) { |
| 187 | + return searchInBST(root.left, q); |
| 188 | + } else { |
| 189 | + return searchInBST(root.right, q); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + public static void printBetweenK1K2(BinaryTreeNode<Integer> root, int k1, int k2) { |
| 194 | + if (root == null) { |
| 195 | + return; |
| 196 | + } |
| 197 | + if (root.data >= k1 && root.data <= k2) { |
| 198 | + System.out.println(root.data); |
| 199 | + } |
| 200 | + |
| 201 | + if (root.data > k1) { |
| 202 | + printBetweenK1K2(root.left, k1, k2); |
| 203 | + } |
| 204 | + |
| 205 | + if (root.data <= k2) { |
| 206 | + printBetweenK1K2(root.right, k1, k2); |
| 207 | + } |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + public static int minimum(BinaryTreeNode<Integer> root) { |
| 212 | + if (root == null) { |
| 213 | + return Integer.MAX_VALUE; |
| 214 | + } |
| 215 | + return Math.min(root.data, Math.min(minimum(root.left), minimum(root.right))); |
| 216 | + } |
| 217 | + |
| 218 | + public static int maximum(BinaryTreeNode<Integer> root) { |
| 219 | + if (root == null) { |
| 220 | + return Integer.MIN_VALUE; |
| 221 | + } |
| 222 | + return Math.max(root.data, Math.max(maximum(root.left), maximum(root.right))); |
| 223 | + } |
| 224 | + |
| 225 | + public static boolean isBST(BinaryTreeNode<Integer> root) { |
| 226 | + if (root == null) { |
| 227 | + return true; |
| 228 | + } |
| 229 | + int leftMax = maximum(root.left); |
| 230 | + int rightMin = minimum(root.right); |
| 231 | + if (root.data <= leftMax) { |
| 232 | + return false; |
| 233 | + } |
| 234 | + |
| 235 | + if (root.data > rightMin) { |
| 236 | + return false; |
| 237 | + } |
| 238 | + boolean isLeftBST = isBST(root.left); |
| 239 | + boolean isRightBST = isBST(root.right); |
| 240 | + if (isLeftBST && isRightBST) { |
| 241 | + return true; |
| 242 | + } else { |
| 243 | + return false; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + public static Pair<Boolean, Pair<Integer, Integer>> isBST2(BinaryTreeNode<Integer> root) { |
| 248 | + if (root == null) { |
| 249 | + Pair<Boolean, Pair<Integer, Integer>> output = new Pair<Boolean, Pair<Integer, Integer>>(); |
| 250 | + output.first = true; |
| 251 | + output.second = new Pair<Integer, Integer>(); |
| 252 | + output.second.first = Integer.MAX_VALUE; |
| 253 | + output.second.second = Integer.MIN_VALUE; |
| 254 | + return output; |
| 255 | + } |
| 256 | + Pair<Boolean, Pair<Integer, Integer>> leftOutput = isBST2(root.left); |
| 257 | + Pair<Boolean, Pair<Integer, Integer>> rightOutput = isBST2(root.right); |
| 258 | + int min = Math.min(root.data, Math.min(leftOutput.second.first, rightOutput.second.first)); |
| 259 | + int max = Math.max(root.data, Math.max(leftOutput.second.second, rightOutput.second.second)); |
| 260 | + boolean isBST = (root.data > leftOutput.second.second) |
| 261 | + && (root.data <= rightOutput.second.first) |
| 262 | + && leftOutput.first && rightOutput.first; |
| 263 | + Pair<Boolean, Pair<Integer, Integer>> output = new Pair<Boolean, Pair<Integer, Integer>>(); |
| 264 | + output.first = isBST; |
| 265 | + output.second = new Pair<Integer, Integer>(); |
| 266 | + output.second.first = min; |
| 267 | + output.second.second = max; |
| 268 | + return output; |
| 269 | + } |
| 270 | + |
| 271 | + public static void main(String[] args) { |
| 272 | +// BinaryTreeNode<Integer> root = takeInputLevelWise(); |
| 273 | +// printTree(root); |
| 274 | + } |
| 275 | +} |
0 commit comments