-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
312 lines (238 loc) · 6.26 KB
/
BinarySearchTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import java.util.LinkedList;
// answers: https://www.programcreek.com/2012/12/leetcode-validate-binary-search-tree-java/
// TODO: fix Comparable so more inclusive
public class BinarySearchTree <T extends Comparable<T>> {
public static void main(String[] args) {
// System.out.println("Hello world!");
// Node two = new Node(2, null, null);
// BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(two);
BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>();
// bst.insert(2);
// bst.insert(1);
// bst.insert(3);
// bst.insert(0);
// bst.insert(4);
bst.buildNonBST(7, 1, 2);
bst.printInOrder();
// System.out.println(bst.contains(3));
// System.out.println(bst.findMin());
// LinkedList<Integer> bstToLL = bst.bstToLL();
// for (Integer i : bstToLL) {
// System.out.print(i + ", ");
// }
System.out.println(bst.isBSTHackey());
}
private class Node {
T data;
Node left, right;
public Node(T data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
Node root;
public BinarySearchTree() {
this.root = null;
}
public BinarySearchTree(Node root) {
this.root = root;
}
public void buildNonBST(T _seven, T _one, T _two) {
Node seven = new Node(_seven, null, null);
Node one = new Node(_one, null, seven);
this.root = new Node(_two, one, null);
}
// source: https://algs4.cs.princeton.edu/32bst/BST.java.html
// TOOD: i dont think this works...
public boolean isBST() {
return isBST(root, null, null);
}
private boolean isBST(Node node, T min, T max) {
if (node == null) {
return true;
}
if (min != null && node.data.compareTo(min) < 0) {
return false;
}
if (max != null && node.data.compareTo(max) > 0) {
return false;
}
return isBST(node.left, min, node.data) && isBST(node.right, max, node.data);
}
public boolean isBSTHackey() {
if (root == null) {
throw new IndexOutOfBoundsException();
}
// LinkedList<T> nodes = new LinkedList<T>();
// nodes.addFirst(Integer.MIN_VALUE);
// return isBST(nodes, root);
return isBSTHackey(new LinkedList<T>(), root);
}
public boolean isBSTHackey(LinkedList<T> nodes, Node current) {
// // left node
// if (current.left == null && current.right == null) {
// T last = nodes.peekLast();
// // assume no duplicates
// if (last.data.compareTo(current.data) > 0) {
// return false;
// }
// nodes.addLast(current);
// return true;
// }
// if (current.left != null && current.right == null) {
// }
if (current == null) {
return true;
}
if (!bstCheck(nodes, current)) {
return false;
}
// return isBSTHackey(nodes, current.left) && bstCheck(nodes, current) && isBSTHackey(nodes, current.right);
return isBSTHackey(nodes, current.left) && isBSTHackey(nodes, current.right);
}
private boolean bstCheck(LinkedList<T> nodes, Node current) {
T last = nodes.peekLast();
if (nodes.size() > 0) {
// assume no duplicates
if (last.compareTo(current.data) > 0) {
return false;
}
}
nodes.addLast(current.data);
return true;
}
// public LinkedList<T> bstToLL() {
// LinkedList<T> nodes = new LinkedList<T>();
// bstToLL(nodes, root);
// return nodes;
// }
// public void bstToLL(LinkedList<T> nodes, Node node) {
// if (node == null) {
// return;
// }
// bstToLL(nodes, node.left);
// nodes.addLast(node.data);
// bstToLL(nodes, node.right);
// }
// public boolean isBinarySearchTree() {
// LinkedList<Node> nodes = new LinkedList<Node>();
// nodes.addFirst(Integer.MIN_VALUE);
// return isBinarySearchTree(nodes, root);
// }
// public void isBinarySearchTree(LinkedList<Node> nodes, Node node) {
// if (node == null) {
// return true;
// }
// // process
// return isBinarySearchTree(nodes, node.left) && nodes.getLast().data.compareTo(node.data) < 0 && isBinarySearchTree(nodes, node.right);
// nodes.addLast(node)
// // right recurse
// }
// public boolean isBinarySearchTree(Node root) {
// if (node == null) {
// return true;
// }
// if (!binarySearchCheck(node)) {
// return false;
// }
// return isBinarySearchTree(node.left) && isBinarySearchTree(node.right);
// }
// public boolean binarySearchCheck(Node node) {
// if (node.left != null) {
// // if left > parent
// if (node.left.data.compareTo(node.data) > 0) {
// return false;
// }
// }
// if (node.right != null) {
// // if right < parent
// if (node.right.data.compareTo(node.data) < 0) {
// return false
// }
// }
// return true;
// }
public void insert(T data) {
this.root = insert(root, data);
}
private Node insert(Node node, T data) {
if (node == null) {
return new Node(data, null, null);
}
int compare = data.compareTo(node.data);
if (compare < 0) {
node.left = insert(node.left, data);
} else if (compare > 0) {
node.right = insert(node.right, data);
} else {
; // duplicate-, do nothing
}
return node;
}
public void printInOrder() {
printInOrder(root);
System.out.println();
}
// inorder: left children -> parent -> right children
public void printInOrder(Node node) {
if (node == null) {
return;
}
// process left child
printInOrder(node.left);
// process current node
System.out.print(node.data + ", ");
// process right child
printInOrder(node.right);
}
public boolean contains(T data) {
return contains(root, data);
}
public boolean contains(Node node, T data) {
if (node == null) {
return false;
}
int compare = data.compareTo(node.data);
if (compare < 0) {
return contains(node.left, data);
} else if (compare > 0) {
return contains(node.right, data);
} else {
return true;
}
}
public void delete(T data) {
// TODO
}
// public T fin
// public T findMin() {
// if (root == null) {
// throw new IndexOutOfBoundsException();
// }
// Node node = root;
// while (node != null) {
// if (node.left == null) {
// return node.data;
// }
// node = node.left;
// }
// return null;
// }
public T findMin() {
if (root == null) {
throw new IndexOutOfBoundsException();
}
return findMin(root);
}
public T findMin(Node node) {
if (node.left == null) {
return node.data;
}
return findMin(node.left);
}
public Node findMax() {
// TODO
return null;
}
}