public RBTNode FlipColors(RBTNode head) { //only flip if parent node is not red & both child nodes are red if (!head.red && head.left.red && head.right.red) { //if parent is the root, which always has to be black, don't flip the color if (head != root) { head.red = true; } head.left.red = false; head.right.red = false; } return head; }
public RBTNode Insert(RBTNode head, int value) { RBTNode node; //if empty tree, create root/leaf node, which is always black if (head == null) { node = new RBTNode(value, false); //if root node hasn't been set, then assume empty tree and set it now if (root == null) { root = node; } return node; } else { //Standard Binary Search Tree Insert if (value < head.data) { head.left = Insert(head.left, value); } else if (value > head.data) { head.right = Insert(head.right, value); } else { head.data = value; } } //if we have a red node on the right, then rotate left to //maintain the the left-leaning red property. if (head.right.red && !head.left.red) { head = RotateLeft(head); } //case where we have 2 consecutive red nodes if (head.left.red && head.left.left.red) { head = RotateRight(head); } //case where we have a node with 2 red links; if (head.left.red && head.right.red) { head = FlipColors(head); } return head; }
public RBTNode RotateRight(RBTNode head) { RBTNode tmp = head; if (head.left.red) { tmp = head.left; head.left = tmp.right; tmp.right = head; tmp.red = head.red; if (head == root) { //rotated the root node out, update root node and remain black root = tmp; tmp.red = false; } head.red = true; } return tmp; }