|
| 1 | +package datastructures; |
| 2 | + |
| 3 | +public class BinarySearchTree { |
| 4 | + |
| 5 | + public static void main(String[] args) { |
| 6 | + BinarySearchTree bst = new BinarySearchTree(); |
| 7 | + |
| 8 | + Tree root = bst.insert(6); |
| 9 | + bst.insert(10); |
| 10 | + bst.insert(8); |
| 11 | + bst.insert(7); |
| 12 | + bst.insert(9); |
| 13 | + bst.insert(4); |
| 14 | + bst.insert(5); |
| 15 | + bst.insert(3); |
| 16 | + bst.traverse(BinarySearchTree.IN_ORDER); |
| 17 | + bst.traverse(BinarySearchTree.POST_ORDER); |
| 18 | + out("\n"); |
| 19 | + //out(findMinimum(root).data + "\n"); |
| 20 | + //out(findMaximum(root).data + "\n"); |
| 21 | + //out(search(root, 5).data + "\n"); |
| 22 | + //out(search(root, 7).data + "\n"); |
| 23 | + bst.delete(6); |
| 24 | + bst.traverse(BinarySearchTree.IN_ORDER); |
| 25 | + out("\n"); |
| 26 | + bst.delete(5); |
| 27 | + bst.traverse(BinarySearchTree.IN_ORDER); |
| 28 | + out("\n"); |
| 29 | + bst.delete(4); |
| 30 | + bst.traverse(BinarySearchTree.IN_ORDER); |
| 31 | + out("\n"); |
| 32 | + bst.delete(10); |
| 33 | + bst.traverse(BinarySearchTree.IN_ORDER); |
| 34 | + out("\n"); |
| 35 | + } |
| 36 | + |
| 37 | + public static final int IN_ORDER = 1; |
| 38 | + public static final int PRE_ORDER = 2; |
| 39 | + public static final int POST_ORDER = 3; |
| 40 | + |
| 41 | + private Tree root; |
| 42 | + |
| 43 | + /* O(h) where h is height of the tree */ |
| 44 | + public Tree search(Tree node, int x) { |
| 45 | + if (root == null) return null; |
| 46 | + if (x == root.data) return root; |
| 47 | + if (x < root.data) return search(root.left, x); |
| 48 | + else return search(root.right, x); |
| 49 | + } |
| 50 | + |
| 51 | + public Tree findMinimum() { |
| 52 | + if (root == null) return null; |
| 53 | + Tree min = root; |
| 54 | + while (min.left != null) { |
| 55 | + min = min.left; |
| 56 | + } |
| 57 | + return min; |
| 58 | + } |
| 59 | + |
| 60 | + public Tree findMaximum() { |
| 61 | + if (root == null) return null; |
| 62 | + Tree max = root; |
| 63 | + while (max.right != null) { |
| 64 | + max = max.right; |
| 65 | + } |
| 66 | + return max; |
| 67 | + } |
| 68 | + |
| 69 | + public void traverse(int order) { |
| 70 | + if (root == null) return; |
| 71 | + traverse(root, order); |
| 72 | + } |
| 73 | + |
| 74 | + private void traverse(Tree node, int order) { |
| 75 | + if (root == null) return; |
| 76 | + if (order == PRE_ORDER) processNode(root); |
| 77 | + traverse(root.left, order); |
| 78 | + if (order == IN_ORDER) processNode(root); |
| 79 | + traverse(root.right, order); |
| 80 | + if (order == POST_ORDER) processNode(root); |
| 81 | + } |
| 82 | + |
| 83 | + /* left most child in right subtree */ |
| 84 | + public Tree successor(Tree node) { |
| 85 | + if (node == null || node.right == null) return null; |
| 86 | + Tree tmp = node.right; |
| 87 | + while (tmp.left != null) tmp = tmp.left; |
| 88 | + return tmp; |
| 89 | + } |
| 90 | + |
| 91 | + /* right most child in left subtree */ |
| 92 | + public Tree predecessor(Tree node) { |
| 93 | + if (node == null || node.left == null) return null; |
| 94 | + Tree tmp = node.left; |
| 95 | + while (tmp.right != null) tmp = tmp.right; |
| 96 | + return tmp.right; |
| 97 | + } |
| 98 | + |
| 99 | + public Tree insert(int x) { |
| 100 | + return insert(root, x, null); |
| 101 | + } |
| 102 | + |
| 103 | + public Tree insert(Tree node, int x, Tree parent) { |
| 104 | + if (root == null) { |
| 105 | + Tree tree = new Tree(x); |
| 106 | + tree.left = tree.right = null; |
| 107 | + tree.parent = parent; |
| 108 | + if (parent != null) { |
| 109 | + if (x < parent.data) parent.left = tree; |
| 110 | + else parent.right = tree; |
| 111 | + } |
| 112 | + root = tree; |
| 113 | + return tree; |
| 114 | + } |
| 115 | + if (x < root.data) return insert(root.left, x, root); |
| 116 | + else return insert(root.right, x, root); |
| 117 | + } |
| 118 | + |
| 119 | + public boolean delete(int x) { |
| 120 | + Tree node = search(root, x); |
| 121 | + if (node == null) return false; |
| 122 | + Tree parent = node.parent; |
| 123 | + Tree delNode = null; |
| 124 | + |
| 125 | + if (parent == null) { /* root node */ |
| 126 | + if (node.left == null && node.right == null) { /* root only tree */ |
| 127 | + root = null; |
| 128 | + return true; |
| 129 | + } else { |
| 130 | + if (node.left != null) delNode = predecessor(node); |
| 131 | + else delNode = successor(node); |
| 132 | + } |
| 133 | + } else { /* non root node */ |
| 134 | + if (node.left != null && node.right != null) { /* both left and right children exist */ |
| 135 | + delNode = successor(node); |
| 136 | + } else if (node.left != null || node.right != null) { /* only a single child exists */ |
| 137 | + if (node.left != null) delNode = node.left; |
| 138 | + else delNode = node.right; |
| 139 | + } else { /* no children */ |
| 140 | + |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + delete(delNode.data); |
| 145 | + node.data = delNode.data; |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + private static void processNode(Tree tree) { |
| 150 | + out(tree.data + " "); |
| 151 | + } |
| 152 | + |
| 153 | + private static void out(String str) { |
| 154 | + System.out.print(str); |
| 155 | + } |
| 156 | +} |
0 commit comments