-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathRedBlackTree.java
216 lines (192 loc) · 4.87 KB
/
RedBlackTree.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
/*
* A red-black tree is a balanced binary tree that simulates a 2-3-4 tree.
* This implementation is a particular form of the red-black tree called the left leaning red-black tree
*
* Time complexity:
* - Remove: O(log N)
* - Insertion: O(log N)
* - Search: O(log N)
* - Access: O(log N)
*/
package codebook.datastructures;
public class RedBlackTree {
// constants for the color
private static final boolean RED = true;
private static final boolean BLACK = false;
// represents the root of the tree
private Node root;
public void remove(int key) {
// if both children of the root are black, set root to red
if (!isRed(root.left) && !isRed(root.right))
root.color = RED;
root = remove(root, key);
if (root != null)
root.color = BLACK;
}
private Node remove(Node n, int key) {
if (n == null)
return n;
if (key < n.key) {
if (!isRed(n.left) && !isRed(n.left.left))
n = shiftLeft(n);
n.left = remove(n.left, key);
} else {
if (isRed(n.left))
n = rotateRight(n);
if (key == n.key && n.right == null)
return null;
if (!isRed(n.right) && !isRed(n.right.left))
n = shiftRight(n);
if (key == n.key) {
Node x = getMinNode(n.right);
n.key = x.key;
n.value = x.value;
n.right = removeMinNode(n.right);
}
get(n.right, key);
}
return balance(n);
}
public boolean contains(int key) {
return contains(root, key);
}
private boolean contains(Node n, int key) {
if (n == null)
return false;
if (key < n.key)
return contains(n.left, key);
else if (key > n.key)
return contains(n.right, key);
else
return true;
}
public Integer get(int key) {
return get(root, key);
}
private Integer get(Node n, int key) {
if (n == null)
return null;
if (key < n.key)
get(n.left, key);
else if (key > n.key)
get(n.right, key);
return n.value;
}
public void add(int key, int value) {
root = add(root, key, value);
root.color = BLACK;
}
private Node add(Node n, int key, int value) {
if (n == null)
return new Node(key, value, RED);
if (key < n.key)
n.left = add(n.left, key, value);
else if (key > n.key)
n.right = add(n.right, key, value);
else
n.value = value;
// fix any right-leaning links
if (isRed(n.right) && !isRed(n.left))
n = rotateLeft(n);
if (isRed(n.left) && isRed(n.left.left))
n = rotateRight(n);
if (isRed(n.left) && isRed(n.right))
flipColors(n);
return n;
}
private Node rotateLeft(Node n) {
Node x = n.right;
n.right = x.left;
x.left = n;
x.color = n.color;
n.color = RED;
return x;
}
private Node rotateRight(Node n) {
Node x = n.left;
n.left = x.right;
x.right = n;
x.color = n.color;
n.color = RED;
return x;
}
private void flipColors(Node n) {
n.color = RED;
n.left.color = n.right.color = BLACK;
}
private boolean isRed(Node n) {
if (n == null)
return false;
return n.color == RED;
}
// restore red-black tree invariant
private Node balance(Node n) {
if (isRed(n.right))
n = rotateLeft(n);
if (isRed(n.left) && isRed(n.left.left))
n = rotateRight(n);
if (isRed(n.left) && isRed(n.right))
flipColors(n);
return n;
}
// Assuming that h is red and both h.left and h.left.left
// are black, make h.left or one of its children red.
private Node shiftLeft(Node n) {
flipColors(n);
if (isRed(n.right.left)) {
n.right = rotateRight(n.right);
n = rotateLeft(n);
flipColors(n);
}
return n;
}
// Assuming that h is red and both h.right and h.right.left
// are black, make h.right or one of its children red.
private Node shiftRight(Node n) {
flipColors(n);
if (isRed(n.left.left)) {
n = rotateRight(n);
flipColors(n);
}
return n;
}
public Node getMinNode(Node n) {
Node curr = n;
while (curr.left != null)
curr = curr.left;
return curr;
}
public Node getMaxNode(Node n) {
Node curr = n;
while (curr.right != null)
curr = curr.right;
return curr;
}
private Node removeMinNode(Node n) {
if (n.left == null)
return null;
if (!isRed(n.left) && !isRed(n.left.left))
n = shiftLeft(n);
n.left = removeMinNode(n.left);
return balance(n);
}
private Node removeMaxNode(Node n) {
if (n.right == null)
return null;
if (!isRed(n.right) && !isRed(n.right.left))
n = shiftRight(n);
n.right = removeMaxNode(n.right);
return balance(n);
}
// object representing the nodes of the tree
private class Node {
int key, value;
Node left, right;
boolean color;
Node(int key, int value, boolean color) {
this.key = key;
this.value = value;
this.color = color;
}
}
}