forked from IvanLu1024/Java-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
400 lines (358 loc) · 9.94 KB
/
Copy pathBST.java
File metadata and controls
400 lines (358 loc) · 9.94 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
package code_08_trie;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* Created by 18351 on 2018/12/17.
*/
public class BST<E extends Comparable<E>> {
private Node root;
private int size;
public BST(){
root=null;
size=0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
private class Node{
public E e;
public Node left,right;
public Node(E e){
this.e=e;
this.left=null;
this.right=null;
}
}
/* //向BST中添加新元素e
public void add(E e){
if(root==null){
root=new Node(e);
size++;
return;
}else{
add(root,e);
}
}
//向以node为根节点的BST树种插入元素e
private void add(Node node,E e){
if(e.equals(node.e)){
//插入元素与根节点相等,直接返回
return;
}else if(e.compareTo(node.e)<0 && node.left==null){
node.left=new Node(e);
size++;
return;
}else if(e.compareTo(node.e)>0 && node.right==null){
node.right=new Node(e);
size++;
return;
}
if(e.compareTo(node.e)<0){
add(node.left,e);
}else{ //e.compareTo(node.e)>0
add(node.right,e);
}
}*/
//向BST中添加新元素e
public void add(E e){
root=add(root,e);
}
//向以node为根节点的BST树种插入元素e
//返回插入新元素后该BST的根
private Node add(Node node,E e){
if(node==null){
size++;
return new Node(e);
}
//注意:对于e.equals(node.e)不做处理
if(e.compareTo(node.e)<0){
node.left=add(node.left,e);
}else if(e.compareTo(node.e)>0){
node.right=add(node.right,e);
}
return node;
}
//查看BST中是否包含元素e
public boolean contains(E e){
return contains(root,e);
}
//查看以node为根节点的BST中是否包含元素e
private boolean contains(Node node,E e){
if(node==null){
return false;
}
if(e.compareTo(node.e)==0){
return true;
}else if(e.compareTo(node.e)<0){
return contains(node.left,e);
}else{
return contains(node.right,e);
}
}
//BST的前序遍历
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node){
if(node==null){
return;
}
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
//BST的前序遍历(非递归方式)
public void preOrderNR(){
preOrderNR(root);
}
private void preOrderNR(Node node){
if(node==null){
return;
}
Stack<StackNode> stack=new Stack<>();
stack.push(new StackNode(Command.GO,node));
while(!stack.isEmpty()){
StackNode stackNode=stack.pop();
Node n=stackNode.node;
Command command=stackNode.command;
if(command== Command.COUT){
System.out.println(stackNode.node.e);
}else{
if(n.right!=null){
stack.push(new StackNode(Command.GO,n.right));
}
if(n.left!=null){
stack.push(new StackNode(Command.GO,n.left));
}
stack.push(new StackNode(Command.COUT,n));
}
}
}
//BST的中序遍历
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if(node==null){
return;
}
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
//BST的中序遍历(非递归方式)
public void inOrderNR(){
inOrderNR(root);
}
private void inOrderNR(Node node){
if(node==null){
return;
}
Stack<StackNode> stack=new Stack<>();
stack.push(new StackNode(Command.GO,node));
while(!stack.isEmpty()){
StackNode stackNode=stack.pop();
Node n=stackNode.node;
Command command=stackNode.command;
if(command== Command.COUT){
System.out.println(stackNode.node.e);
}else{
if(n.right!=null){
stack.push(new StackNode(Command.GO,n.right));
}
stack.push(new StackNode(Command.COUT,n));
if(n.left!=null){
stack.push(new StackNode(Command.GO,n.left));
}
}
}
}
//BST的后序遍历
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node==null){
return;
}
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
//BST的后序遍历(非递归方式)
public void postOrderNR(){
postOrderNR(root);
}
private void postOrderNR(Node node){
if(node==null){
return;
}
Stack<StackNode> stack=new Stack<>();
stack.push(new StackNode(Command.GO,node));
while(!stack.isEmpty()){
StackNode stackNode=stack.pop();
Node n=stackNode.node;
Command command=stackNode.command;
if(command== Command.COUT){
System.out.println(stackNode.node.e);
}else{
stack.push(new StackNode(Command.COUT,n));
if(n.right!=null){
stack.push(new StackNode(Command.GO,n.right));
}
if(n.left!=null){
stack.push(new StackNode(Command.GO,n.left));
}
}
}
}
//枚举命令,GO表示访问元素,COUT表示打印元素
private enum Command{GO,COUT};
private class StackNode{
Command command;
Node node;
StackNode(Command command,Node node){
this.command=command;
this.node=node;
}
}
//BST的层序遍历
public void levelOrder(){
Queue<Node> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
Node node=q.remove();
System.out.println(node.e);
if(node.left!=null){
q.add(node.left);
}
if(node.right!=null){
q.add(node.right);
}
}
}
//寻找BST中的最小元素
public E min(){
if(size==0){
throw new IllegalArgumentException("BST is emmpty");
}
return min(root).e;
}
private Node min(Node node){
if(node.left==null){
return node;
}
return min(node.left);
}
//寻找BST中的最大元素
public E max(){
if(size==0){
throw new IllegalArgumentException("BST is emmpty");
}
return max(root).e;
}
private Node max(Node node){
if(node.right==null){
return node;
}
return max(node.right);
}
//删除BST中的最小值
public E delMin(){
E res=min();
delMin(root);
return res;
}
//删除以node为根结点的BST中的最小值元素
private Node delMin(Node node){
if(node.left==null){
Node nodeRight=node.right;
node.right=null;
size--;
return nodeRight;
}
node.left=delMin(node.left);
return node;
}
//删除BST中的最大值
public E delMax(){
E res=max();
delMax(root);
return res;
}
//删除以node为根结点的BST中的最大值元素
private Node delMax(Node node){
if(node.right==null){
Node nodeLeft=node.left;
node.left=null;
size--;
return nodeLeft;
}
node.right=delMax(node.right);
return node;
}
//删除BST中任意元素
public void del(E e){
root=del(root,e);
}
private Node del(Node node,E e){
if(node==null){
return null;
}
if(e.compareTo(node.e)<0){
return del(node.left,e);
}else if(e.compareTo(node.e)>0){
return del(node.right,e);
}else{
//节点node就是要删除的节点
//该节点只右有子树
if(node.left==null){
Node rightNode=node.right;
node.right=null;
size--;
return rightNode;
}
//该节点只有左子树
if(node.right==null){
Node leftNode=node.left;
node.left=null;
size--;
return leftNode;
}
//删除既有左子树又有右子树的节点
Node s=min(node.right);
s.right=delMin(node.right);
s.left=node.left;
//删除node
node.left=node.right=null;
return s;
}
}
@Override
public String toString() {
StringBuilder res=new StringBuilder();
generateBST(root,0,res);
return res.toString();
}
//生成以node为根节点,深度为depth的描述二叉树的字符串(利用前序遍历)
private void generateBST(Node node,int depth,StringBuilder res){
if(node==null){
res.append(generateDepth(depth)+"null\n");
return;
}
res.append(generateDepth(depth)+node.e+"\n");
generateBST(node.left,depth+1,res);
generateBST(node.right,depth+1,res);
}
private String generateDepth(int depth){
StringBuilder res=new StringBuilder();
for(int i=0;i<depth;i++){
res.append("--");
}
return res.toString();
}
}