示例#1
0
 private BST FindMinValue()
 {
     if (left == null)
     {
         return(this);
     }
     else
     {
         return(left.FindMinValue());
     }
 }
示例#2
0
        private BST Remove(int value, BST parent)
        {
            if (this.value > value)
            {
                left.Remove(value, this);
            }
            else if (this.value < value)
            {
                right.Remove(value, this);
            }
            else
            {
                if (right == null && left == null)
                {
                    if (parent == null)
                    {
                        return(null);
                    }

                    if (parent.left != null && parent.left.value == value)
                    {
                        parent.left = null;
                    }
                    else if (parent.right != null && parent.right.value == value)
                    {
                        parent.right = null;
                    }
                }
                else if (right != null && left != null)
                {
                    var min = right.FindMinValue();
                    this.value = min.value;
                    right.Remove(min.value, this);
                }
                else if (right != null || left != null)
                {
                    if (right != null)
                    {
                        this.value = right.value;
                        this.right = right.right;
                    }
                    else if (left != null)
                    {
                        this.value = left.value;
                        this.left  = left.left;
                    }
                }
            }

            return(this);
        }