|
| 1 | +package Java_DSA.Tree; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class BST { |
| 6 | + private static Node root; |
| 7 | + |
| 8 | + private static class Node{ |
| 9 | + int data; |
| 10 | + Node left; |
| 11 | + Node right; |
| 12 | + |
| 13 | + public Node(int data){ |
| 14 | + this.data = data; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public static Node insert(Node root,int val){ |
| 19 | + Node temp = new Node(val); |
| 20 | + if(root == null){ |
| 21 | + root = temp; |
| 22 | + return root; |
| 23 | + } |
| 24 | + if(val < root.data){ |
| 25 | + root.left = insert(root.left,val); |
| 26 | + } |
| 27 | + else{ |
| 28 | + root.right = insert(root.right,val); |
| 29 | + } |
| 30 | + |
| 31 | + return root; |
| 32 | + } |
| 33 | + |
| 34 | + public static void insert(int val){ |
| 35 | + Node p = root; |
| 36 | + Node ptr = null; |
| 37 | + while(p != null){ |
| 38 | + ptr = p; |
| 39 | + if(val < p.data){ |
| 40 | + p = p.left; |
| 41 | + } |
| 42 | + else if(val > p.data){ |
| 43 | + p = p.right; |
| 44 | + } |
| 45 | + else{ |
| 46 | + System.out.println(val +" is already present in tree."); |
| 47 | + return; |
| 48 | + } |
| 49 | + } |
| 50 | + Node temp = new Node(val); |
| 51 | + |
| 52 | + if(ptr == null) |
| 53 | + root = temp; |
| 54 | + else if(val < ptr.data) |
| 55 | + ptr.left = temp; |
| 56 | + else |
| 57 | + ptr.right = temp; |
| 58 | + } |
| 59 | + |
| 60 | + public static void inorder(Node root){ |
| 61 | + if(root == null){ |
| 62 | + return; |
| 63 | + } |
| 64 | + inorder(root.left); |
| 65 | + System.out.print(root.data + " "); |
| 66 | + inorder(root.right); |
| 67 | + } |
| 68 | + |
| 69 | + public static boolean search(Node root,int key){ |
| 70 | + if(root == null){ |
| 71 | + return false; |
| 72 | + } |
| 73 | + if(key < root.data){ |
| 74 | + return search(root.left,key); |
| 75 | + } |
| 76 | + else if(key == root.data){ |
| 77 | + return true; |
| 78 | + } |
| 79 | + else{ |
| 80 | + return search(root.right,key); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static Node delete(Node root,int val){ |
| 85 | + if(search(root,val)) { |
| 86 | + if (val < root.data) { |
| 87 | + root.left = delete(root.left, val); |
| 88 | + } else if (val > root.data) { |
| 89 | + root.right = delete(root.right, val); |
| 90 | + } else { //root.data == val |
| 91 | + if (root.left == null && root.right == null) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + if (root.left == null) |
| 95 | + return root.right; |
| 96 | + else if (root.right == null) |
| 97 | + return root.left; |
| 98 | + else { |
| 99 | + Node IS = inorderSuccesser(root.right); |
| 100 | + root.data = IS.data; |
| 101 | + root.right = delete(root.right, IS.data); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + System.out.println("Data is not present to delete."); |
| 107 | + |
| 108 | + return root; |
| 109 | + } |
| 110 | + |
| 111 | + public static Node inorderSuccesser(Node root){ |
| 112 | + while(root.left != null){ |
| 113 | + root = root.left; |
| 114 | + } |
| 115 | + return root; |
| 116 | + } |
| 117 | + |
| 118 | + public static int countNode(Node root){ |
| 119 | + if(root == null){ |
| 120 | + return 0; |
| 121 | + } |
| 122 | + int leftNode = countNode(root.left); |
| 123 | + int rightNode = countNode(root.right); |
| 124 | + return leftNode + rightNode + 1; |
| 125 | + } |
| 126 | + |
| 127 | + public static void main(String[] args) { |
| 128 | + Scanner sc = new Scanner(System.in); |
| 129 | + //Node root = null; |
| 130 | + boolean flag = true; |
| 131 | + System.out.println("1)Insert data.\n2)Search.\n3)Delete.\n4)Display inorder.\n5)Count the nodes.\n6)Exit."); |
| 132 | + while(flag){ |
| 133 | + System.out.println("Enter your choice : "); |
| 134 | + int choice = sc.nextInt(); |
| 135 | + switch (choice){ |
| 136 | + case 1->{ |
| 137 | + System.out.println("Enter data to insert : "); |
| 138 | + int data = sc.nextInt(); |
| 139 | + //root = insert(root,data); |
| 140 | + insert(data); |
| 141 | + } |
| 142 | + case 2->{ |
| 143 | + System.out.println("Enter data to search : "); |
| 144 | + int searchValue = sc.nextInt(); |
| 145 | + if(search(root,searchValue)) |
| 146 | + System.out.println("Found"); |
| 147 | + else |
| 148 | + System.out.println("Not found"); |
| 149 | + } |
| 150 | + case 3->{ |
| 151 | + System.out.println("Enter data to delete : "); |
| 152 | + int dataToDelete = sc.nextInt(); |
| 153 | + root = delete(root,dataToDelete); |
| 154 | + } |
| 155 | + case 4->{ |
| 156 | + inorder(root); |
| 157 | + System.out.println(); |
| 158 | + } |
| 159 | + case 5->{ |
| 160 | + System.out.println("Number of nodes : "+countNode(root)); |
| 161 | + System.out.println(); |
| 162 | + } |
| 163 | + case 6->{ |
| 164 | + flag = false; |
| 165 | + } |
| 166 | + default -> { |
| 167 | + System.out.println("Invalid choice..!"); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments