|
| 1 | + |
| 2 | +public class deleteNode { |
| 3 | + public static void main(String[] args) { |
| 4 | + BinarySearchTree tree = new BinarySearchTree(); |
| 5 | + |
| 6 | + tree.insertData(10); |
| 7 | + tree.insertData(20); |
| 8 | + tree.insertData(30); |
| 9 | + tree.insertData(40); |
| 10 | + tree.insertData(6); |
| 11 | + |
| 12 | + System.out.println("Inorder traversal of the binary search tree:"); |
| 13 | + tree.inorder(); |
| 14 | + System.out.println(); |
| 15 | + |
| 16 | + |
| 17 | + //delete a node |
| 18 | + int deleteValue = 20; |
| 19 | + tree.root = tree.deleteNode(tree.root, deleteValue); |
| 20 | + System.out.println("Inorder traversal after deleting " + deleteValue + ":"); |
| 21 | + tree.inorder(); |
| 22 | + |
| 23 | + System.out.println(); |
| 24 | + } |
| 25 | + |
| 26 | + // node |
| 27 | + static class Node { |
| 28 | + int data; |
| 29 | + Node left; |
| 30 | + Node right; |
| 31 | + |
| 32 | + public Node(int data) { |
| 33 | + this.data = data; |
| 34 | + |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // binary search tree |
| 39 | + |
| 40 | + public static class BinarySearchTree { |
| 41 | + static Node root; |
| 42 | + |
| 43 | + public static void insertData(int data) { |
| 44 | + root = insertRec(root, data); |
| 45 | + } |
| 46 | + |
| 47 | + public static Node insertRec(Node root, int data) { |
| 48 | + if (root == null) { |
| 49 | + root = new Node(data); // create the root first. |
| 50 | + } else if (data < root.data) { |
| 51 | + root.left = insertRec(root.left, data); |
| 52 | + } else if (data > root.data) { |
| 53 | + root.right = insertRec(root.right, data); |
| 54 | + } |
| 55 | + |
| 56 | + return root; |
| 57 | + } |
| 58 | + |
| 59 | + public void inorder() { |
| 60 | + inorderRec(root); |
| 61 | + } |
| 62 | + |
| 63 | + public static void inorderRec(Node root) { |
| 64 | + if (root == null) { |
| 65 | + return; |
| 66 | + } else { |
| 67 | + inorderRec(root.left); |
| 68 | + System.out.print(root.data + " "); |
| 69 | + inorderRec(root.right); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + // Delete the node operation |
| 75 | + public Node deleteNode(Node root, int key) { |
| 76 | + if (root == null) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + // traverse to the node first . |
| 80 | + if (key < root.data) { |
| 81 | + root.left = deleteNode(root.left, key); |
| 82 | + } else if (key > root.data) { |
| 83 | + root.right = deleteNode(root.right, key); |
| 84 | + } else { |
| 85 | + |
| 86 | + // case 1 -> Node has no child |
| 87 | + if (root.left == null && root.right == null) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + // case 2 -> Node has only one child |
| 92 | + else if (root.left == null) { |
| 93 | + return root.right; |
| 94 | + } else if (root.right == null) { |
| 95 | + return root.left; |
| 96 | + } |
| 97 | + |
| 98 | + // case 3 -> Node has two child |
| 99 | + // minimum value in right side |
| 100 | + |
| 101 | + int minValue = findMin(root.right); |
| 102 | + root.data = minValue; |
| 103 | + |
| 104 | + root.right = deleteNode(root.right, minValue); |
| 105 | + } |
| 106 | + return root; |
| 107 | + } |
| 108 | + |
| 109 | + // helper fun For find the minimum value in the case 3 |
| 110 | + public int findMin(Node node) { |
| 111 | + int min = node.data; |
| 112 | + while (node.left != null) { |
| 113 | + min = node.left.data; |
| 114 | + node = node.left; |
| 115 | + } |
| 116 | + return min; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | +} |
0 commit comments