|  | 
|  | 1 | +// binary search tree creation, new node insertion , and node deletion | 
|  | 2 | +import java.util.*; | 
|  | 3 | + | 
|  | 4 | +class bstree{ | 
|  | 5 | + //node structure | 
|  | 6 | + nodes headp; | 
|  | 7 | + static class nodes{ | 
|  | 8 | + nodes left; | 
|  | 9 | + int value; | 
|  | 10 | + nodes right; | 
|  | 11 | + } | 
|  | 12 | + //bst creation | 
|  | 13 | + void bstreec(){ | 
|  | 14 | + int size; | 
|  | 15 | + nodes duph=headp; | 
|  | 16 | + Scanner in= new Scanner(System.in); | 
|  | 17 | + System.out.println("enter the no of nodes you want to insert"); | 
|  | 18 | + int no=in.nextInt(); //no of nodes asked  | 
|  | 19 | + for(int i=0;i<no;i++) | 
|  | 20 | + { | 
|  | 21 | + if(headp == null){ //if our root is null first create root node | 
|  | 22 | + nodes obj = new nodes (); | 
|  | 23 | + System.out.println("enter the root node");  | 
|  | 24 | + int value=in.nextInt(); | 
|  | 25 | + obj.value=value; | 
|  | 26 | + headp=obj; | 
|  | 27 | + duph=headp; | 
|  | 28 | + | 
|  | 29 | + } // if root is assigned or had been created then create futher | 
|  | 30 | + else{ | 
|  | 31 | + duph=headp; | 
|  | 32 | + System.out.println("enter next node"); | 
|  | 33 | + int value=in.nextInt(); | 
|  | 34 | + nodes obj = new nodes(); | 
|  | 35 | + obj.value=value; | 
|  | 36 | + while(duph != null){ //traversing the tree from root node till we find the position to insert the given node and inserting it | 
|  | 37 | + if(value < duph.value && duph.left != null){ | 
|  | 38 | + duph=duph.left; | 
|  | 39 | + } | 
|  | 40 | + else if(value >duph.value && duph.right != null){ | 
|  | 41 | + duph=duph.right; | 
|  | 42 | + } | 
|  | 43 | + else if( value <duph.value){ | 
|  | 44 | + duph.left=obj; | 
|  | 45 | + duph=duph.left; // now going on my newly node which is inserted | 
|  | 46 | + duph=duph.left; // going to left of my newly node(it dosent matter you can go right also) | 
|  | 47 | + } | 
|  | 48 | + else{ | 
|  | 49 | + duph.right=obj; | 
|  | 50 | + duph=duph.right; // now going on my newly node which is inserted | 
|  | 51 | + duph=duph.right; // going to right of my newly node(it dosent matter you can go left also) | 
|  | 52 | + } | 
|  | 53 | + } | 
|  | 54 | + | 
|  | 55 | + } | 
|  | 56 | +  | 
|  | 57 | + } | 
|  | 58 | +  | 
|  | 59 | + } | 
|  | 60 | + | 
|  | 61 | + //insertion of node | 
|  | 62 | + void insert(int value){ //taking the value | 
|  | 63 | + nodes duph=headp;  | 
|  | 64 | + nodes obj = new nodes(); | 
|  | 65 | + obj.value=value; | 
|  | 66 | + while(duph != null){ //travrsing the whol | 
|  | 67 | + if(value < duph.value && duph.left != null){ | 
|  | 68 | + duph=duph.left; | 
|  | 69 | + } | 
|  | 70 | + else if(value >duph.value && duph.right != null){ | 
|  | 71 | + duph=duph.right; | 
|  | 72 | + } | 
|  | 73 | + else if( value <duph.value){ | 
|  | 74 | + duph.left=obj; | 
|  | 75 | + duph=duph.left; //same explanation as explained above | 
|  | 76 | + duph=duph.left; //same explanation as explained above  | 
|  | 77 | + } | 
|  | 78 | + else{ | 
|  | 79 | + duph.right=obj; | 
|  | 80 | + duph=duph.right;  | 
|  | 81 | + duph=duph.right; | 
|  | 82 | +  | 
|  | 83 | + } | 
|  | 84 | + } | 
|  | 85 | + } | 
|  | 86 | +  | 
|  | 87 | + | 
|  | 88 | + // Get minimum element in binary search tree | 
|  | 89 | +public nodes minimumElement(nodes root) { | 
|  | 90 | +if (root.left == null) | 
|  | 91 | +return root; | 
|  | 92 | +else { | 
|  | 93 | +return minimumElement(root.left); | 
|  | 94 | +} | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | + | 
|  | 98 | + public nodes deleteNode(nodes root, int value) { | 
|  | 99 | +if (root == null) | 
|  | 100 | +return null; | 
|  | 101 | +if (root.value > value) { | 
|  | 102 | +root.left = deleteNode(root.left, value); | 
|  | 103 | +} else if (root.value < value) { | 
|  | 104 | +root.right = deleteNode(root.right, value); | 
|  | 105 | +  | 
|  | 106 | +} else { | 
|  | 107 | +// if nodeToBeDeleted have both children | 
|  | 108 | +if (root.left != null && root.right != null) { | 
|  | 109 | +nodes temp = root; | 
|  | 110 | +// Finding minimum element from right | 
|  | 111 | +nodes minNodeForRight = minimumElement(temp.right); | 
|  | 112 | +// Replacing current node with minimum node from right subtree | 
|  | 113 | +root.value = minNodeForRight.value; | 
|  | 114 | +// Deleting minimum node from right now | 
|  | 115 | +root.right = deleteNode(root.right, minNodeForRight.value); | 
|  | 116 | +  | 
|  | 117 | +} | 
|  | 118 | +// if nodeToBeDeleted has only left child | 
|  | 119 | +else if (root.left != null) { | 
|  | 120 | +root = root.left; | 
|  | 121 | +} | 
|  | 122 | +// if nodeToBeDeleted has only right child | 
|  | 123 | +else if (root.right != null) { | 
|  | 124 | +root = root.right; | 
|  | 125 | +} | 
|  | 126 | +// if nodeToBeDeleted do not have child (Leaf node) | 
|  | 127 | +else | 
|  | 128 | +root = null; | 
|  | 129 | +} | 
|  | 130 | +return root; | 
|  | 131 | +} | 
|  | 132 | + | 
|  | 133 | + public void treet(nodes poi){  | 
|  | 134 | + //recursive  | 
|  | 135 | + //tree traversal | 
|  | 136 | + if(poi != null){ | 
|  | 137 | + System.out.println(poi.value);  | 
|  | 138 | + treet(poi.left);  | 
|  | 139 | + treet(poi.right); | 
|  | 140 | + } | 
|  | 141 | + | 
|  | 142 | + } | 
|  | 143 | +} | 
|  | 144 | +public class binary_search_tree{ | 
|  | 145 | + public static void main(String [] arrrr){ | 
|  | 146 | + Scanner wow = new Scanner(System.in); | 
|  | 147 | + //creting object  | 
|  | 148 | + bstree obj = new bstree(); | 
|  | 149 | + obj.bstreec(); | 
|  | 150 | +  | 
|  | 151 | + System.out.println("tree :-"); | 
|  | 152 | + obj.treet(obj.headp); //display of tree | 
|  | 153 | + | 
|  | 154 | + //calling to insert | 
|  | 155 | + System.out.println("enter the value you wanna insert"); | 
|  | 156 | + int ni=wow.nextInt(); | 
|  | 157 | + obj.insert(ni); | 
|  | 158 | + | 
|  | 159 | + //calling for deletion of node | 
|  | 160 | + System.out.println("enter the value you wanna delete"); | 
|  | 161 | + int val=wow.nextInt(); | 
|  | 162 | + obj.deleteNode(obj.headp,val); | 
|  | 163 | + | 
|  | 164 | + System.out.println("tree afterd:-"); | 
|  | 165 | + obj.treet(obj.headp); //display of tree after deleting the node | 
|  | 166 | + | 
|  | 167 | + } | 
|  | 168 | +} | 
0 commit comments