GENERIC RED-BLACK TREE AND ITS <strong>C#</strong> IMPLEMENTATION}} else {node.Left = DeleteLeftMost(node.Left, node);return node;}private T LeftMost(Node node) {if (node.Left == null) {return node.Item;} else {return LeftMost(node.Left);}}private void Fix<strong>Tree</strong>AfterDeletion(Node curNode, Node parent,Node sibling, Node gr<strong>and</strong>Parent) {Node siblingLeftChild = null;Node siblingRightChild = null;if (sibling != null && sibling.Left != null) {siblingLeftChild = sibling.Left;}if (sibling != null && sibling.Right != null) {siblingRightChild = sibling.Right;}bool sibling<strong>Red</strong> = (sibling != null && sibling.<strong>Red</strong>);bool siblingLeft<strong>Red</strong> = (siblingLeftChild != null&& siblingLeftChild.<strong>Red</strong>);bool siblingRight<strong>Red</strong> = (siblingRightChild != null &&siblingRightChild.<strong>Red</strong>);}if (parent != null && !parent.<strong>Red</strong> && sibling<strong>Red</strong> &&!siblingLeft<strong>Red</strong> && !siblingRight<strong>Red</strong>) {Case1(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (parent != null && !parent.<strong>Red</strong> &&!sibling<strong>Red</strong> && !siblingLeft<strong>Red</strong> && !siblingRight<strong>Red</strong>) {Case2A(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (parent != null && parent.<strong>Red</strong> &&!sibling<strong>Red</strong> && !siblingLeft<strong>Red</strong> && !siblingRight<strong>Red</strong>) {Case2B(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (siblingToRight && !sibling<strong>Red</strong> &&siblingLeft<strong>Red</strong> && !siblingRight<strong>Red</strong>) {Case3(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (!siblingToRight &&!sibling<strong>Red</strong> && !siblingLeft<strong>Red</strong> && siblingRight<strong>Red</strong>) {Case3P(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (siblingToRight && !sibling<strong>Red</strong> &&siblingRight<strong>Red</strong>) {Case4(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (!siblingToRight && !sibling<strong>Red</strong> &&siblingLeft<strong>Red</strong>) {Case4P(curNode, parent, sibling, gr<strong>and</strong>Parent);}private void Case1(Node curNode, Node parent,Node sibling, Node gr<strong>and</strong>Parent) {if (siblingToRight) {parent.<strong>Red</strong> = !parent.<strong>Red</strong>;sibling.<strong>Red</strong> = !sibling.<strong>Red</strong>;if (gr<strong>and</strong>Parent != null) {if (parentToRight) {76 JOURNAL OF OBJECT TECHNOLOGY VOL. 4, NO. 2
LeftRotate(ref parent);gr<strong>and</strong>Parent.Right = parent;} else if (!parentToRight) {LeftRotate(ref parent);gr<strong>and</strong>Parent.Left = parent;}} else {LeftRotate(ref parent);root = parent;}gr<strong>and</strong>Parent = sibling;parent = parent.Left;parentToRight = false;} else if (!siblingToRight) {parent.<strong>Red</strong> = !parent.<strong>Red</strong>;sibling.<strong>Red</strong> = !sibling.<strong>Red</strong>;if (gr<strong>and</strong>Parent != null) {if (parentToRight) {RightRotate(ref parent);gr<strong>and</strong>Parent.Right = parent;} else if (!parentToRight) {RightRotate(ref parent);gr<strong>and</strong>Parent.Left = parent;}} else {RightRotate(ref parent);root = parent;}gr<strong>and</strong>Parent = sibling;parent = parent.Right;parentToRight = true;}if (parent.Right == curNode) {sibling = parent.Left;siblingToRight = false;} else if (parent.Left == curNode) {sibling = parent.Right;siblingToRight = true;}Node siblingLeftChild = null;Node siblingRightChild = null;if (sibling != null && sibling.Left != null) {siblingLeftChild = sibling.Left;}if (sibling != null && sibling.Right != null) {siblingRightChild = sibling.Right;}bool sibling<strong>Red</strong> = (sibling != null && sibling.<strong>Red</strong>);bool siblingLeft<strong>Red</strong> = (siblingLeftChild != null &&siblingLeftChild.<strong>Red</strong>);bool siblingRight<strong>Red</strong> = (siblingRightChild != null &&siblingRightChild.<strong>Red</strong>);if (parent.<strong>Red</strong> && !sibling<strong>Red</strong> && !siblingLeft<strong>Red</strong> &&!siblingRight<strong>Red</strong>) {Case2B(curNode, parent, sibling, gr<strong>and</strong>Parent);} else if (siblingToRight && !sibling<strong>Red</strong> && siblingLeft<strong>Red</strong>&& !siblingRight<strong>Red</strong>) {Case3(curNode, parent, sibling, gr<strong>and</strong>Parent);VOL. 4, NO. 2 JOURNAL OF OBJECT TECHNOLOGY 77