-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST.java
297 lines (249 loc) · 6.88 KB
/
BST.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
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Queue;
/**
* BST
*
* @author LiYang
* @date 2020/1/13
**/
public class BST<E extends Comparable> {
private class Node{ //内部类
public E e;
public Node left, right;
public Node(E e){
this.e = e;
left = null;
right = null;
}
}
private Node root;
private int size;
public BST(){
root = null;
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty(){
return size == 0;
}
public void add(E e) {
root = add(root, e);
}
private Node add(Node node, E e) {
// 终止条件
if (node == null){
size++;
return new 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;
}
public boolean contains(E e) {
return contains(root, e);
}
private boolean contains(Node node, E e) {
if (node == null)
return false;
if (e.compareTo(node.e) < 0)
return contains(node.left, e);
else if (e.compareTo(node.e) > 0)
return contains(node.right, e);
else
return true;
}
/** 前序遍历 **/
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);
}
/** 中序遍历 **/
public void inOrder() {
inOrder(root);
}
public void preOrderNR(){
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node cur = stack.pop();
System.out.println(cur.e);
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
}
private void inOrder(Node node) {
if (node == null)
return;
inOrder(node.left);
System.out.println(node.e); // 在中间 三角形
inOrder(node.right);
}
public void lastOrder() {
lastOrder(root);
}
private void lastOrder(Node node) {
if (node == null)
return;
lastOrder(node.left);
lastOrder(node.right);
System.out.println(node.e);
}
public void levelOrder() {
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node cur = q.remove();
System.out.println(cur.e);
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
}
}
public void removeMinimum() {
if (size == 0)
throw new IllegalArgumentException("BST is empty");
removeMinimum(root);
}
private Node removeMinimum(Node node) {
if (node.left == null) {
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMinimum(node.left);
return node;
}
public void removeMaximum() {
if (size == 0)
throw new IllegalArgumentException("BST is empty");
removeMaximum(root);
}
private Node removeMaximum(Node node) {
if (node.right == null) {
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMaximum(node.right);
return node;
}
public void remove(E e) {
root = remove(root, e);
}
private Node remove(Node node, E e) {
if (node == null)
return null;
if (e.compareTo(node.e) < 0) {
node.left = remove(node.left, e);
return node;
} else if (e.compareTo(node.e) > 0) {
node.right = remove(node.right, e);
return node;
} else {
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 successor = minimum(node.right);
successor.right = removeMinimum(node.right);
successor.left = node.left;
size++;
node.left = node.right = null;
size--;
return successor;
}
}
public void minimum() {
if (size == 0)
throw new IllegalArgumentException("BST is empty");
System.out.println(minimum(root).e);
}
private Node minimum(Node node) {
if (node.left == null)
return node;
return minimum(node.left);
}
public void maximum() {
if (size == 0)
throw new IllegalArgumentException("BST is empty");
System.out.println(maximum(root).e);
}
private Node maximum(Node node) {
if (node.right == null)
return node;
return minimum(node.right);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
generateBSTString(root, 0, res);
return res.toString();
}
private void generateBSTString(Node node, int depth, StringBuilder res) {
if (node == null) {
res.append(generateDepthString(depth) + "null\n");
return;
}
res.append(generateDepthString(depth) + node.e + "null\n");
generateBSTString(node.left, depth + 1, res);
generateBSTString(node.right, depth + 1, res);
}
private String generateDepthString(int depth) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < depth; i++) {
res.append("--");
}
return res.toString();
}
public static void main(String[] args) {
BST<Integer> bst = new BST<Integer>();
bst.add(10);
bst.add(3);
bst.add(5);
bst.add(2);
bst.add(12);
bst.add(15);
bst.add(11);
System.out.println(bst.contains(1));
bst.preOrder();
System.out.println();
bst.preOrderNR();
System.out.println("inOrder");
bst.inOrder();
System.out.println();
bst.lastOrder();
System.out.println();
bst.levelOrder();
System.out.println();
bst.removeMinimum();
bst.levelOrder();
System.out.println();
bst.minimum();
}
}