|
| 1 | +package DataStructures.Trees; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * <h1>Binary Search Tree (Recursive) Generic Type Implementation</h1> |
| 8 | + * |
| 9 | + * <p> |
| 10 | + * A recursive implementation of generic type BST. |
| 11 | + * |
| 12 | + * Reference: https://en.wikipedia.org/wiki/Binary_search_tree |
| 13 | + * </p> |
| 14 | + * |
| 15 | + * @author [Madhur Panwar](https://github.com/mdrpanwar) |
| 16 | + */ |
| 17 | +public class BSTRecursiveGeneric<T extends Comparable<T>> { |
| 18 | + /** only data member is root of BST */ |
| 19 | + private Node<T> root; |
| 20 | + |
| 21 | + /** Constructor use to initialize node as null */ |
| 22 | + public BSTRecursiveGeneric() { |
| 23 | + root = null; |
| 24 | + } |
| 25 | + |
| 26 | + /** main function for testing */ |
| 27 | + public static void main(String[] args) { |
| 28 | + System.out.println("Testing for integer data..."); |
| 29 | + // Integer |
| 30 | + DataStructures.Trees.BSTRecursiveGeneric<Integer> integerTree = new DataStructures.Trees.BSTRecursiveGeneric<Integer>(); |
| 31 | + |
| 32 | + integerTree.add(5); |
| 33 | + integerTree.add(10); |
| 34 | + integerTree.add(9); |
| 35 | + assert !integerTree.find(4) : "4 is not yet present in BST"; |
| 36 | + assert integerTree.find(10) : "10 should be present in BST"; |
| 37 | + integerTree.remove(9); |
| 38 | + assert !integerTree.find(9) : "9 was just deleted from BST"; |
| 39 | + integerTree.remove(1); |
| 40 | + assert !integerTree.find(1) : "Since 1 was not present so find deleting would do no change"; |
| 41 | + integerTree.add(20); |
| 42 | + integerTree.add(70); |
| 43 | + assert integerTree.find(70) : "70 was inserted but not found"; |
| 44 | + /* |
| 45 | + Will print in following order |
| 46 | + 5 10 20 70 |
| 47 | + */ |
| 48 | + integerTree.inorder(); |
| 49 | + System.out.println(); |
| 50 | + System.out.println("Testing for string data..."); |
| 51 | + // String |
| 52 | + DataStructures.Trees.BSTRecursiveGeneric<String> stringTree = new DataStructures.Trees.BSTRecursiveGeneric<String>(); |
| 53 | + |
| 54 | + stringTree.add("banana"); |
| 55 | + stringTree.add("pineapple"); |
| 56 | + stringTree.add("date"); |
| 57 | + assert !stringTree.find("girl") : "girl is not yet present in BST"; |
| 58 | + assert stringTree.find("pineapple") : "10 should be present in BST"; |
| 59 | + stringTree.remove("date"); |
| 60 | + assert !stringTree.find("date") : "date was just deleted from BST"; |
| 61 | + stringTree.remove("boy"); |
| 62 | + assert !stringTree.find("boy") : "Since boy was not present so deleting would do no change"; |
| 63 | + stringTree.add("india"); |
| 64 | + stringTree.add("hills"); |
| 65 | + assert stringTree.find("hills") : "hills was inserted but not found"; |
| 66 | + /* |
| 67 | + Will print in following order |
| 68 | + banana hills india pineapple |
| 69 | + */ |
| 70 | + stringTree.inorder(); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Recursive method to delete a data if present in BST. |
| 76 | + * |
| 77 | + * @param node the node under which to (recursively) search for data |
| 78 | + * @param data the value to be deleted |
| 79 | + * @return Node the updated value of root parameter after delete operation |
| 80 | + */ |
| 81 | + private Node<T> delete(Node<T> node, T data) { |
| 82 | + if (node == null) { |
| 83 | + System.out.println("No such data present in BST."); |
| 84 | + } else if (node.data.compareTo(data) > 0) { |
| 85 | + node.left = delete(node.left, data); |
| 86 | + } else if (node.data.compareTo(data) < 0) { |
| 87 | + node.right = delete(node.right, data); |
| 88 | + } else { |
| 89 | + if (node.right == null && node.left == null) { // If it is leaf node |
| 90 | + node = null; |
| 91 | + } else if (node.left == null) { // If only right node is present |
| 92 | + Node<T> temp = node.right; |
| 93 | + node.right = null; |
| 94 | + node = temp; |
| 95 | + } else if (node.right == null) { // Only left node is present |
| 96 | + Node<T> temp = node.left; |
| 97 | + node.left = null; |
| 98 | + node = temp; |
| 99 | + } else { // both child are present |
| 100 | + Node<T> temp = node.right; |
| 101 | + // Find leftmost child of right subtree |
| 102 | + while (temp.left != null) { |
| 103 | + temp = temp.left; |
| 104 | + } |
| 105 | + node.data = temp.data; |
| 106 | + node.right = delete(node.right, temp.data); |
| 107 | + } |
| 108 | + } |
| 109 | + return node; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Recursive insertion of value in BST. |
| 114 | + * |
| 115 | + * @param node to check if the data can be inserted in current node or its subtree |
| 116 | + * @param data the value to be inserted |
| 117 | + * @return the modified value of the root parameter after insertion |
| 118 | + */ |
| 119 | + private Node<T> insert(Node<T> node, T data) { |
| 120 | + if (node == null) { |
| 121 | + node = new Node<>(data); |
| 122 | + } else if (node.data.compareTo(data) > 0) { |
| 123 | + node.left = insert(node.left, data); |
| 124 | + } else if (node.data.compareTo(data) < 0) { |
| 125 | + node.right = insert(node.right, data); |
| 126 | + } |
| 127 | + return node; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Recursively print Preorder traversal of the BST |
| 132 | + * |
| 133 | + * @param node the root node |
| 134 | + */ |
| 135 | + private void preOrder(Node<T> node) { |
| 136 | + if (node == null) { |
| 137 | + return; |
| 138 | + } |
| 139 | + System.out.print(node.data + " "); |
| 140 | + if (node.left != null) { |
| 141 | + preOrder(node.left); |
| 142 | + } |
| 143 | + if (node.right != null) { |
| 144 | + preOrder(node.right); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Recursively print Postorder traversal of BST. |
| 150 | + * |
| 151 | + * @param node the root node |
| 152 | + */ |
| 153 | + private void postOrder(Node<T> node) { |
| 154 | + if (node == null) { |
| 155 | + return; |
| 156 | + } |
| 157 | + if (node.left != null) { |
| 158 | + postOrder(node.left); |
| 159 | + } |
| 160 | + if (node.right != null) { |
| 161 | + postOrder(node.right); |
| 162 | + } |
| 163 | + System.out.print(node.data + " "); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Recursively print Inorder traversal of BST. |
| 168 | + * |
| 169 | + * @param node the root node |
| 170 | + */ |
| 171 | + private void inOrder(Node<T> node) { |
| 172 | + if (node == null) { |
| 173 | + return; |
| 174 | + } |
| 175 | + if (node.left != null) { |
| 176 | + inOrder(node.left); |
| 177 | + } |
| 178 | + System.out.print(node.data + " "); |
| 179 | + if (node.right != null) { |
| 180 | + inOrder(node.right); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Recursively traverse the tree using inorder traversal |
| 186 | + * and keep adding elements to argument list. |
| 187 | + * |
| 188 | + * @param node the root node |
| 189 | + * @param sortedList the list to add the srted elements into |
| 190 | + */ |
| 191 | + private void inOrderSort(Node<T> node, List<T> sortedList) { |
| 192 | + if (node == null) { |
| 193 | + return; |
| 194 | + } |
| 195 | + if (node.left != null) { |
| 196 | + inOrderSort(node.left, sortedList); |
| 197 | + } |
| 198 | + sortedList.add(node.data); |
| 199 | + if (node.right != null) { |
| 200 | + inOrderSort(node.right, sortedList); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Serach recursively if the given value is present in BST or not. |
| 206 | + * |
| 207 | + * @param node the node under which to check |
| 208 | + * @param data the value to be checked |
| 209 | + * @return boolean if data is present or not |
| 210 | + */ |
| 211 | + private boolean search(Node<T> node, T data) { |
| 212 | + if (node == null) { |
| 213 | + return false; |
| 214 | + } else if (node.data.compareTo(data) == 0) { |
| 215 | + return true; |
| 216 | + } else if (node.data.compareTo(data) > 0) { |
| 217 | + return search(node.left, data); |
| 218 | + } else { |
| 219 | + return search(node.right, data); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * add in BST. if the value is not already present it is inserted or else no change takes place. |
| 225 | + * |
| 226 | + * @param data the value to be inserted |
| 227 | + */ |
| 228 | + public void add(T data) { |
| 229 | + this.root = insert(this.root, data); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * If data is present in BST delete it else do nothing. |
| 234 | + * |
| 235 | + * @param data the value to be removed |
| 236 | + */ |
| 237 | + public void remove(T data) { |
| 238 | + this.root = delete(this.root, data); |
| 239 | + } |
| 240 | + |
| 241 | + /** To call inorder traversal on tree */ |
| 242 | + public void inorder() { |
| 243 | + System.out.println("Inorder traversal of this tree is:"); |
| 244 | + inOrder(this.root); |
| 245 | + System.out.println(); // for next line |
| 246 | + } |
| 247 | + |
| 248 | + /** return a sorted list by traversing the tree elements using inorder traversal */ |
| 249 | + public List<T> inorderSort() { |
| 250 | + List<T> sortedList = new ArrayList<>(); |
| 251 | + inOrderSort(this.root, sortedList); |
| 252 | + return sortedList; |
| 253 | + } |
| 254 | + |
| 255 | + /** To call postorder traversal on tree */ |
| 256 | + public void postorder() { |
| 257 | + System.out.println("Postorder traversal of this tree is:"); |
| 258 | + postOrder(this.root); |
| 259 | + System.out.println(); // for next line |
| 260 | + } |
| 261 | + |
| 262 | + /** To call preorder traversal on tree. */ |
| 263 | + public void preorder() { |
| 264 | + System.out.println("Preorder traversal of this tree is:"); |
| 265 | + preOrder(this.root); |
| 266 | + System.out.println(); // for next line |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * To check if given value is present in tree or not. |
| 271 | + * |
| 272 | + * @param data the data to be found for |
| 273 | + */ |
| 274 | + public boolean find(T data) { |
| 275 | + if (search(this.root, data)) { |
| 276 | + System.out.println(data + " is present in given BST."); |
| 277 | + return true; |
| 278 | + } |
| 279 | + System.out.println(data + " not found."); |
| 280 | + return false; |
| 281 | + } |
| 282 | + |
| 283 | + /** The generic Node class used for building binary search tree */ |
| 284 | + private static class Node<T> { |
| 285 | + T data; |
| 286 | + Node<T> left; |
| 287 | + Node<T> right; |
| 288 | + |
| 289 | + /** Constructor with data as parameter */ |
| 290 | + Node(T d) { |
| 291 | + data = d; |
| 292 | + left = null; |
| 293 | + right = null; |
| 294 | + } |
| 295 | + } |
| 296 | +} |
0 commit comments