Beispiel #1
0
 public RBValNode(T val, RBNode <T> leftNode = null, RBNode <T> rightNode = null, RBNode <T> parent = null)
 {
     base.val = val;
     left     = leftNode;
     if (leftNode == null)
     {
         left = new RBNullNode <T>();
     }
     right = rightNode;
     if (rightNode == null)
     {
         right = new RBNullNode <T>();
     }
     this.parent = parent;
 }
        public void Delete(RBNode <T> nodeToDelete)
        {
            //Do a BST delete. If Delete() is called on another node (in a special case) then the check is CANCELED on the original "deleted" node.
            if (nodeToDelete.left is RBNullNode <T> && nodeToDelete.right is RBNullNode <T> )
            {
                //no children
                RBNode <T> oldNode = nodeToDelete;
                RBNode <T> newNode = new RBNullNode <T>();



                DeleteCheck(oldNode, newNode);

                if (nodeToDelete == topNode) //roundabout way to say nodeToDelete = newNode. But that doesn't work because of refs
                {
                    topNode        = newNode;
                    topNode.parent = null;
                }
                else if (nodeToDelete.parent.left == nodeToDelete)
                {
                    nodeToDelete.parent.left = newNode;
                }
                else if (nodeToDelete.parent.right == nodeToDelete)
                {
                    nodeToDelete.parent.right = newNode;
                }
                else
                {
                    throw new Exception("Some kind of parenting issue");
                }
            }
            else if (nodeToDelete.right is RBNullNode <T> )
            {
                //the only child is left
                RBNode <T> oldNode = nodeToDelete;
                RBNode <T> newNode = nodeToDelete.left;


                DeleteCheck(oldNode, newNode);

                if (nodeToDelete == topNode)
                {                 //roundabout way to say nodeToDelete = newNode. But that doesn't work because of refs
                    topNode = newNode;
                }
                else if (nodeToDelete.parent.left == nodeToDelete)
                {
                    nodeToDelete.parent.left = newNode;
                }
                else if (nodeToDelete.parent.right == nodeToDelete)
                {
                    nodeToDelete.parent.right = newNode;
                }
                else
                {
                    throw new Exception("Some kind of parenting issue");
                }
            }
            else if (nodeToDelete.left is RBNullNode <T> )
            {
                //only child is right
                RBNode <T> oldNode = nodeToDelete;
                RBNode <T> newNode = nodeToDelete.right;

                DeleteCheck(oldNode, newNode);

                if (nodeToDelete == topNode)
                {                 //roundabout way to say nodeToDelete = newNode. But that doesn't work because of refs
                    topNode = newNode;
                }
                else if (nodeToDelete.parent.left == nodeToDelete)
                {
                    nodeToDelete.parent.left = newNode;
                }
                else if (nodeToDelete.parent.right == nodeToDelete)
                {
                    nodeToDelete.parent.right = newNode;
                }
                else
                {
                    throw new Exception("Some kind of parenting issue");
                }
            }
            else
            {
                RBNode <T> replacer = FindRightestChild(nodeToDelete.left);
                nodeToDelete.val = replacer.val;
                Delete(replacer);
            }
        }