-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBSTree.java
More file actions
267 lines (228 loc) · 6.51 KB
/
BSTree.java
File metadata and controls
267 lines (228 loc) · 6.51 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
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.util.NoSuchElementException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class BSTree <Key extends Comparable<Key>, Value> {
private Node root;
private class Node {
private Key key;
private Value val;
private Node father;
private Node left;
private Node right;
public Node(Key key, Value val) {
this.key = key;
this.val = val;
}
}
public boolean isEmpty() {
return (root == null);
}
public void inorder(){
inorder(root);
}
private void inorder(Node x) {
if (x == null) return;
inorder(x.left);
System.out.println(x.key + " " + x.val);
inorder(x.right);
}
public void revorder(){
revorder(root);
}
private void revorder(Node x) {
if (x == null) return;
revorder(x.right);
System.out.println(x.key + " " + x.val);
revorder(x.left);
}
public void preorder() {
preorder(root);
}
private void preorder(Node x) {
if (x == null) return;
System.out.println(x.key + " " + x.val);
preorder(x.left);
preorder(x.right);
}
public void posorder() {
posorder(root);
}
private void posorder(Node x) {
if (x == null) return;
posorder(x.left);
posorder(x.right);
System.out.println(x.key + " " + x.val);
}
/**
* Inicializes an empty symbol table
*/
public BSTree(){ }
public boolean contains(Key key) {
if (key == null)
throw new NullPointerException("argument to contains() is null");
return get(key) != null;
}
public Value get(Key key) {
return get(root, key);
}
private Value get(Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key); //Algorithms
if (cmp < 0)
return get(x.left, key);
else if (cmp > 0)
return get(x.right, key);
else
return x.val;
}
public void put (Key key, Value val) {
int cmp;
Node z = new Node(key, val);
Node x = root;
Node y = null;
while(x != null) {
y = x;
cmp = key.compareTo(x.key);
if (cmp < 0)
x = x.left;
if (cmp > 0)
x = x.right;
if (cmp == 0) {
if (val == null)
delete(key);
else
x.val = val;
return;
}
}
z.father = y;
if (y == null)
root = z;
else {
cmp = key.compareTo(y.key);
if(cmp < 0)
y.left = z;
else
y.right = z;
}
}
private void transplant(Node u, Node v) {
if (u.father == null) {
root = v;
} else {
if (u == u.father.left) {
u.father.left = v;
} else {
u.father.right = v;
}
}
if (v != null) {
v.father = u.father;
}
}
public void delete(Key key) {
if (key == null)
throw new NullPointerException("argument to delete() is null");
delete(root, key);
}
private void delete(Node z, Key key) {
if (z == null) return;
int cmp = key.compareTo(z.key);
if (cmp < 0)
delete(z.left, key);
else if (cmp > 0)
delete(z.right, key);
else {
if (z.left == null) {
transplant(z, z.right);
} else {
if (z.right == null) {
transplant(z, z.left);
} else {
Node y = min(z.right);
if (y.father != z) {
transplant(y, y.right);
y.right = z.right;
y.right.father = y;
}
transplant(z, y);
y.left = z.left;
y.left.father = y;
}
}
}
}
public Key min() {
if (isEmpty())
throw new NoSuchElementException("called min() with empty symbol table");
return min(root).key;
}
private Node min(Node x) {
if (x.left == null)
return x;
else
return min(x.left);
}
public Key max() {
if (isEmpty())
throw new NoSuchElementException("called max() with empty symbol table");
return max(root).key;
}
private Node max(Node x) {
if (x.right == null)
return x;
else
return max(x.right);
}
/**
* Unit tests the <tt>BST</tt> data type.
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("\n\nUsage: java BSTree file1 file2\n\n");
System.exit(0);
}
int n;
String tmp;
StringTokenizer st;
BSTree<String, City> mytree = new BSTree<String, City>();
City city;
try {
FileReader in1 = new FileReader(args[0]);
BufferedReader br = new BufferedReader(in1);
n = Integer.parseInt(br.readLine());
for (int j = 0; j < n; j++) {
tmp = br.readLine();
st = new StringTokenizer(tmp);
city = new City(
st.nextToken(),
Integer.parseInt(st.nextToken())
);
mytree.put(city.getName(), city);
}
br.close();
in1.close();
in1 = new FileReader(args[1]);
br = new BufferedReader(in1);
n = Integer.parseInt(br.readLine());
for (int j = 0; j < n; j ++) {
tmp = br.readLine();
st = new StringTokenizer(tmp);
//pos = rank(new City(tmp, 0), whitelist);
String name = st.nextToken();
city = mytree.get(name);
if (city == null){
System.out.print("\n[Failed] " + name + " wasn't found.");
} else {
System.out.print("\n[Ok]\t" + city.toString());
}
}
br.close();
in1.close();
System.out.println("\n");
} catch (IOException ioe) {
}
}
}