-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
254 lines (206 loc) · 5.17 KB
/
BST.java
File metadata and controls
254 lines (206 loc) · 5.17 KB
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
import java.util.ArrayList;
/**
* Binary Search Tree (sorted, unbalanced)
*/
public class BST {
private class Node {
int key; // sort key
Node left = null;
Node right = null;
Node(int key) {
this.key = key;
}
}
private Node root = null;
@Override
public String toString() {
return toString(root);
}
private String toString(Node n) {
String s = "";
if (n != null) {
s = " " + n.key;
s += toString(n.left);
s += toString(n.right);
}
return s;
}
public boolean insert(int key) {
Node n = new Node(key);
if (root == null) {
root = n;
return true;
}
boolean wentLeft = false;
Node p = root;
Node c = root;
while (c != null && c.key != key) {
p = c;
if (key < c.key) {
c = c.left;
wentLeft = true;
} else {
c = c.right;
wentLeft = false;
}
}
if (c != null && c.key == key) {
return false;
}
if (wentLeft) {
p.left = n;
} else {
p.right = n;
}
return true;
}
public boolean search(int key) {
return search(key, root);
}
private boolean search(int key, Node n) {
if (n == null) {
return false;
}
if (n.key == key) {
return true;
}
if (key < n.key) {
return search(key, n.left);
} else {
return search(key, n.right);
}
}
public ArrayList<Integer> getPath(int key) {
return getPath(key, root, new ArrayList<>());
}
public ArrayList<Integer> getPath(int key, Node n, ArrayList<Integer> path) {
if (n == null) {
return null;
}
path.add(n.key);
if (key == n.key) {
return path;
}
if (key < n.key) {
return getPath(key, n.left, path);
} else {
return getPath(key, n.right, path);
}
}
public boolean delete(int key) {
if (root == null) {
return false;
}
Node p = root;
Node c = root;
boolean wentLeft = false;
while (c != null && c.key != key) {
p = c;
if (key < c.key) {
c = c.left;
wentLeft = true;
} else {
c = c.right;
wentLeft = false;
}
}
if (c == null || c.key != key) {
return false;
}
// c.key == key
// c is the node to be deleted
if (c.left == null || c.right == null) {
// 1 leaf or no leaf node
Node n = (c.left != null)? c.left : c.right;
if (wentLeft) {
p.left = n;
} else {
p.right = n;
}
} else {
// 2 leaf nodes
// go left and then right all the way to the end
Node n = c; // n is the actual node to be deleted but we will delete c instead
p = c;
c = c.left;
wentLeft = true;
while (c.right != null) {
p = c;
c = c.right;
wentLeft = false;
}
n.key = c.key;
if (wentLeft) {
p.left = null;
} else {
p.right = null;
}
}
return true;
}
public static void testSanity() {
BST tree = new BST();
tree.insert(100);
tree.insert(1);
tree.insert(1000);
tree.insert(2000);
tree.insert(500);
tree.insert(5);
System.out.println("tests:");
System.out.println(tree.toString() + " " + tree.toString().equals(" 100 1 5 1000 500 2000"));
System.out.println(tree.insert(100) == false);
System.out.println(tree.search(400) == false);
System.out.println(tree.search(1000) == true);
tree.delete(1000);
System.out.println(tree.toString() + " " + tree.toString().equals(" 100 1 5 500 2000"));
tree.delete(1);
System.out.println(tree.toString() + " " + tree.toString().equals(" 100 5 500 2000"));
tree.delete(100);
System.out.println(tree.toString() + " " + tree.toString().equals(" 5 500 2000"));
tree = new BST();
tree.insert(100);
tree.insert(1);
tree.insert(1000);
tree.insert(2000);
tree.insert(500);
tree.insert(5);
tree.delete(100);
System.out.println(tree.toString() + " " + tree.toString().equals(" 5 1 1000 500 2000"));
}
public static void testGetPathEmptyTree() {
BST tree = new BST();
String s = "testGetPathEmptyTree: " + (tree.getPath(1) == null);
System.out.println(s);
}
public static void testGetPath1Node() {
BST tree = new BST();
tree.insert(1);
ArrayList<Integer> path = tree.getPath(1);
String s = "testGetPath1Node: " + (path.get(0).equals(1) && path.size() == 1);
System.out.println(s);
}
public static void testGetPath(BST tree, int key, int expectedSize) {
ArrayList<Integer> path = tree.getPath(key);
String pathStr = "";
for (Integer k : path) {
pathStr = pathStr + k.toString() + " ";
}
String s = "testGetPath1: " + pathStr + " " + (path.size() == expectedSize);
System.out.println(s);
}
public static void main(String[] args) {
testGetPathEmptyTree();
testGetPath1Node();
BST tree = new BST();
tree.insert(100);
tree.insert(1);
tree.insert(1000);
tree.insert(2000);
tree.insert(500);
tree.insert(5);
testGetPath(tree, 100, 1);
testGetPath(tree, 2000, 3);
testGetPath(tree, 500, 3);
testGetPath(tree, 5, 3);
}
}