public void GetSiblingFindsNoSiblingWhenNoneExists() { PointerBackedBinaryTreeNode <string> testNode = new PointerBackedBinaryTreeNode <string>("abc", null); testNode.Left = new PointerBackedBinaryTreeNode <string>("def", testNode); Assert.IsNull(testNode.GetSibling()); Assert.IsNull(testNode.Left.GetSibling()); }
private void DeleteRepair(PointerBackedBinaryTreeNode <RedBlackTreeNode <T> > node) { if (node.Parent != null) { // If this node isn't the root, we need to... PointerBackedBinaryTreeNode <RedBlackTreeNode <T> > sibling = node.GetSibling(); if (!sibling.Data.IsBlack) { // ... reverse colors sibling.Parent.Data.IsBlack = false; sibling.Data.IsBlack = true; // .... and rotate around if (node.Parent.Left == node) { this.RotateLeft(node.Parent); } else { this.RotateRight(node.Parent); } } // Once we do that, we need to possibly ... if (sibling.Data.IsBlack && sibling.Left.Data.IsBlack && sibling.Right.Data.IsBlack) { // ... repeat recursivly or color theparent sibling.Data.IsBlack = false; if (node.Parent.Data.IsBlack) { DeleteRepair(node.Parent); } else { node.Parent.Data.IsBlack = false; } } else { // ... or perform more rotations with coloration swaps if (sibling.Data.IsBlack) { // Force subsequent rotations to 'do the right thing' if (node.Parent.Left == node && sibling.Right.Data.IsBlack && !sibling.Left.Data.IsBlack) { sibling.Data.IsBlack = false; sibling.Left.Data.IsBlack = true; this.RotateRight(sibling); } else if (node.Parent.Right == node && sibling.Left.Data.IsBlack && !sibling.Right.Data.IsBlack) { sibling.Data.IsBlack = false; sibling.Right.Data.IsBlack = true; this.RotateLeft(sibling); } } sibling.Data.IsBlack = node.Parent.Data.IsBlack; node.Parent.Data.IsBlack = true; if (node.Parent.Left == node) { sibling.Right.Data.IsBlack = true; this.RotateLeft(node.Parent); } else { sibling.Left.Data.IsBlack = true; this.RotateRight(node.Parent); } } } else { this.Root = node; } }