-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBNode.java
More file actions
348 lines (301 loc) · 9.1 KB
/
Copy pathRBNode.java
File metadata and controls
348 lines (301 loc) · 9.1 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
class RBTree<T extends Comparable<T>> {
/** 根节点 **/
private RBNode<T> root;
/** 红黑树标志 **/
private static final boolean RED = false;
private static final boolean BLACK = false;
public class RBNode<T extends Comparable<T>> {
/** 颜色 **/
boolean color;
/** 值 **/
T key;
/** 父节点 **/
RBNode<T> parent;
/** 左子节点 **/
RBNode<T> left;
/** 右子节点 **/
RBNode<T> right;
public RBNode(boolean color, T key, RBNode<T> parent, RBNode<T> left, RBNode<T> right) {
this.color = color;
this.key = key;
this.parent = parent;
this.left = left;
this.right = right;
}
public T getKey() {
return key;
}
public String toString(){
return ""+key+(this.color == RED ? "R" : "B");
}
}
public RBTree(){
root = null;
}
/** 获得父节点 **/
public RBNode<T> parentOf(RBNode<T> node){
return node.parent;
}
/** 设置父节点 **/
public void setParent(RBNode<T> node, RBNode<T> parent) {
if (node != null) {
node.parent = parent;
}
}
/** 获得节点的颜色 **/
public boolean colorOf(RBNode<T> node){
return null != node ? node.color : BLACK;
}
/** 判断节点的颜色 **/
public boolean isRed(RBNode<T> node){
return node != null && node.color == RED;
}
public boolean isBlack(RBNode<T> node){
return !isRed(node);
}
/** 设置节点的颜色 **/
public void setColor(RBNode<T> node,boolean color) {
if (node != null) {
node.color = color;
}
}
/***************** 前序遍历红黑树 *********************/
public void preOrder(){
preOrder(root);
}
private void preOrder(RBNode<T> tree) {
if (tree != null) {
System.out.println(tree.key + " ");
preOrder(tree.left);
preOrder(tree.right);
}
}
/***************** 中序遍历红黑树 *********************/
public void inOrder(){
inOrder(root);
}
private void inOrder(RBNode<T> tree) {
if (tree != null) {
inOrder(tree.left);
System.out.println(tree.key + " ");
inOrder(tree.right);
}
}
/***************** 后序遍历红黑树 *********************/
public void postOrder(){
postOrder(root);
}
private void postOrder(RBNode<T> tree) {
if (tree != null) {
postOrder(tree.left);
postOrder(tree.right);
System.out.println(tree.key + " ");
}
}
/**************** 查找红黑树中键值为key的节点 ***************/
private RBNode<T> search(T key){
return searchByCycle(root,key);
// return searchByRecursive(root,key);
}
private RBNode<T> searchByRecursive(RBNode<T> tree, T key) {
if (tree == null) {
return null;
}
int compare = key.compareTo(tree.key);
if (compare < 0) {
searchByRecursive(tree.left, key);
} else if (compare > 0) {
searchByRecursive(tree.right, key);
}
return tree;
}
private RBNode<T> searchByCycle(RBNode<T> tree, T key) {
while (tree != null) {
int compare = key.compareTo(tree.key);
if (compare < 0) {
tree = tree.left;
} else if (compare > 0) {
tree = tree.right;
} else {
return tree;
}
}
return null;
}
/**************** 查找最小节点的值 **********************/
public T minValue() {
RBNode<T> minNode = minNode(root);
if (minNode != null) {
return minNode.key;
}
return null;
}
private RBNode<T> minNode(RBNode<T> root) {
if (null == root) {
return null;
}
if (null == root.left) {
return root;
}
return minNode(root.left);
}
/******************** 查找最大节点的值 *******************/
public T maxValue() {
RBNode<T> maxNode = maxNode(root);
if (maxNode != null) {
return maxNode.key;
}
return null;
}
private RBNode<T> maxNode(RBNode<T> root) {
if (null == root) {
return null;
}
if (null == root.right) {
return root;
}
return maxNode(root.right);
}
/********* 查找节点x的后继节点,即大于节点x的最小节点 ***********/
public RBNode<T> successor(RBNode<T> x) {
if (x.right == null) {
RBNode<T> p = x.parent;
while (p != null && p.right == x) {
x = p;
p = x.parent;
}
return p;
}
return minNode(x.right);
}
/********* 查找节点x的前驱节点,即小于节点x的最大节点 ************/
public RBNode<T> predecessor(RBNode<T> x) {
if (x.left == null) {
RBNode<T> p = x.parent;
while (p != null && p.left == x) {
x = p;
p = x.parent;
}
return p;
}
return maxNode(x.left);
}
/**
* 左旋示意图:对节点x进行左旋
* p p
* / /
* x y
* / \ / \
* lx y -----> x ry
* / \ / \
* ly ry lx ly
* 左旋做了三件事:
* 1. 将y的左子节点赋给x的右子节点,并将x赋给y左子节点的父节点(y左子节点非空时)
* 2. 将x的父节点p(非空时)赋给y的父节点,同时更新p的子节点为y(左或右)
* 3. 将y的左子节点设为x,将x的父节点设为y
*/
private void leftRotate(RBNode<T> x){
RBNode<T> y = x.right;
x.right = y.left;
if(y.left != null){
y.left.parent = x;
}
y.parent = x.parent;
if(x.parent == null){
this.root = y;
}else {
if(x == x.parent.left){
x.parent.left = y;
}else {
x.parent.right = y;
}
}
y.left = x;
x.parent = y;
}
/**
* 右旋示意图:对节点y进行右旋
* p p
* / /
* y x
* / \ / \
* x ry -----> lx y
* / \ / \
* lx rx rx ry
* 右旋做了三件事:
* 1. 将x的右子节点赋给y的左子节点,并将y赋给x右子节点的父节点(x右子节点非空时)
* 2. 将y的父节点p(非空时)赋给x的父节点,同时更新p的子节点为x(左或右)
* 3. 将x的右子节点设为y,将y的父节点设为x
*/
private void rightRotate(RBNode<T> y){
RBNode<T> x = y.left;
y.left = x.right;
if(x.right != null){
x.right.parent = y;
}
x.parent = y.parent;
if(y.parent == null){
this.root = x;
}else {
if(y == y.parent.left){
y.parent.left = x;
}else {
y.parent.right = x;
}
}
x.right = y;
y.parent = x;
}
public void insert(T key){
RBNode<T> node = new RBNode<T>(RED,key,null,null,null);
if(node != null){
insert(node);
}
}
public void insert(RBNode<T> node){
RBNode<T> current = null;
RBNode<T> x = this.root;
while (x != null){
current = x;
int cmp = node.key.compareTo(x.key);
if(cmp < 0){
x = x.left;
}else {
x = x.right;
}
}
node.parent = current;
if(current != null){
int cmp = node.key.compareTo(current.key);
if(cmp < 0){
current.left = node;
}else{
current.right = node;
}
}else{
this.root = node;
}
insertFixUp(node);
}
private void insertFixUp(RBNode<T> node){
//定义父节点和祖父节点
RBNode<T> parent, gparent;
//需要修整的条件:父节点存在,且父节点的颜色是红色
while(((parent = parentOf(node)) != null) && isRed(parent)){
//获得祖父节点
gparent = parentOf(parent);
//若父节点是祖父节点的左子节点,下面else与其相反
if(parent == gparent.left){
// 获得叔叔节点
RBNode<T> uncle = gparent.right;
}
}
}
/*********************** 删除红黑树中的节点 **********************/
public void remove(T key){
RBNode<T> node;
/*if((node = search(key)) != null){
remove(node);
}*/
}
}