-
Notifications
You must be signed in to change notification settings - Fork 13
/
BinarySearchTree.java
270 lines (224 loc) · 5.53 KB
/
BinarySearchTree.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
package leetcode.tree.bst;
import java.util.ArrayList;
import java.util.List;
/**
* 实现二叉搜索树
*
* @author FangYuan
* @since 2023-11-02 21:06:56
*/
public class BinarySearchTree {
public static void main(String[] args) {
BinarySearchTree binarySearchTree = new BinarySearchTree();
binarySearchTree.putNode(8, 8);
binarySearchTree.putNode(4, 4);
binarySearchTree.putNode(12, 12);
binarySearchTree.putNode(10, 10);
binarySearchTree.putNode(13, 13);
binarySearchTree.putNode(2, 2);
binarySearchTree.putNode(6, 6);
System.out.println(binarySearchTree.keys(3, 10));
}
static class Node {
int key;
int val;
Node left;
Node right;
public Node(int key, int val) {
this.key = key;
this.val = val;
}
}
// 根节点
Node root;
/**
* 根据 key 获取对应的节点值
*/
public Integer getValue(int key) {
return getValue(root, key);
}
private Integer getValue(Node node, int key) {
if (node == null) {
return null;
}
if (key > node.key) {
return getValue(node.right, key);
}
if (key < node.key) {
return getValue(node.left, key);
}
return node.val;
}
/**
* 将节点插入二叉搜索树中合适的位置
*/
public void putNode(int key, int val) {
root = putNode(root, key, val);
}
private Node putNode(Node node, int key, int val) {
if (node == null) {
return new Node(key, val);
}
if (key > node.val) {
node.right = putNode(node.right, key, val);
} else if (key < node.val) {
node.left = putNode(node.left, key, val);
} else {
node.val = val;
}
return node;
}
/**
* 获取最大节点
*/
public Node getMaxNode() {
if (root == null) {
return null;
}
return getMaxNode(root);
}
private Node getMaxNode(Node node) {
if (node.right == null) {
return node;
}
return getMaxNode(node.right);
}
/**
* 获取最小节点
*/
public Node getMinNode() {
if (root == null) {
return null;
}
return getMinNode(root);
}
private Node getMinNode(Node node) {
if (node.left == null) {
return node;
}
return getMinNode(node.left);
}
/**
* 向下取整查找
*/
public Node floor(int key) {
return floor(root, key);
}
private Node floor(Node node, int key) {
if (node == null) {
return null;
}
if (key == node.val) {
return node;
}
if (key < node.val) {
return floor(node.left, key);
}
Node right = floor(node.right, key);
return right != null ? right : node;
}
/**
* 向上取整查找
*/
public Node ceiling(int key) {
return ceiling(root, key);
}
private Node ceiling(Node node, int key) {
if (node == null) {
return null;
}
if (key == node.val) {
return node;
}
if (key > node.val) {
return ceiling(node.right, key);
}
Node left = ceiling(node.left, key);
return left != null ? left : node;
}
/**
* 删除最小节点
*/
public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node node) {
if (node == null) {
return null;
}
if (node.left == null) {
return node.right;
}
node.left = deleteMin(root.left);
return node;
}
/**
* 删除最大节点
*/
public void deleteMax() {
root = deleteMax(root);
}
private Node deleteMax(Node node) {
if (node == null) {
return null;
}
if (node.right == null) {
return node.left;
}
node.right = deleteMax(node.right);
return node;
}
/**
* 删除指定节点
*/
public void delete(int key) {
root = delete(key, root);
}
private Node delete(int key, Node node) {
if (node == null) {
return null;
}
if (key > node.val) {
node.right = delete(key, node.right);
return node;
}
if (key < node.val) {
node.left = delete(key, node.left);
return node;
}
if (node.left == null) {
return node.right;
}
if (node.right == null) {
return node.left;
}
Node min = getMinNode(node.right);
min.left = node.left;
return node;
}
/**
* 范围查找
*
* @param left 区间下界
* @param right 区间上界
*/
public List<Integer> keys(int left, int right) {
ArrayList<Integer> res = new ArrayList<>();
keys(root, left, right, res);
return res;
}
private void keys(Node node, int left, int right, ArrayList<Integer> res) {
if (node == null) {
return;
}
if (node.val > left) {
keys(node.left, left, right, res);
}
if (node.val >= left && node.val <= right) {
res.add(node.val);
}
if (node.val < right) {
keys(node.right, left, right, res);
}
}
}