-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedBlackTree.cs
243 lines (213 loc) · 6.17 KB
/
RedBlackTree.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
namespace RedBlackTree;
public enum Side
{
Left, Right,
}
public enum Color
{
Red, Black,
}
public static class SideExtensions
{
public static Side Switch(this Side side)
=> side switch
{
Side.Left => Side.Right,
Side.Right => Side.Left,
_ => throw new ArgumentOutOfRangeException(nameof(side), side, null),
};
}
public class TreeNode<T> where T : IComparable<T>
{
public T Value;
public Color Color;
public TreeNode<T> Left;
public TreeNode<T> Right;
public TreeNode<T> Parent;
public bool IsRoot;
internal TreeNode(T value, TreeNode<T> parent = null, bool isRoot = false)
{
Value = value;
Parent = parent;
if (isRoot)
{
IsRoot = true;
Color = Color.Black;
}
}
internal TreeNode<T> GetChildNode(Side side)
{
var node = side switch
{
Side.Left => this.Left,
Side.Right => this.Right,
_ => throw new ArgumentException(null, nameof(side)),
};
return node;
}
internal void ChangeColor()
{
this.Color = this.Color switch
{
Color.Red => Color.Black,
Color.Black => Color.Red,
_ => throw new ArgumentException(null, nameof(Color)),
};
}
internal Side? CheckColorMatch()
{
if (Left != null && Color == Color.Red && Color == Left.Color)
return Side.Left;
if (Right != null && Color == Color.Red && Color == Right.Color)
return Side.Right;
return null;
}
}
public class RedBlackTree<T> where T : IComparable<T>
{
public TreeNode<T> Root;
public uint Length { get; private set; }
public RedBlackTree()
{
Root = null;
Length = 0;
}
public bool Insert(T value)
{
bool inserted = false;
if (Root != null)
{
inserted = InsertNode(Root, value, inserted);
}
else
{
Root = new TreeNode<T>(value, isRoot: true);
inserted = true;
}
if (inserted)
{
Length++;
}
return inserted;
}
private bool InsertNode(TreeNode<T> node, T value, bool inserted)
{
TreeNode<T> insertedNode = null;
if (node.Value.CompareTo(value) == 0)
{
inserted = false;
return inserted;
}
if (value.CompareTo(node.Value) < 0)
{
if (node.Left != null)
inserted = InsertNode(node.Left, value, inserted);
else
{
inserted = true;
node.Left = new TreeNode<T>(value, parent: node);
insertedNode = node.Left;
}
}
else
{
if (node.Right != null)
inserted = InsertNode(node.Right, value, inserted);
else
{
inserted = true;
node.Right = new TreeNode<T>(value, parent: node);
insertedNode = node.Right;
}
}
if (inserted)
{
var colorMatch = node.CheckColorMatch();
if (colorMatch.HasValue)
{
CheckForRebalancing(insertedNode, colorMatch.Value);
}
}
return inserted;
}
private void CheckForRebalancing(TreeNode<T> node, Side side)
{
if (node.Parent == null) return;
if (node.Color == Color.Red && node.Parent.Color == Color.Red)
{
TreeNode<T> grandParent = node.Parent?.Parent;
TreeNode<T> uncle = null;
if (grandParent != null)
{
if (node.Parent == grandParent.Left)
uncle = grandParent.Right;
else
uncle = grandParent.Left;
}
if (uncle != null && uncle.Color == Color.Red)
{
node.Parent.Color = Color.Black;
uncle.Color = Color.Black;
if (grandParent != null)
{
grandParent.Color = grandParent.IsRoot ? Color.Black : Color.Red;
CheckForRebalancing(grandParent, side);
}
}
else
{
if (uncle == null || uncle.Color == Color.Black)
{
if (grandParent != null)
{
if (node.Parent != grandParent.GetChildNode(side))
{
Rotate(side, node.Parent);
side = side.Switch();
}
Rotate(side, grandParent);
node.ChangeColor();
node.GetChildNode(side.Switch()).ChangeColor();
}
}
Root.Color = Color.Black; // Ensure root is always black
}
}
}
private void Rotate(Side side, TreeNode<T> node)
{
// Save child and grandchild nodes
TreeNode<T> child = node.GetChildNode(side);
TreeNode<T> grandChild = child?.GetChildNode(side.Switch());
// Perform rotation
if (node.IsRoot)
{
Root = child;
child.Parent = null;
child.IsRoot = true;
}
else
{
TreeNode<T> parent = node.Parent;
if (parent.Left == node)
parent.Left = child;
else
parent.Right = child;
child.Parent = parent;
}
node.Parent = child;
if (side == Side.Left)
{
child.Right = node;
node.Left = grandChild;
}
else
{
child.Left = node;
node.Right = grandChild;
}
// Reassign grandchild
if (grandChild != null)
grandChild.Parent = node;
}
}