|
12 | 12 | * To change this template go to Preferences | IDE Settings | File and Code Templates
|
13 | 13 | */
|
14 | 14 | public class BinarySearchTree<E extends Comparable<E>> extends BinaryTree<E> {
|
15 |
| - public static void main(String[] a) { |
16 |
| - BinarySearchTree<Integer> bst = new BinarySearchTree<>(); |
17 |
| - bst.put(6); |
18 |
| - bst.put(3); |
19 |
| - bst.put(5); |
20 |
| - bst.put(7); |
21 |
| - bst.put(8); |
22 |
| - bst.put(9); |
23 |
| - /*bst.put(12); |
24 |
| - bst.put(10); |
25 |
| - bst.put(16); |
26 |
| - bst.put(14); |
27 |
| - bst.put(25); |
28 |
| - bst.put(15); |
29 |
| - bst.put(20); |
30 |
| - bst.put(35); |
31 |
| - bst.put(23); |
32 |
| - bst.put(22); |
33 |
| - bst.put(21); |
34 |
| - bst.put(45); |
35 |
| - bst.put(40); |
36 |
| - bst.put(56); |
37 |
| - bst.put(65); |
38 |
| - bst.put(75); |
39 |
| - bst.put(85);*/ |
40 |
| - bst.preOrder(); |
41 |
| - out.println(""); |
42 |
| - bst.inOrder(); |
43 |
| - out.println(""); |
44 |
| - bst.postOrder(); |
45 |
| - out.println("\n" + bst.size()); |
46 |
| - out.println(bst.height()); |
47 |
| - /*obj.delete(); |
48 |
| - out.println("After deletion: "); |
49 |
| - obj.postOrder();*/ |
50 |
| - out.print("\nIn Order: "); |
51 |
| - bst.inOrder(); |
52 |
| - /*out.println("\nAfter mirroring: "); |
53 |
| - obj.mirror(); |
54 |
| - obj.inOrder();*/ |
55 |
| - out.println("LCA: " + bst.leastCommonAncestor(bst.root, 6, 8).value); |
56 |
| - out.println("Min: " + bst.min().value); |
57 |
| - out.println("BFT: "); |
58 |
| - bst.breadthFirstTraversal(); |
59 |
| - out.println("\nBFT using queue: "); |
60 |
| - bst.breadthFirstTraversalUsingQueue(); |
61 |
| - out.println("Is BST: " + bst.isBST()); |
62 |
| - /*out.print("Tree to list: "); |
63 |
| - bst.treeToList();*/ |
64 |
| - out.print("\nIs height balanced: " + bst.isHeightBalanced()); |
65 |
| - out.print("\nDiameter: " + bst.diameter()); |
66 |
| - } |
67 |
| - |
68 | 15 |
|
69 | 16 | /**
|
70 | 17 | * Inserts a node into the BST.
|
@@ -117,39 +64,6 @@ public BinaryNode<E> min(BinaryNode<E> node) {
|
117 | 64 | }
|
118 | 65 | }
|
119 | 66 |
|
120 |
| - |
121 |
| - /** |
122 |
| - * Determines the LCA for a BST |
123 |
| - * <p/> |
124 |
| - * DEFINITION OF LCA: |
125 |
| - * Let T be a rooted tree. The lowest |
126 |
| - * common ancestor between two nodes n1 and |
127 |
| - * n2 is defined as the lowest node in T that has |
128 |
| - * both n1 and n2 as descendants (where we allow |
129 |
| - * a node to be a descendant of itself). |
130 |
| - */ |
131 |
| - public void leastCommonAncestor() { |
132 |
| - /*int value1, value2; |
133 |
| - Scanner in = new Scanner(System.in); |
134 |
| - out.println("Enter value 1: "); |
135 |
| - value1 = (E) Integer.valueOf(in.nextLine()); |
136 |
| - out.println("Enter value 1: "); |
137 |
| - value2 = (E) in.nextLine(); |
138 |
| - out.println("LCA of " + value1 + " and " + value2 + " is: " + leastCommonAncestor(root, value1, value2).value);*/ |
139 |
| - } |
140 |
| - |
141 |
| - public BinaryNode<E> leastCommonAncestor(BinaryNode<E> node, E value1, E value2) { |
142 |
| - if (node == null || value1.compareTo(value2) > 0) throw new NoSuchElementException(); |
143 |
| - |
144 |
| - if (value1.compareTo(node.value) <= 0 && value2.compareTo(node.value) >= 0) { |
145 |
| - return node; |
146 |
| - } else if (value1.compareTo(node.value) > 0 && value2.compareTo(node.value) > 0) { |
147 |
| - return leastCommonAncestor(node.right, value1, value2); |
148 |
| - } else { |
149 |
| - return leastCommonAncestor(node.left, value1, value2); |
150 |
| - } |
151 |
| - } |
152 |
| - |
153 | 67 | public void printList(BinaryNode<E> node) {
|
154 | 68 | BinaryNode<E> current = node;
|
155 | 69 | out.print("[");
|
|
0 commit comments