|
| 1 | +/* |
| 2 | +Copyright (C) Deepali Srivastava - All Rights Reserved |
| 3 | +This code is part of DSA course available on CourseGalaxy.com |
| 4 | +*/ |
| 5 | + |
| 6 | + |
| 7 | +import java.lang.NullPointerException; |
| 8 | + |
| 9 | +public class BinarySearchTree |
| 10 | +{ |
| 11 | + private TreeNode root; |
| 12 | + private static int x; |
| 13 | + |
| 14 | + public BinarySearchTree() |
| 15 | + { |
| 16 | + root=null; |
| 17 | + } |
| 18 | + |
| 19 | + public void inorder(int[] a) |
| 20 | + { |
| 21 | + x=0; |
| 22 | + inorder(root, a); |
| 23 | + } |
| 24 | + |
| 25 | + private void inorder(TreeNode p, int[] a) |
| 26 | + { |
| 27 | + if(p==null) |
| 28 | + return; |
| 29 | + inorder(p.lchild,a); |
| 30 | + a[x++] = p.info; |
| 31 | + inorder(p.rchild,a); |
| 32 | + } |
| 33 | + |
| 34 | + boolean isEmpty() |
| 35 | + { |
| 36 | + return (root==null); |
| 37 | + } |
| 38 | + public void insert(int x) |
| 39 | + { |
| 40 | + root = insert(root,x); |
| 41 | + } |
| 42 | + |
| 43 | + private TreeNode insert(TreeNode p, int x) |
| 44 | + { |
| 45 | + if(p==null) |
| 46 | + p=new TreeNode(x); |
| 47 | + else if(x < p.info) |
| 48 | + p.lchild = insert(p.lchild,x); |
| 49 | + else |
| 50 | + p.rchild = insert(p.rchild,x); |
| 51 | + return p; |
| 52 | + } |
| 53 | + |
| 54 | + public boolean search(int x) |
| 55 | + { |
| 56 | + return(search(root,x)!=null); |
| 57 | + } |
| 58 | + private TreeNode search(TreeNode p, int x) |
| 59 | + { |
| 60 | + if(p==null) |
| 61 | + return null; /*key not found*/ |
| 62 | + if(x < p.info)/*search in left subtree*/ |
| 63 | + return search(p.lchild, x); |
| 64 | + if(x > p.info)/*search in right subtree*/ |
| 65 | + return search(p.rchild, x); |
| 66 | + return p; /*key found*/ |
| 67 | + } |
| 68 | + |
| 69 | + public void delete(int x) |
| 70 | + { |
| 71 | + root = delete(root,x); |
| 72 | + } |
| 73 | + |
| 74 | + private TreeNode delete(TreeNode p, int x) |
| 75 | + { |
| 76 | + TreeNode ch,s; |
| 77 | + |
| 78 | + if(p==null) |
| 79 | + { |
| 80 | + System.out.println(x + " not found"); |
| 81 | + return p; |
| 82 | + } |
| 83 | + if(x < p.info) /*delete from left subtree*/ |
| 84 | + p.lchild = delete(p.lchild, x); |
| 85 | + else if(x > p.info) /*delete from right subtree*/ |
| 86 | + p.rchild = delete(p.rchild, x); |
| 87 | + else |
| 88 | + { |
| 89 | + /*key to be deleted is found*/ |
| 90 | + if( p.lchild!=null && p.rchild!=null ) /*2 children*/ |
| 91 | + { |
| 92 | + s=p.rchild; |
| 93 | + while(s.lchild!=null) |
| 94 | + s=s.lchild; |
| 95 | + p.info=s.info; |
| 96 | + p.rchild = delete(p.rchild,s.info); |
| 97 | + } |
| 98 | + else /*1 child or no child*/ |
| 99 | + { |
| 100 | + if(p.lchild != null) /*only left child*/ |
| 101 | + ch=p.lchild; |
| 102 | + else /*only right child or no child*/ |
| 103 | + ch=p.rchild; |
| 104 | + p=ch; |
| 105 | + } |
| 106 | + } |
| 107 | + return p; |
| 108 | + } |
| 109 | + |
| 110 | + public int min() |
| 111 | + { |
| 112 | + if(isEmpty()) |
| 113 | + throw new NullPointerException("Tree is empty"); |
| 114 | + return min(root).info; |
| 115 | + } |
| 116 | + |
| 117 | + private TreeNode min(TreeNode p) |
| 118 | + { |
| 119 | + if(p.lchild==null) |
| 120 | + return p; |
| 121 | + return min(p.lchild); |
| 122 | + } |
| 123 | + |
| 124 | + public int max() |
| 125 | + { |
| 126 | + if(isEmpty()) |
| 127 | + throw new NullPointerException("Tree is empty"); |
| 128 | + return max(root).info; |
| 129 | + } |
| 130 | + |
| 131 | + private TreeNode max(TreeNode p) |
| 132 | + { |
| 133 | + if(p.rchild==null) |
| 134 | + return p; |
| 135 | + return max(p.rchild); |
| 136 | + } |
| 137 | + |
| 138 | + public void display() |
| 139 | + { |
| 140 | + display(root,0); |
| 141 | + System.out.println(); |
| 142 | + } |
| 143 | + private void display(TreeNode p,int level) |
| 144 | + { |
| 145 | + int i; |
| 146 | + if(p==null) |
| 147 | + return; |
| 148 | + |
| 149 | + display(p.rchild, level+1); |
| 150 | + System.out.println(); |
| 151 | + |
| 152 | + for(i=0; i<level; i++) |
| 153 | + System.out.print(" "); |
| 154 | + System.out.print(p.info); |
| 155 | + |
| 156 | + display(p.lchild, level+1); |
| 157 | + }/*End of display()*/ |
| 158 | + |
| 159 | + public void preorder() |
| 160 | + { |
| 161 | + preorder(root); |
| 162 | + System.out.println(); |
| 163 | + } |
| 164 | + |
| 165 | + private void preorder(TreeNode p) |
| 166 | + { |
| 167 | + if(p==null) |
| 168 | + return; |
| 169 | + System.out.print(p.info + " "); |
| 170 | + preorder(p.lchild); |
| 171 | + preorder(p.rchild); |
| 172 | + } |
| 173 | + |
| 174 | + public void inorder() |
| 175 | + { |
| 176 | + inorder(root); |
| 177 | + System.out.println(); |
| 178 | + } |
| 179 | + |
| 180 | + private void inorder(TreeNode p) |
| 181 | + { |
| 182 | + if(p==null) |
| 183 | + return; |
| 184 | + inorder(p.lchild); |
| 185 | + System.out.print(p.info + " "); |
| 186 | + inorder(p.rchild); |
| 187 | + } |
| 188 | + |
| 189 | + |
| 190 | + public void postorder() |
| 191 | + { |
| 192 | + postorder(root); |
| 193 | + System.out.println(); |
| 194 | + } |
| 195 | + |
| 196 | + private void postorder(TreeNode p) |
| 197 | + { |
| 198 | + if(p==null) |
| 199 | + return; |
| 200 | + postorder(p.lchild); |
| 201 | + postorder(p.rchild); |
| 202 | + System.out.print(p.info + " "); |
| 203 | + } |
| 204 | + |
| 205 | + public int height() |
| 206 | + { |
| 207 | + return height(root); |
| 208 | + } |
| 209 | + |
| 210 | + private int height(TreeNode p) |
| 211 | + { |
| 212 | + int hL,hR; |
| 213 | + |
| 214 | + if(p==null) |
| 215 | + return 0; |
| 216 | + |
| 217 | + hL=height(p.lchild); |
| 218 | + hR=height(p.rchild); |
| 219 | + |
| 220 | + if(hL > hR) |
| 221 | + return 1+hL; |
| 222 | + else |
| 223 | + return 1+hR; |
| 224 | + }/*End of height()*/ |
| 225 | + |
| 226 | + public void insert1(int x) |
| 227 | + { |
| 228 | + TreeNode temp,par,p; |
| 229 | + |
| 230 | + p=root; |
| 231 | + par=null; |
| 232 | + |
| 233 | + while(p!=null) |
| 234 | + { |
| 235 | + par=p; |
| 236 | + if(x < p.info) |
| 237 | + p=p.lchild; |
| 238 | + else if(x > p.info) |
| 239 | + p=p.rchild; |
| 240 | + else |
| 241 | + System.out.println(x + " already present in the tree"); |
| 242 | + } |
| 243 | + |
| 244 | + temp=new TreeNode(x); |
| 245 | + |
| 246 | + if(par==null) |
| 247 | + root=temp; |
| 248 | + else if(x < par.info) |
| 249 | + par.lchild=temp; |
| 250 | + else |
| 251 | + par.rchild=temp; |
| 252 | + |
| 253 | + } |
| 254 | + |
| 255 | + public boolean search1(int x) |
| 256 | + { |
| 257 | + TreeNode p = root; |
| 258 | + while(p!=null) |
| 259 | + { |
| 260 | + if(x < p.info) |
| 261 | + p=p.lchild; /*Move to left child*/ |
| 262 | + else if(x > p.info) |
| 263 | + p=p.rchild; /*Move to right child */ |
| 264 | + else /*x found*/ |
| 265 | + return true; |
| 266 | + } |
| 267 | + return false; |
| 268 | + } |
| 269 | + |
| 270 | + public void delete1(int x) |
| 271 | + { |
| 272 | + TreeNode par,p,ch,s,ps; |
| 273 | + |
| 274 | + p=root; |
| 275 | + par=null; |
| 276 | + while(p!=null) |
| 277 | + { |
| 278 | + if(x==p.info) |
| 279 | + break; |
| 280 | + par=p; |
| 281 | + if(x < p.info) |
| 282 | + p=p.lchild; |
| 283 | + else |
| 284 | + p=p.rchild; |
| 285 | + } |
| 286 | + |
| 287 | + if(p==null) |
| 288 | + System.out.println(x + " not found\n"); |
| 289 | + |
| 290 | + /*Case C: 2 children*/ |
| 291 | + /*Find inorder successor and its parent*/ |
| 292 | + if(p.lchild!=null && p.rchild!=null) |
| 293 | + { |
| 294 | + ps=p; |
| 295 | + s=p.rchild; |
| 296 | + while(s.lchild!=null) |
| 297 | + { |
| 298 | + ps=s; |
| 299 | + s=s.lchild; |
| 300 | + } |
| 301 | + p.info=s.info; |
| 302 | + p=s; |
| 303 | + par=ps; |
| 304 | + } |
| 305 | + |
| 306 | + /*Case B and Case A : 1 or no child*/ |
| 307 | + if(p.lchild!=null) /*TreeNode to be deleted has left child */ |
| 308 | + ch=p.lchild; |
| 309 | + else /*TreeNode to be deleted has right child or no child*/ |
| 310 | + ch=p.rchild; |
| 311 | + |
| 312 | + if(par==null) /*TreeNode to be deleted is root TreeNode*/ |
| 313 | + root=ch; |
| 314 | + else if(p==par.lchild)/*TreeNode is left child of its parent*/ |
| 315 | + par.lchild=ch; |
| 316 | + else /*TreeNode is right child of its parent*/ |
| 317 | + par.rchild=ch; |
| 318 | + } |
| 319 | + |
| 320 | + int min1() |
| 321 | + { |
| 322 | + if(isEmpty()) |
| 323 | + { |
| 324 | + System.out.println("Tree is empty"); |
| 325 | + return -1; |
| 326 | + } |
| 327 | + TreeNode p=root; |
| 328 | + while(p.lchild!=null) |
| 329 | + p=p.lchild; |
| 330 | + return p.info; |
| 331 | + } |
| 332 | + |
| 333 | + int max1() |
| 334 | + { |
| 335 | + if(isEmpty()) |
| 336 | + { |
| 337 | + System.out.println("Tree is empty"); |
| 338 | + return -1; |
| 339 | + } |
| 340 | + TreeNode p=root; |
| 341 | + while(p.rchild!=null) |
| 342 | + p=p.rchild; |
| 343 | + return p.info; |
| 344 | + } |
| 345 | + |
| 346 | + |
| 347 | + |
| 348 | +} |
0 commit comments