コード例 #1
0
        private Node <T, U> FindSuccessor(Node <T, U> currentNode)
        {
            Node <T, U> succ = null;

            //if curr has rightChild(right subtree), the succ is min of right sub tree
            if (currentNode.HasRightChild())
            {
                succ = MinOfRightSubTree(currentNode);
            }

            //if curr doesn't have right sub tree
            else
            {
                //if has parent and is left Child, the succ is its parent.
                if (currentNode.IsLeftChild())
                {
                    succ = currentNode.Parent;
                }
                //if curr is right child. the succ is the succ of its parent excluding itself
                else
                {
                    currentNode.Parent.RightChild = null;
                    succ = FindSuccessor(currentNode.Parent);
                    currentNode.Parent.RightChild = currentNode;
                }
            }

            return(succ);
        }
コード例 #2
0
 private void _Remove(Node <T, U> currentNode)
 {
     //is leaf
     if (currentNode.IsLeaf())
     {
         //is left child
         if (currentNode.IsLeftChild())
         {
             currentNode.Parent.LeftChild = null;
         }
         //is right child
         else
         {
             currentNode.Parent.RightChild = null;
         }
     }
     //is not leaf
     else
     {
         //is left child
         if (currentNode.IsLeftChild())
         {
             //has only left child
             if (currentNode.HasLeftChild() && !currentNode.HasRightChild())
             {
                 currentNode.Parent.LeftChild = currentNode.LeftChild;
                 currentNode.LeftChild.Parent = currentNode.Parent;
             }
             //has only right child
             if (!currentNode.HasLeftChild() && currentNode.HasRightChild())
             {
                 currentNode.Parent.LeftChild  = currentNode.RightChild;
                 currentNode.RightChild.Parent = currentNode.Parent;
             }
             //has both children
             else
             {
             }
         }
         //is right child
         else
         {
         }
     }
 }