public bool IsBST(BTNode root)
 {
     return(IsBST(root, int.MinValue, int.MaxValue));
 }
 public bool InsertWithRecursion(BTNode node)
 {
     return(InsertWithRecursion(node, this.root));
 }
        /*
         * Delete a Node in Binary Search tree. There can be three cases
         * Case A: Node has no child, it is a leaf node. P.LChild = null OR P.Rchild = null
         * Case B: Node has exactly one child.
         *         If node to be deleted is left child node of it's parent node. Then, child node of deleted node will become
         *         the left child of the parent. Similar for right child as well. If deleted node is root node then child node will become root node.
         * Case C: Node has exactly two children.
         *         Find the inorder successor of the node to be deleted.
         *         Inorder successor of a node N : leftmost node in right subtree of N.
         *         After find the inorder successor copy the data of inorder successor to the node to be deleted.
         *         Then,delete the inorder successor from the tree.
         *
         *
         *
         *
         */

        public bool Remove(char x)
        {
            BTNode p = this.root;

            BTNode parent = null;

            while (p != null)
            {
                if (x < p.value)
                {
                    parent = p;
                    p      = p.lChild;
                }
                else if (x > p.value)
                {
                    parent = p;
                    p      = p.rChild;
                }
                else
                {
                    break;
                }
            }

            if (p == null)
            {
                Console.WriteLine("Node is not present in the tree.");
                return(false);
            }

            //Node to be deleted is root node
            if (parent == null)
            {
                if (p.lChild != null & p.rChild != null)
                {
                    BTNode p1       = p.rChild;
                    BTNode p1Parent = null;
                    while (p1.lChild != null)
                    {
                        p1Parent = p1;
                        p1       = p1.lChild;
                    }
                    p.value = p1.value;

                    p1Parent.lChild = p1.rChild;
                }
                else if (p.lChild != null && p.rChild == null)
                {
                    this.root = p.lChild;
                    p         = null;
                }
                else if (p.lChild == null && p.rChild != null)
                {
                    this.root = p.rChild;
                    p         = null;
                }
                else
                {
                    p = null;
                }
            }
            else
            {
                if (p.lChild != null & p.rChild != null)
                {
                    BTNode p1       = p.rChild;
                    BTNode p1Parent = null;
                    while (p1.lChild != null)
                    {
                        p1Parent = p1;
                        p1       = p1.lChild;
                    }
                    p.value = p1.value;

                    p1Parent.lChild = p1.rChild;
                }
                else if (p.lChild != null && p.rChild == null)
                {
                    if (parent.rChild == p)
                    {
                        parent.rChild = p.lChild;
                    }
                    else
                    {
                        parent.lChild = p.lChild;
                    }

                    p = null;
                }
                else if (p.lChild == null && p.rChild != null)
                {
                    if (parent.rChild == p)
                    {
                        parent.rChild = p.rChild;
                    }
                    else
                    {
                        parent.lChild = p.rChild;
                    }

                    p = null;
                }
                else
                {
                    p = null;
                }
            }

            return(false);
        }
 public BinarySearchTree()
 {
     this.root = null;
 }