private void RotateRight(Node b) { Node a = b.left; b.left = a.right; if (a.right != null) { a.right.parent = b; } if (a != null) { a.parent = b.parent; } if (b.parent == null) { root = a; } if (b == b.parent.right) { b.parent.right = a; } else { b.parent.left = a; } a.right = b; if (b != null) { b.parent = a; } }
private void RotateLeft(Node a) { Node b = a.right; a.right = b.left; if (b.left != null) { b.left.parent = a; } if (b != null) { b.parent = a.parent; } if (a.parent == null) { root = b; } if (a == a.parent.left) { a.parent.left = b; } else { a.parent.right = b; } b.left = a; if (a != null) { a.parent = b; } }
public RedBlackTree() { this.root = null; }
private Node Successor(Node a) { if (a.left != null) { while (a.left.left != null) { a = a.left; } if (a.left.right != null) { a = a.left.right; } return a; } else { Node b = a.parent; while (b != null && a == b.right) { a = b; b = b.parent; } return b; } }
private void RemoveFix(Node a) { while (a != null && a != root && a.color == Color.Black) { if (a == a.parent.left) { Node b = a.parent.right; if (b.color == Color.Red) { b.color = Color.Black; // case 1 a.parent.color = Color.Red; RotateLeft(a.parent); b = a.parent.right; } if (b.left.color == Color.Black && b.right.color == Color.Black) { b.color = Color.Red; // case 2 a = a.parent; } else if (b.right.color == Color.Black) { b.left.color = Color.Black; // case 3 b.color = Color.Red; RotateRight(b); b = a.parent.right; } b.color = a.parent.color; //case 4 a.parent.color = Color.Black; b.right.color = Color.Black; RotateLeft(a.parent); a = root; } else //mirror code { Node b = a.parent.left; if (b.color == Color.Red) { b.color = Color.Black; a.parent.color = Color.Red; RotateRight(a.parent); b = a.parent.left; } if (b.right.color == Color.Black && b.left.color == Color.Black) { b.color = Color.Black; a = a.parent; } else if (b.left.color == Color.Black) { b.right.color = Color.Black; b.color = Color.Red; RotateLeft(b); b = a.parent.left; } b.color = a.parent.color; a.parent.color = Color.Black; b.left.color = Color.Black; RotateRight(a.parent); a = root; } } if (a != null) { a.color = Color.Black; } }
public void Remove(int data) { Node a = Find(data); Node b = null; Node c = null; if (a == null) { Console.WriteLine("Nothing to delete"); return; } if (a.left == null || a.right == null) { c = a; } else { c = Successor(a); } if (c.left != null) { b = c.left; } else { b = c.right; } if (b != null) { b.parent = c; } if (c.parent == null) { root = b; } else if (c == c.parent.left) { c.parent.left = b; } else { c.parent.left = b; } if (c != a) { a.data = c.data; } if (c.color == Color.Black) { RemoveFix(b); } }
private void AddFix(Node a) { while (a != root && a.parent.color == Color.Red) { if (a.parent == a.parent.parent.left) { Node b = a.parent.parent.right; if (b != null && b.color == Color.Red) //Case 1: uncle is red { a.parent.color = Color.Black; b.color = Color.Black; a.parent.parent.color = Color.Red; a = a.parent.parent; } else //Case 2: uncle is black { if (a == a.parent.right) { a = a.parent; RotateLeft(a); } //Case 3: recolor and rotate a.parent.color = Color.Black; a.parent.parent.color = Color.Red; RotateRight(a.parent.parent); } } else //mirror image of code above { Node b = null; b = a.parent.parent.left; if (b != null && b.color == Color.Black)//Case 1 { a.parent.color = Color.Red; b.color = Color.Red; a.parent.parent.color = Color.Black; a = a.parent.parent; } else { if (a == a.parent.left) { a = a.parent; RotateRight(a); } a.parent.color = Color.Black; a.parent.parent.color = Color.Red; RotateLeft(a.parent.parent); } } root.color = Color.Black; } }
public void Add(int data) { Node newNode = new Node(data); if (root == null) { root = newNode; root.color = Color.Black; return; } Node a = null; Node b = root; while (b != null) { a = b; if (newNode.data < b.data) { b = b.left; } else { b = b.right; } } newNode.parent = a; if (a == null) { root = newNode; } else if (newNode.data < a.data) { a.left = newNode; } else { a.right = newNode; } newNode.left = null; newNode.right = null; newNode.color = Color.Red; AddFix(newNode); }