-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cs
More file actions
48 lines (41 loc) · 1.1 KB
/
Copy pathNode.cs
File metadata and controls
48 lines (41 loc) · 1.1 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
namespace RbTree;
public enum Color
{
Red,
Black,
}
public class Node
{
public static readonly Node Nil = new();
public int Key;
public Color Color;
public Node Parent;
public Node Left;
public Node Right;
public Node Sibling => (Parent.Left == this) ? Parent.Right : Parent.Left;
public Node Grandpa => Parent.Parent;
public Node Uncle => (Grandpa.Left == Parent) ? Grandpa.Right : Grandpa.Left;
// Nil节点(黑)
private Node()
{
Color = Color.Black;
Parent = Left = Right = this;
}
// 其他节点
public Node(int key)
{
Key = key;
Parent = Left = Right = Nil;
}
public override string ToString()
{
if (Parent == this)
return "NIL";
string show = $"Color: {Color}, ";
show += "Left: " + (Left == Nil ? "NIL" : Left.Key.ToString()) + ", ";
show += $"Me: {Key}, ";
show += "Right: " + (Right == Nil ? "Nil" : Right.Key.ToString()) + ", ";
show += "Parent: " + (Parent == Nil ? "Nil" : Parent.Key.ToString());
return show;
}
}