1 public class BST<E> implements Tree<E> {
2 protected TreeNode<E> root;
3 protected int size = 0;
4 protected java.util.Comparator<E> c;
5
6
7 public BST() {
8 this.c = (e1, e2) -> ((Comparable<E>)e1).compareTo(e2);
9 }
10
11
12 public BST(java.util.Comparator<E> c) {
13 this.c = c;
14 }
15
16
17 public BST(E[] objects) {
18 this.c = (e1, e2) -> ((Comparable<E>)e1).compareTo(e2);
19 for (int i = 0; i < objects.length; i++)
20 add(objects[i]);
21 }
22
23 @Override
24 public boolean search(E e) {
25 TreeNode<E> current = root;
26
27 while (current != null) {
28 if (c.compare(e, current.element) < 0) {
29 current = current.left;
30 }
31 else if (c.compare(e, current.element) > 0) {
32 current = current.right;
33 }
34 else
35 return true;
36 }
37
38 return false;
39 }
40
41 @Override
43 public boolean insert(E e) {
44 if (root == null)
45 root = createNewNode(e);
46 else {
47
48 TreeNode<E> parent = null;
49 TreeNode<E> current = root;
50 while (current != null)
51 if (c.compare(e, current.element) < 0) {
52 parent = current;
53 current = current.left;
54 }
55 else if (c.compare(e, current.element) > 0) {
56 parent = current;
57 current = current.right;
58 }
59 else
60 return false;
61
62
63 if (c.compare(e, parent.element) < 0)
64 parent.left = createNewNode(e);
65 else
66 parent.right = createNewNode(e);
67 }
68
69 size++;
70 return true;
71 }
72
73 protected TreeNode<E> createNewNode(E e) {
74 return new TreeNode<>(e);
75 }
76
77 @Override
78 public void inorder() {
79 inorder(root);
80 }
81
82
83 protected void inorder(TreeNode<E> root) {
84 if (root == null) return;
85 inorder(root.left);
86 System.out.print(root.element + " ");
87 inorder(root.right);
88 }
89
90 @Override
91 public void postorder() {
92 postorder(root);
93 }
94
95
96 protected void postorder(TreeNode<E> root) {
97 if (root == null) return;
98 postorder(root.left);
99 postorder(root.right);
100 System.out.print(root.element + " ");
101 }
102
103 @Override
104 public void preorder() {
105 preorder(root);
106 }
107
108
109 protected void preorder(TreeNode<E> root) {
110 if (root == null) return;
111 System.out.print(root.element + " ");
112 preorder(root.left);
113 preorder(root.right);
114 }
115
116
118 public static class TreeNode<E> {
119 protected E element;
120 protected TreeNode<E> left;
121 protected TreeNode<E> right;
122
123 public TreeNode(E e) {
124 element = e;
125 }
126 }
127
128 @Override
129 public int getSize() {
130 return size;
131 }
132
133
134 public TreeNode<E> getRoot() {
135 return root;
136 }
137
138
139 public java.util.ArrayList<TreeNode<E>> path(E e) {
140 java.util.ArrayList<TreeNode<E>> list =
141 new java.util.ArrayList<>();
142 TreeNode<E> current = root;
143
144 while (current != null) {
145 list.add(current);
146 if (c.compare(e, current.element) < 0) {
147 current = current.left;
148 }
149 else if (c.compare(e, current.element) > 0) {
150 current = current.right;
151 }
152 else
153 break;
154 }
155
156 return list;
157 }
158
159 @Override
162 public boolean delete(E e) {
163
164 TreeNode<E> parent = null;
165 TreeNode<E> current = root;
166 while (current != null) {
167 if (c.compare(e, current.element) < 0) {
168 parent = current;
169 current = current.left;
170 }
171 else if (c.compare(e, current.element) > 0) {
172 parent = current;
173 current = current.right;
174 }
175 else
176 break;
177 }
178
179 if (current == null)
180 return false;
181
182
183 if (current.left == null) {
184
185 if (parent == null) {
186 root = current.right;
187 }
188 else {
189 if (c.compare(e, parent.element) < 0)
190 parent.left = current.right;
191 else
192 parent.right = current.right;
193 }
194 }
195 else {
196
197
198
199 TreeNode<E> parentOfRightMost = current;
200 TreeNode<E> rightMost = current.left;
201
202 while (rightMost.right != null) {
203 parentOfRightMost = rightMost;
204 rightMost = rightMost.right;
205 }
206
207
208 current.element = rightMost.element;
209
210
211 if (parentOfRightMost.right == rightMost)
212 parentOfRightMost.right = rightMost.left;
213 else
214
215 parentOfRightMost.left = rightMost.left;
216 }
217
218 size--;
219 return true;
220 }
221
222 @Override
223 public java.util.Iterator<E> iterator() {
224 return new InorderIterator();
225 }
226
227
228 private class InorderIterator implements java.util.Iterator<E> {
229
230 private java.util.ArrayList<E> list =
231 new java.util.ArrayList<>();
232 private int current = 0;
233
234 public InorderIterator() {
235 inorder();
236 }
237
238
239 private void inorder() {
240 inorder(root);
241 }
242
243
244 private void inorder(TreeNode<E> root) {
245 if (root == null) return;
246 inorder(root.left);
247 list.add(root.element);
248 inorder(root.right);
249 }
250
251 @Override
252 public boolean hasNext() {
253 if (current < list.size())
254 return true;
255
256 return false;
257 }
258
259 @Override
260 public E next() {
261 return list.get(current++);
262 }
263
264 @Override
265 public void remove() {
266 if (current == 0)
267 throw new IllegalStateException();
268
269 delete(list.get(--current));
270 list.clear();
271 inorder();
272 }
273 }
274
275 @Override
276 public void clear() {
277 root = null;
278 size = 0;
279 }
280 }