-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTree.java
More file actions
459 lines (384 loc) · 12.3 KB
/
BSTree.java
File metadata and controls
459 lines (384 loc) · 12.3 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.BaseStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
class Node{
private String key;
private String value;
private Node left;
private Node right;
public Node(String key,String value){
this.key = key;
this.value = value;
left = right = null;
}
public Node left() {
return this.left;
}
public Node right() {
return this.right;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
public String key() {
return this.key;
}
public void setKey(String key) {
this.key = key;
}
public String value() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
class Result{
String value = null;
String originalValue = null;
public Result(String value){
this.value = value;
}
}
public class BSTree implements BST<String,String> {
private Node root;
private String key;
private String value;
private BSTree left;
private BSTree right;
public BSTree(String key, String value) {
this.key = key;
this.value = value;
left = null;
right = null;
}
public BSTree() {
root = null;
}
public BSTree(Node root) {
this.root = root;
}
public BSTree left() {
return this.left;
}
public BSTree right() {
return this.right;
}
private void setLeft(BSTree left) {
this.left = left;
}
private void setRight(BSTree right) {
this.right = right;
}
public String key() {
return this.key;
}
private void setKey(String key) {
this.key = key;
}
public String value() {
return this.value;
}
private void setValue(String value) {
this.value = value;
}
public Node root(){
return this.root;
}
@Override
public void insert(String key, String value) {
this.root = insert(root,key,value); //将结点插入树
}
private Node insert(Node node,String key,String value){
if(key != null && value != null) {
if (node == null) { //空树
node = new Node(key, value);
return node;
}
if(key.compareTo(node.key())<0){
node.setLeft(insert(node.left(),key,value));
}
else if(key.compareTo(node.key())>0){
node.setRight(insert(node.right(),key,value)); //插入右子树
}
else{
node.setValue(value);
}
return node;
}
return null;
}
@Override
public String remove(String key) {
if (key == null) {
throw new IllegalArgumentException("Key is null");
}
Result result = new Result(null);
root = remove(root, key, result);
if (result.value == null) {
return (String) ("remove failure --- " + key);
} else {
return (String) ("remove success --- " + key + " " + result.originalValue);
}
}
private Node remove(Node node, String key, Result result){
if(node == null || key == null){
return null;
}
if(key.compareTo(node.key())<0){
node.setLeft(remove(node.left(),key,result));
return node;
}
else if(key.compareTo(node.key())>0){
node.setRight(remove(node.right(),key,result));
return node;
}else{ //结点关键码匹配
if(result.value == null){
result.value = node.value();
result.originalValue = node.value(); //存储结点值
}
if(node.left() == null){
return node.right();
}
else if(node.right() == null){
return node.left();
}
else{
Node min = findMin(node.right());
node.setKey(min.key());
node.setValue(min.value());
node.setRight(remove(node.right(),min.key(),result));
}
}
return node;
}
private Node findMin(Node node){
while(node.left() != null){
node = node.left();
}
return node;
}
@Override
public String search(String key) {
return search(root,key);
}
private String search(Node node,String key){
if(key == null){
return null;
}
if(node == null){
return (String)("search failure --- "+key);
}
if(key.compareTo(node.key())<0){
return search(node.left(),key);
}
else if(key.compareTo(node.key())>0){
return search(node.right(),key);
}
else{
return (String)("search success --- "+key+" "+node.value());
}
}
public String find(String key){
return find(key,root);
}
private String find(String key,Node node){
if(key == null){
return null;
}
if(node == null){
return (String)("FAILURE");
}
if(key.compareTo(node.key())<0){
return find(key,node.left());
}
else if(key.compareTo(node.key())>0){
return find(key,node.right());
}
else{
return node.value();
}
}
@Override
public boolean update(String key, String value) {
return update(root,key,value) != null;
}
private Node update(Node node,String key,String value){
if(key == null || value == null || node == null){
return null;
}
if(key.compareTo(node.key())<0){
return update(node.left(),key,value);
}
else if(key.compareTo(node.key())>0){
return update(node.right(),key,value);
}
else{
node.setValue(value);
return node;
}
}
@Override
public void clear() {
root = null;
}
@Override
public boolean isEmpty() {
return root == null;
}
@Override
public void showStructure(PrintWriter pw) throws IOException {
pw.println("-----------------------");
pw.printf("There are %d nodes in this BST.", inorder(this));
pw.println();
pw.printf("The height of this BST is %d", height(this));
pw.println();
pw.println("-----------------------");
}
@Override
public void printInorder(PrintWriter pw) throws IOException {
printInorderHelper(root,pw);
}
private void printInorderHelper(Node root,PrintWriter pw) throws IOException{
if(root != null){
printInorderHelper(root.left(),pw);
pw.println("["+root.key()+"---<"+root.value()+">]");
printInorderHelper(root.right(),pw);
}
}
public static int inorder(BSTree root) {
if (root == null) {
return 0;
}
return inorder(root.left()) + inorder(root.right()) + 1;
}
public static int height(BSTree root) {
if (root == null) {
return 0;
}
return height(root.left()) > height(root.right()) ? height(root.left()) + 1 : height(root.right()) + 1;
}
public static void processCommand(BSTree root,String command,PrintWriter pw,int value){
String newValue =Integer.toString(value);
String key = null;
if(command.length()>1){
String[] parts = command.substring(0,command.length()).split("[ ,?,\\,\",!,--,*,&,(,),:,;]");
for(int i = 0;i< parts.length;i++){
if(parts[i].length()>1 && parts[i].charAt(0) == '\'' && parts[i].charAt(parts[i].length()-1)=='\''){
parts[i] = parts[i].substring(1,parts[i].length()-1);
}else if(parts[i].length()>0 && parts[i].charAt(0) == '\''){
parts[i] = parts[i].substring(1,parts[i].length());
}
else if(parts[i].length()>0 && parts[i].charAt(parts[i].length()-1) == '\''){
parts[i] = parts[i].substring(0,parts[i].length()-1);
}
if(parts[i].length()>0 && parts[i].charAt(parts[i].length()-1) == '.'){
parts[i] = parts[i].substring(0,parts[i].length()-1);
}
}
if(parts.length>0) {
for(int i = 0;i< parts.length;i++){
if(!parts[i].equals("")){
key = parts[i];
if(root.find(key) == "FAILURE"){
root.insert(key,newValue);
}
else{
String original = root.find(key);
root.update(key,original+" "+newValue);
}
}
}
}
}
if(value == 33404){
try{
root.printInorder(pw);
}catch (IOException e){
e.printStackTrace();
}
}
}
public static void processCommand(BSTree root,String command,PrintWriter pw){
char operation = command.charAt(0);
String key = null;
String value = null;
if(command.length()>1){
String[] parts = command.substring(2,command.length()-1).split(",");
key = parts[0].trim();
if(parts.length>=2){
value = parts[1].substring(2,parts[1].length()-2);
}
}
switch(operation){
case '+':
root.insert(key,value);
break;
case '-':
pw.println(root.remove(key));
break;
case'?':
pw.println(root.search(key));
break;
case'=':
boolean updateResult = root.update(key,value);
if(updateResult){
pw.println("update success ---"+key+" "+value);
}
else{
pw.println("update failure ---"+key);
}
break;
case '#':
try{
root.showStructure(pw);
}
catch(IOException e){
throw new RuntimeException(e);
}
break;
default:
System.out.println("Invalid Command");
}
}
public static void main(String[] args){
BSTree tree = new BSTree();
/* try{
File file = new File("D:/temp/homework3_testcases.txt");
Scanner scanner = new Scanner(file);
File outputFile = new File("output.txt");
PrintWriter printWriter = new PrintWriter(outputFile);
while (scanner.hasNextLine()){
String input = scanner.nextLine();
processCommand(tree,input,printWriter);
}
scanner.close();
printWriter.close();
}catch (FileNotFoundException e){
e.printStackTrace();;
}*/
try{
File file = new File("D:/temp/homework3_article.txt");
Scanner scanner = new Scanner(file);
File outputFile = new File("index_result.txt");
PrintWriter printWriter = new PrintWriter(outputFile);
int Line = 0;
while (scanner.hasNextLine()){
Line++;
String input = scanner.nextLine();
processCommand(tree,input,printWriter,Line);
}
scanner.close();
printWriter.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}