Skip to content

Commit b1075ce

Browse files
committed
red black tree add
1 parent 7e87a3a commit b1075ce

File tree

2 files changed

+186
-19
lines changed
  • myutils-algorithm/src
    • main/java/org/easyarch/myutils/algorithm/struct/tree/btree
    • test/java/org/easyarch/myutils/algorithm/sort/tree

2 files changed

+186
-19
lines changed

myutils-algorithm/src/main/java/org/easyarch/myutils/algorithm/struct/tree/btree/RBTree.java

Lines changed: 161 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.easyarch.myutils.algorithm.struct.tree.btree;
22

3+
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
34
import com.sun.org.apache.regexp.internal.RE;
45

56
/**
@@ -9,14 +10,15 @@ public class RBTree<E extends Comparable> {
910

1011
private static final boolean RED = false;
1112

12-
private static final boolean BLACK = false;
13+
private static final boolean BLACK = true;
1314

1415
//所有叶子节点默认都是nil节点,黑色
15-
private final RBNode NIL = new RBNode<>(null,null,null,null,true);
16+
private final RBNode NIL = new RBNode<>(null,null,null,null,BLACK);
1617

1718
private RBNode<E> root;
1819

1920
public void add(E elem){
21+
System.out.println("add elem:"+elem);
2022
if (elem == null){
2123
return;
2224
}
@@ -35,39 +37,166 @@ private void add(RBNode<E> currentNode,E elem){
3537
return;
3638
}
3739
if (elem.compareTo(currentNode.elem) > 0){
38-
if (currentNode.right == null){
40+
if (currentNode.right == NIL){
3941
currentNode.right = new RBNode(NIL,elem,NIL,currentNode, RED);
40-
42+
rebalance(currentNode.right);
4143
}else{
4244
add(currentNode.right,elem);
4345
}
4446
}else if (elem.compareTo(currentNode.elem) < 0){
45-
if (currentNode.left == null){
47+
if (currentNode.left == NIL){
4648
currentNode.left = new RBNode(NIL,elem,NIL,currentNode,RED);
47-
49+
rebalance(currentNode.left);
4850
}else{
4951
add(currentNode.left,elem);
5052
}
5153
}
5254
}
5355

56+
/**
57+
* @param currentNode 表示当前判定节点
58+
*/
5459
private void rebalance(RBNode<E> currentNode){
55-
//父亲节点为黑色则不作任何处理
56-
if (currentNode.color){
60+
if (currentNode == this.root) {
61+
//根节点为红色
62+
if (!this.root.color){
63+
this.root.color = BLACK;
64+
}
5765
return;
5866
}
59-
if (currentNode.parent != null){
60-
RBNode<E> cParent = currentNode.parent;
61-
if (currentNode == cParent.left){
62-
RBNode<E> cUncle = cParent.right;
63-
}else if (currentNode == cParent.right){
64-
RBNode<E> cUncle = cParent.left;
67+
RBNode<E> cParent = currentNode.parent;
68+
if (cParent == null){
69+
System.out.println("cParent is null,root is"+this.root.elem+",current is:"+currentNode.elem);
70+
}
71+
//父节点为黑色
72+
if (cParent.color){
73+
return;
74+
}
75+
if (cParent != null){
76+
//黑父,无需处理
77+
if (cParent.color){
78+
return;
79+
}
80+
RBNode<E> cUncle;
81+
RBNode<E> cGrand = cParent.parent;
82+
if (cGrand.left == cParent){
83+
cUncle = cGrand.right;
6584
}else{
66-
85+
cUncle = cGrand.left;
86+
}
87+
//叔节点是红色
88+
if (!cUncle.color){
89+
cParent.color = BLACK;
90+
cGrand.color = RED;
91+
cUncle.color = BLACK;
92+
rebalance(cGrand);
93+
return;
94+
}
95+
//叔节点是黑色
96+
if (cParent == cGrand.left){
97+
//父节点在祖节点左边
98+
if (currentNode == cParent.left){
99+
//新节点在父节点左边,此时右旋。祖父变红,父亲变黑,当前节点仍然是红色
100+
currentNode.color = RED;
101+
cGrand.color = RED;
102+
cParent.color= BLACK;
103+
rightRotate(cGrand);
104+
}else{
105+
//新节点在父节点右边,先左旋再右旋。祖父变红,当前节点变黑,父亲仍然是红色
106+
cParent.color = RED;
107+
cGrand.color = RED;
108+
currentNode.color = BLACK;
109+
leftRotate(cParent);
110+
rightRotate(cGrand);
111+
}
112+
}else{
113+
//父节点在祖节点右边
114+
if(currentNode == cParent.right){
115+
//新节点在父节点右边,此时左旋.
116+
cGrand.color = RED;
117+
cParent.color= BLACK;
118+
currentNode.color = RED;
119+
leftRotate(cGrand);
120+
}else{
121+
//新节点在父节点左边,先右旋再左旋
122+
cParent.color = RED;
123+
cGrand.color = RED;
124+
currentNode.color = BLACK;
125+
rightRotate(cParent);
126+
leftRotate(cGrand);
127+
}
67128
}
68-
69129
}
70130
}
131+
//
132+
// private void rebalance(RBNode<E> currentNode,RBNode<E> newNode){
133+
// //父亲节点为黑色则不作任何处理
134+
// if (currentNode.color){
135+
// return;
136+
// }
137+
// if (currentNode == this.root){
138+
// //根节点为红色
139+
// if (!this.root.color){
140+
// this.root.color = BLACK;
141+
// }
142+
// }
143+
// if (currentNode.parent != null){
144+
// RBNode<E> cParent = currentNode.parent;
145+
// //黑父,无需处理
146+
// if (currentNode.color){
147+
// return ;
148+
// }
149+
// //父是红色,此时要看叔节点的颜色
150+
// RBNode<E> cUncle;
151+
// if (currentNode == cParent.left){
152+
// cUncle = cParent.right;
153+
// }else{
154+
// cUncle = cParent.left;
155+
// }
156+
// //叔节点是红色
157+
// if (!cUncle.color){
158+
// currentNode.color = BLACK;
159+
// cParent.color = RED;
160+
// cUncle.color = BLACK;
161+
// rebalance(cParent,newNode);
162+
// return;
163+
// }
164+
// //叔节点是黑色
165+
// if (currentNode == cParent.left){
166+
// //父节点在祖节点左边
167+
// if (newNode == currentNode.left){
168+
// //新节点在父节点左边,此时右旋。祖父变红,父亲变黑,当前节点仍然是红色
169+
// cParent.color = RED;
170+
// currentNode.color= BLACK;
171+
// newNode.color = RED;
172+
// rightRotate(cParent);
173+
// }else{
174+
// //新节点在父节点右边,先左旋再右旋。祖父变红,当前节点变黑,父亲仍然是红色
175+
// cParent.color = RED;
176+
// newNode.color = BLACK;
177+
// currentNode.color = RED;
178+
// leftRotate(currentNode);
179+
// rightRotate(cParent);
180+
// }
181+
// }else{
182+
// //父节点在祖节点右边
183+
// if(newNode == currentNode.right){
184+
// //新节点在父节点右边,此时左旋.
185+
// cParent.color = RED;
186+
// currentNode.color= BLACK;
187+
// newNode.color = RED;
188+
// leftRotate(cParent);
189+
// }else{
190+
// //新节点在父节点左边,先右旋再左旋
191+
// cParent.color = RED;
192+
// newNode.color = BLACK;
193+
// currentNode.color = RED;
194+
// rightRotate(currentNode);
195+
// leftRotate(cParent);
196+
// }
197+
// }
198+
// }
199+
// }
71200

72201
private void leftRotate(RBNode<E> currentNode){
73202
RBNode<E> cRight = currentNode.right;
@@ -83,15 +212,15 @@ private void leftRotate(RBNode<E> currentNode){
83212
cRight.parent = cParent;
84213
}else{
85214
//当前节点为根节点的情况
86-
cRight.parent = NIL;
215+
cRight.parent = null;
87216
this.root = cRight;
88217
}
89218
//当前节点包括一个左分支的情况
90219
if (cRight.left != null){
91220
currentNode.right = cRight.left;
92221
cRight.left.parent = currentNode;
93222
}else{
94-
currentNode.right = null;
223+
currentNode.right = NIL;
95224
}
96225
cRight.left = currentNode;
97226
currentNode.parent = cRight;
@@ -116,12 +245,25 @@ private void rightRotate(RBNode<E> currentNode){
116245
currentNode.left = cLeft.right;
117246
cLeft.right.parent = currentNode;
118247
}else{
119-
currentNode.parent = null;
248+
currentNode.parent = NIL;
120249
}
121250
cLeft.right = currentNode;
122251
currentNode.parent = cLeft;
123252
}
124253

254+
public void iterate(){
255+
iterate(root);
256+
}
257+
258+
private void iterate(RBNode node){
259+
if (node == null){
260+
return ;
261+
}
262+
System.out.println(node.elem);
263+
iterate(node.left);
264+
iterate(node.right);
265+
}
266+
125267
private class RBNode<E extends Comparable> {
126268

127269
private RBNode<E> left;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.easyarch.myutils.algorithm.sort.tree;
2+
3+
import org.easyarch.myutils.algorithm.struct.tree.btree.RBTree;
4+
5+
/**
6+
* Created by xingtianyu on 2018/4/18.
7+
*/
8+
public class BTreeTest {
9+
10+
public static void main(String[] args) {
11+
RBTree<User> tree = new RBTree<>();
12+
tree.add(new User("aaa",1));
13+
tree.add(new User("bbb",2));
14+
tree.add(new User("ccc",3));
15+
tree.add(new User("ddd",4));
16+
tree.add(new User("eee",5));
17+
tree.add(new User("fff",6));
18+
tree.add(new User("ggg",7));
19+
tree.add(new User("hhh",8));
20+
tree.add(new User("hhh",9));
21+
tree.add(new User("hhh",10));
22+
23+
tree.iterate();
24+
}
25+
}

0 commit comments

Comments
 (0)