-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary-search-tree.java
285 lines (271 loc) · 8.01 KB
/
binary-search-tree.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
package me.iwts;
class BST<Key extends Comparable<Key>, Value>{
private Node root;
private class Node{
private Key key;
private Value value;
private Node left;
private Node right;
private Node father;
public Node(Key key,Value value){
this.key = key;
this.value = value;
}
}
// 递归方法进行搜索
// public Value get(Key key){
// if(key == null) return null;
// return get(this.root,key);
// }
// public Value get(Node root,Key key){
// if(root == null) return null;
// int cmp = key.compareTo(root.key);
// if(cmp == 0){
// return root.value;
// }else{
// if(cmp < 0){
// return get(root.left,key);
// }else{
// return get(root.right,key);
// }
// }
// }
public Value get(Key key){
if(key == null) return null;
return get(this.root,key);
}
public Value get(Node root,Key key){
while(root != null){
int cmp = key.compareTo(root.key);
if(cmp == 0) return root.value;
if(cmp < 0){
root = root.left;
}else{
root = root.right;
}
}
return null;
}
public Value max(){
if(root == null) return null;
Node temp = max(root);
return temp.value;
}
public Node max(Node root){
if(root == null) return null;
while(root.right != null){
root = root.right;
}
return root;
}
public Value min(){
if(root == null) return null;
Node temp = min(root);
return temp.value;
}
public Node min(Node root){
if(root == null) return null;
while(root.left != null){
root = root.left;
}
return root;
}
public Value floor(Key key){
if(root == null || key == null) return null;
return floor(root,key);
}
public Value floor(Node root,Key key){
Value res = null;
while(root != null){
int cmp = key.compareTo(root.key);
if(cmp == 0) return root.value;
if(cmp < 0){
root = root.left;
}else{
res = root.value;
root = root.right;
}
}
return res;
}
public Value ceiling(Key key){
if(root == null || key == null) return null;
return ceiling(root,key);
}
public Value ceiling(Node root,Key key){
Value res = null;
while(root != null){
int cmp = key.compareTo(root.key);
if(cmp == 0) return root.value;
if(cmp < 0){
root = root.left;
root = root.left;
}else{
res = root.value;
}
}
return res;
}
public void put(Key key,Value value){
if(key == null || value == null) return;
if(root == null){
root = new Node(key,value);
return;
}
put(root,key,value);
}
public void put(Node root,Key key,Value value){
while(root != null){
int cmp = key.compareTo(root.key);
if(cmp == 0){
root.value = value;
return;
}
if(cmp < 0){
if(root.left == null){
root.left = new Node(key,value);
root.left.father = root;
return;
}else{
root = root.left;
}
}else{
if(root.right == null){
root.right = new Node(key,value);
root.right.father = root;
return;
}else{
root = root.right;
}
}
}
}
public void deleteMin(){
deleteMin(root);
}
public void deleteMin(Node root){
Node res = min(root);
if(res.father == null){
this.root = null;
}else{
if(res.father.left.key.compareTo(res.key) == 0){
res.father.left = res.right;
}else{
res.father.right = res.right;
}
}
}
public void deleteMax(){
deleteMax(root);
}
public void deleteMax(Node root){
Node res = max(root);
if(res.father == null){
this.root = null;
}else{
if(res.father.left.key.compareTo(res.key) == 0){
res.father.left = res.left;
}else{
res.father.right = res.left;
}
}
}
public void delete(Key key){
if(key == null) return;
// 删除根的情况,需要额外处理
if(key.compareTo(root.key) == 0){
if(root.left == null && root.right == null){
root = null;
}else{
if(root.left != null && root.right != null){
Node temp = min(root.right);
deleteMin(root.right);
temp.left = root.left;
temp.right = root.right;
root = temp;
}else{
if(root.left == null){
root = root.right;
}else{
root = root.left;
}
}
}
}else{
// 其他情况
delete(root,key);
}
}
public void delete(Node root,Key key){
// flag处理方向,父结点是从左子树连接下来还是右子树
int flag = 0;
while(root != null){
int cmp = key.compareTo(root.key);
if(cmp == 0){
Node father = root.father;
if(root.left == null && root.right == null){
if(flag == 1){
father.left = null;
}else{
father.right = null;
}
}else{
// 核心算法部分
if(root.left != null && root.right != null){
// 找右子树最小结点
Node temp = min(root.right);
deleteMin(root.right);
temp.left = root.left;
temp.right = root.right;
temp.father = root.father;
if(flag == 1){
father.left = temp;
}else{
father.right = temp;
}
}else{
if(root.left == null){
if(flag == 1){
father.left = root.right;
}else{
father.right = root.right;
}
root.right.father = father;
}else{
if(flag == 1){
father.left = root.left;
}else{
father.right = root.left;
}
root.left.father = father;
}
}
}
return;
}else{
if(cmp < 0){
root = root.left;
flag = 1;
}else{
root = root.right;
flag = 2;
}
}
}
}
}
public class Main{
public static void main(String[] args){
BST<String,String> bst = new BST<>();
bst.put("s","s");
bst.put("e","e");
bst.put("a","a");
bst.put("c","c");
bst.put("r","r");
bst.put("h","h");
bst.put("m","m");
bst.put("x","x");
bst.delete("e");
System.out.println(bst.min());
}
}