/// <summary>
 /// Constructs the tree
 /// </summary>
 public BinarySearchTree()
 {
     this.root = null;
 }
        private void Remove(BinaryTreeNode <T> node)
        {
            // Case 3: If the node has two children.
            // Note that if we get here at the end
            // the node will be with at most one child
            if (node.LeftChild != null && node.RightChild != null)
            {
                BinaryTreeNode <T> replacement = node.RightChild;
                while (replacement.LeftChild != null)
                {
                    replacement = replacement.LeftChild;
                }
                node.Value = replacement.Value;
                node       = replacement;
            }
            // Case 1 and 2: If the node has at most one child
            BinaryTreeNode <T> theChild = node.LeftChild != null ?
                                          node.LeftChild : node.RightChild;

            // If the element to be deleted has one child
            if (theChild != null)
            {
                theChild.Parent = node.Parent;
                // Handle the case when the element is the root
                if (node.Parent == null)
                {
                    root = theChild;
                }
                else
                {
                    // Replace the element with its child subtree
                    if (node.Parent.LeftChild == node)
                    {
                        node.Parent.LeftChild = theChild;
                    }
                    else
                    {
                        node.Parent.RightChild = theChild;
                    }
                }
            }
            else
            {
                // Handle the case when the element is the root
                if (node.Parent == null)
                {
                    root = null;
                }
                else
                {
                    // Remove the element - it is a leaf
                    if (node.Parent.LeftChild == node)
                    {
                        node.Parent.LeftChild = null;
                    }
                    else
                    {
                        node.Parent.RightChild = null;
                    }
                }
            }
        }