private static RBNode NewNullNode() { var node = new RBNode(default(T)); node.ColorBlack(); return(node); }
private void DeleteFix(RBNode node) { while (node != Root && node.IsBlack) { if (node == node.Parent.LeftChild) { var sibling = node.Parent.RightChild; if (sibling.IsRed) { sibling.ColorBlack(); node.Parent.ColorRed(); RotateLeft(node.Parent); sibling = node.Parent.RightChild; } if (sibling.LeftChild.IsBlack && sibling.RightChild.IsBlack) { sibling.ColorRed(); node = node.Parent; } else { if (sibling.RightChild.IsBlack) { sibling.ColorRed(); sibling.LeftChild.ColorBlack(); RotateRight(sibling); sibling = node.Parent.RightChild; } sibling.RightChild.ColorBlack(); if (node.Parent.IsBlack) { sibling.ColorBlack(); } else { sibling.ColorRed(); } node.Parent.ColorBlack(); RotateLeft(node.Parent); node = Root; } } else // Repeat for RightChild case { var sibling = node.Parent.LeftChild; if (sibling.IsRed) { sibling.ColorBlack(); node.Parent.ColorRed(); RotateRight(node.Parent); sibling = node.Parent.LeftChild; } if (sibling.RightChild.IsBlack && sibling.LeftChild.IsBlack) { sibling.ColorRed(); node = node.Parent; } else { if (sibling.LeftChild.IsBlack) { sibling.ColorRed(); sibling.RightChild.ColorBlack(); RotateLeft(sibling); sibling = node.Parent.LeftChild; } sibling.LeftChild.ColorBlack(); if (node.Parent.IsBlack) { sibling.ColorBlack(); } else { sibling.ColorRed(); } node.Parent.ColorBlack(); RotateRight(node.Parent); node = Root; } } } node.ColorBlack(); }