-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathred-black-tree.java
More file actions
201 lines (187 loc) · 6.09 KB
/
red-black-tree.java
File metadata and controls
201 lines (187 loc) · 6.09 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
package me.iwts;
class RBT<Key extends Comparable<Key>, Value>{
private static final boolean BLACK = false;
private static final boolean RED = true;
private Node root;
private class Node{
private Key key;
private Value value;
private Node left;
private Node right;
private boolean color;
public Node(Key key,Value value){
this.key = key;
this.value = value;
this.color = RED;
}
}
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 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;
}
// 左右旋以及颜色转换
private boolean isRed(Node node){
if(node == null) return false;
return node.color == RED;
}
private Node rotateLeft(Node h){
Node x = h.right;
h.right = x.left;
x.left = h;
x.color = h.color;
h.color = RED;
return x;
}
private Node rotateRight(Node h){
Node x = h.left;
h.left = x.right;
x.right = h;
x.color = h.color;
h.color = RED;
return x;
}
private void flipColors(Node h){
h.color = !h.color;
h.left.color = !h.left.color;
h.right.color = !h.right.color;
}
// 平衡算法,非常核心,下面三种情况按顺序跑,控制了红黑树的所有自平衡情况
private Node balance(Node root){
if(isRed(root.right)) root = rotateLeft(root);
if(isRed(root.left) && isRed(root.left.left)) root = rotateRight(root);
if(isRed(root.left) && isRed(root.right)) flipColors(root);
return root;
}
// 插入
public void put(Key key,Value value){
root = put(root,key,value);
root.color = BLACK;
}
private Node put(Node root,Key key,Value value){
if(root == null) return new Node(key,value);
int cmp = key.compareTo(root.key);
if(cmp == 0){
root.value = value;
}else{
if(cmp > 0){
root.right = put(root.right,key,value);
}else{
root.left = put(root.left,key,value);
}
}
if(isRed(root.right) && !isRed(root.left)) root = rotateLeft(root);
if(isRed(root.left) && isRed(root.left.left)) root = rotateRight(root);
if(isRed(root.left) && isRed(root.right)) flipColors(root);
return root;
}
// 删除最小值
private Node moveRedLeft(Node root){
flipColors(root);
if(isRed(root.right.left)){
root.right = rotateRight(root.right);
root = rotateLeft(root);
flipColors(root);
}
return root;
}
public void deleteMin(){
if(!isRed(root.left) && !isRed(root.right)) root.color = RED;
root = deleteMin(root);
if(root != null) root.color = BLACK;
}
private Node deleteMin(Node root){
if(root.left == null) return null;
if(!isRed(root.left) && !isRed(root.left.left)) root = moveRedLeft(root);
root.left = deleteMin(root.left);
return balance(root);
}
private Node moveRedRight(Node root){
flipColors(root);
if(isRed(root.left.left)){
root = rotateRight(root);
flipColors(root);
}
return root;
}
public void deleteMax(){
if(!isRed(root.left) && !isRed(root.right)) root.color = RED;
root = deleteMax(root);
if(root != null) root.color = BLACK;
}
private Node deleteMax(Node root){
if(isRed(root.left)) root = rotateRight(root);
if(root.right == null) return null;
if(!isRed(root.right) && !isRed(root.right.left)) root = moveRedRight(root);
root.right = deleteMax(root.right);
return balance(root);
}
public void delete(Key key){
if(!isRed(root.left) && !isRed(root.right)) root.color = RED;
root = delete(root,key);
if(root != null) root.color = BLACK;
}
private Node delete(Node root,Key key){
int cmp = key.compareTo(root.key);
if(cmp < 0){
// 左递归,跟删除最小值的一样
if(!isRed(root.left) && !isRed(root.left.left)) root = moveRedLeft(root);
root.left = delete(root.left,key);
}else{
// 右递归,先判定了是否需要删除最小值的第一次旋转保证不会丢失左子树
if(isRed(root.left)) root = rotateRight(root);
// 找到了,该删了,但是需要确保不会丢失右子树
if(cmp == 0 && (root.right == null)) return null;
// 这个是删除最大值中的算法
if(!isRed(root.right) && !isRed(root.right.left)) root = moveRedRight(root);
// 此时,完美解决所有丢失情况,可以正常删除了,这个过程实际上就是二叉搜索树树的删除
if(cmp == 0){
Node x = min(root.right);
root.key = x.key;
root.value = x.value;
root.right = deleteMin(root.right);
}else{
root.right = delete(root.right,key);
}
}
return balance(root);
}
}
public class Main{
public static void main(String[] args){
RBT<String,String> rbt = new RBT<>();
rbt.put("s","s");
rbt.put("e","e");
rbt.put("a","a");
rbt.put("r","r");
rbt.put("c","c");
rbt.put("h","h");
rbt.put("x","x");
rbt.put("m","m");
rbt.put("p","p");
rbt.put("l","l");
}
}