-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementBinaryTree.java
More file actions
299 lines (250 loc) · 7.21 KB
/
ImplementBinaryTree.java
File metadata and controls
299 lines (250 loc) · 7.21 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
import java.util.Queue;
import java.util.LinkedList;
public class ImplementBinaryTree {
public static void main(String[] args) {
System.out.println("Test BinaryTree...");
BinarySearchTree<Integer, String> t1 = new BinarySearchTree<Integer, String>();
t1.put(1, "1");
t1.put(2, "2");
t1.put(3, "3");
System.out.println("插入完成后元素的个数: " + t1.size());
System.out.println("key2对应的元素是: " + t1.get(2));
t1.delete(3);
System.out.println("插入完成后元素的个数: " + t1.size());
System.out.println("key3对应的元素是: " + t1.get(3));
t1.put(-3, "-3");
System.out.println("最小的key为: " + t1.min());
System.out.println("最大的key为: " + t1.max());
BinarySearchTree<String, String> t2 = new BinarySearchTree<String, String>();
t2.put("E", "5");
t2.put("B", "2");
t2.put("G", "3");
t2.put("A", "1");
t2.put("D", "4");
t2.put("F", "6");
t2.put("H", "8");
t2.put("C", "3");
System.out.println("前序遍历: " + t2.preErgodic());
System.out.println("中序遍历: " + t2.midErgodic());
System.out.println("后序遍历: " + t2.afterErgodic());
System.out.println("层序遍历: " + t2.layerErgodic());
System.out.println("最大深度: " + t2.maxDepth());
}
}
abstract class BinaryTree<Key extends Comparable<Key>, Value> {
class Node {
protected Node left;
protected Node right;
protected Key key;
protected Value value;
public Node(Key key, Value value, Node left, Node right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
}
protected Node root;
protected int N;
public int size() {
return this.N;
}
abstract public void put(Key key, Value value);
abstract public Value get(Key key);
abstract public void delete(Key key);
public int maxDepth() {
return maxDepth(root);
}
protected int maxDepth(Node x) {
int leftMaxDepth = 0;
int rightMaxDepth = 0;
if (x.left != null) {
leftMaxDepth = maxDepth(x.left);
}
if (x.right != null) {
rightMaxDepth = maxDepth(x.right);
}
return Math.max(leftMaxDepth, rightMaxDepth) + 1;
}
public Queue<Key> preErgodic() {
Queue<Key> keys = new LinkedList<Key>();
preErgodic(root, keys);
return keys;
}
protected void preErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
keys.add(x.key);
if (x.left != null) {
preErgodic(x.left, keys);
}
if (x.right != null) {
preErgodic(x.right, keys);
}
}
public Queue<Key> midErgodic() {
Queue<Key> keys = new LinkedList<Key>();
midErgodic(root, keys);
return keys;
}
protected void midErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
if (x.left != null) {
midErgodic(x.left, keys);
}
keys.add(x.key);
if (x.right != null) {
midErgodic(x.right, keys);
}
}
public Queue<Key> afterErgodic() {
Queue<Key> keys = new LinkedList<Key>();
afterErgodic(root, keys);
return keys;
}
protected void afterErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
if (x.left != null) {
afterErgodic(x.left, keys);
}
if (x.right != null) {
afterErgodic(x.right, keys);
}
keys.add(x.key);
}
public Queue<Key> layerErgodic() {
Queue<Node> q1 = new LinkedList<Node>();
Queue<Key> q2 = new LinkedList<Key>();
q1.add(root);
while (q1.size() > 0) {
layerErgodic(q1, q2);
}
return q2;
}
protected void layerErgodic(Queue<Node> q1, Queue<Key> q2) {
Node firstNode = q1.remove();
q2.add(firstNode.key);
if (firstNode.left != null) {
q1.add(firstNode.left);
}
if (firstNode.right != null) {
q1.add(firstNode.right);
}
}
}
class BinarySearchTree<Key extends Comparable<Key>, Value> extends BinaryTree<Key, Value> {
public BinarySearchTree() {
this.root = null;
this.N = 0;
}
@Override
public void put(Key key, Value value) {
root = this.put(root, key, value);
}
protected Node put(Node x, Key key, Value value) {
if (x == null) {
this.N++;
return new Node(key, value, null, null);
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
// key > x.key
x.right = this.put(x.right, key, value);
} else if (cmp < 0) {
// key < x.key
x.left = this.put(x.left, key, value);
} else {
// key == x.key
x.value = value;
}
return x;
}
@Override
public Value get(Key key) {
return this.get(this.root, key);
}
protected Value get(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
// key > x.key
return this.get(x.right, key);
} else if (cmp < 0) {
// key < x.key
return this.get(x.left, key);
} else {
// key == x.key
return x.value;
}
}
@Override
public void delete(Key key) {
this.delete(this.root, key);
}
protected Node delete(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
// key > x.key
x.right = delete(x.right, key);
} else if (cmp < 0) {
// key < x.key
x.left = delete(x.left, key);
} else {
// key == x.key
if (x.right == null) {
this.N--;
return x.left;
}
if (x.left == null) {
this.N--;
return x.right;
}
Node minNode = x.right;
while (minNode.left != null) {
minNode = minNode.left;
}
Node node = x.right;
while (node.left != null) {
if (node.left.left == null) {
node.left = null;
} else {
node = node.left;
}
}
x.key = minNode.key;
x.value = minNode.value;
this.N--;
}
return x;
}
public Key min() {
return min(root).key;
}
protected Node min(Node x) {
Node node = x;
while (node.left != null) {
node = node.left;
}
return node;
}
public Key max() {
return max(root).key;
}
protected Node max(Node x) {
if (x.right == null) {
return x;
} else {
return max(x.right);
}
}
}