-
Notifications
You must be signed in to change notification settings - Fork 0
/
RbTree.cs
283 lines (255 loc) · 10.1 KB
/
RbTree.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RbTreeLibrary
{
public class RBTree<T>
{
public IComparer<T> Comparer { get; protected set; }
public TreeNode<T> Root;
public int Depth { get; protected set; } // black depth;
public RBTree()
{
this.Comparer = Comparer<T>.Default;
this.Root = null;
this.Depth = 0;
}
public RBTree(IComparer<T> comparer)
{
this.Comparer = comparer;
this.Root = null;
this.Depth = 0;
}
public bool BinarySearchFirst(T item, out TreeNode<T> first, out NodeDirection direction)
=> BinarySearch(item, out first, out direction);
//public bool BinarySearchLeft(T item, out TreeNode<T> lessThanOrEqual)
// => BinarySearch(item, out lessThanOrEqual, out );
internal bool BinarySearch(T item, out TreeNode<T> output, out NodeDirection lastMove)
{
TreeNode<T> cur = this.Root;
lastMove = NodeDirection.None;
if(cur == null) { output = null; return false; }
while (true)
{
int cmp = Comparer.Compare(item, cur.Item);
if (cmp == 0) { output = cur; return true; }
else if (cmp > 0)
{
if (cur.Right == null)
{
lastMove = NodeDirection.Right;
break;
}
cur = cur.Right;
}
else
{
if (cur.Left == null)
{
lastMove = NodeDirection.Left;
break;
}
cur = cur.Left;
}
}
output = cur;
return false;
}
public bool Add(T item)
{
if (BinarySearchFirst(item, out var first, out var direction))
return false;
if (first == null)
{
Root = new TreeNode<T>(item) { Color = RbColor.Black };
Depth++;
return true;
}
var added = new TreeNode<T>(item) { Color = RbColor.Red }; // to make sure
if (direction == NodeDirection.Left)
first.AddChildLeft(added);
else if (direction == NodeDirection.Right)
first.AddChildRight(added);
else throw new InvalidOperationException("should have returned true in binary search");
// Try Recoloring and Shifting
var child = added;
while (true) // child will be always red in this loop
{
var parent = child.Parent;
if (parent == null)
{
child.Color = RbColor.Black;
Depth++;
break;
}
if (parent.Color == RbColor.Black) break;
// now parent color is red, so it cannot be the root
if (parent.Parent == null) throw new InvalidOperationException("RB Tree violated");
var uncle = parent.Parent.Left;
bool parentLeft = false;
if (uncle == parent)
{
uncle = parent.Parent.Right;
parentLeft = true;
}
if ( !IsBlack(uncle) )
{
parent.Color = RbColor.Black;
uncle.Color = RbColor.Black;
child = parent.Parent; // proceed to grandparent;
child.Color = RbColor.Red;
continue;
}
// now uncle color = Black,
// switch into 4 cases accridng to uncleLeft, childLeft
bool childLeft = parent.Left == child;
// LL case
// g : Black p : changed to Black
// p:Red u:Black => c:Red g : changed to Red
// c:Red x:(Black by assumption) x:Black u :Black
if (parentLeft) // case LL
{
//case LR
if (!childLeft)
parent = parent.ShiftLeft(); // this reduces to case LL
//case LL and LR
parent = parent.Parent.ShiftRight();
if (parent.Parent == null) Root = parent;
parent.Color = RbColor.Black;
parent.Right.Color = RbColor.Red;
break;
}
else
{
// case RL
if (childLeft)
parent = parent.ShiftRight();
// case RR and RL
parent = parent.Parent.ShiftLeft();
if (parent.Parent == null) Root = parent;
parent.Color = RbColor.Black;
parent.Left.Color = RbColor.Red;
break;
}
}
return true;
}
public bool Remove(T item)
{
if (!BinarySearchFirst(item, out var first, out _))
return false;
return Remove(first);
}
public bool Remove(TreeNode<T> item)
{
var parent = item.Parent; // save parent first;
var succ = item.RemoveSelf(out var type, out var selfNextParent,
out var selfNextChild);
if (parent == null)
Root = succ;
switch (type)
{
case TreeRemovalType.LeftNull:
case TreeRemovalType.RightNull:
if (item.Color == RbColor.Black)
RemoveFixup(succ, parent); // in case succ is null
break;
case TreeRemovalType.RightSuccessor:
var removed_color = succ.Color;
succ.Color = item.Color;
if (removed_color == RbColor.Black)
RemoveFixup(selfNextChild, selfNextParent);
break;
default:
throw new NotImplementedException("Unknown Removal type");
}
return true;
}
private bool IsBlack(TreeNode<T> node)
=> node == null || node.Color == RbColor.Black;
private TreeNode<T> Sibling(TreeNode<T> child, TreeNode<T> parent)
{
if (parent == null) return null;
if (parent.Left == child) return parent.Right;
else if (parent.Right == child) return parent.Left;
else throw new ArgumentException();
}
// node is double black..
private void RemoveFixup(TreeNode<T> extrablack, TreeNode<T> parent)
{
while (IsBlack(extrablack) && parent != null)
{
var sibling = Sibling(extrablack, parent);
if (IsBlack(sibling.Left) && IsBlack(sibling.Right)) // case 1 and 2
{
if (!IsBlack(sibling)) // case 1
{
if (parent.Left == extrablack) parent.ShiftLeft();
else parent.ShiftRight();
parent.Color = RbColor.Red;
sibling.Color = RbColor.Black;
if (Root == parent) Root = sibling;
continue;
}
else // case 2
{
sibling.Color = RbColor.Red;
extrablack = parent;
parent = extrablack.Parent;
continue;
}
}
//case 3
else if (extrablack == parent.Left && IsBlack(sibling.Right)) // and left = Red
{
sibling.ShiftRight();
sibling.Color = RbColor.Red;
sibling.Parent.Color = RbColor.Black;
continue;
}
else if (extrablack == parent.Right && IsBlack(sibling.Left)) // mirror of previous
{
sibling.ShiftLeft();
sibling.Color = RbColor.Red;
sibling.Parent.Color = RbColor.Black;
continue;
}
// case 4
else
{
if (parent.Left == extrablack)
{
//parent.Right.Right is Red, so it is not null
parent.Right.Right.Color = RbColor.Black;
parent.ShiftLeft();
}
else
{
parent.Left.Left.Color = RbColor.Black;
parent.ShiftRight();
}
if (Root == parent)
Root = sibling;
parent.Parent.Color = parent.Color;
parent.Color = RbColor.Black;
Depth++;
extrablack = Root;
parent = null;
}
}
if (extrablack == null) // should only reacheable when no element left
{
if (parent != null) throw new InvalidOperationException("Tree has invalid state");
Depth = 0;
Root = null;
return;
}
if (extrablack.Color == RbColor.Red)
extrablack.Color = RbColor.Black;
else
Depth--; // node is already root
}
}
}