private BST RemoveBST(BST bst, int value)
            {
                if (bst == null || bst.value == value)
                {
                    return(bst);
                }

                if (value < bst.value)
                {
                    bst.left = RemoveBST(bst, value);
                }
                else if (value > bst.value)
                {
                    bst.right = RemoveBST(bst, value);
                }
                else
                {
                    if (bst.left == null)
                    {
                        bst       = bst.right;
                        bst.right = null;
                    }
                    else if (bst.right == null)
                    {
                        bst      = bst.left;
                        bst.left = null;
                    }
                    else
                    {
                        var minInorderSuccesor = FindMinInOrderSuccesor(bst.right, value);
                        bst.value = minInorderSuccesor;
                        RemoveBST(bst.right, bst.value);
                    }
                }
                return(bst);
            }
Example #2
0
        public void start()
        {
            BST bst = new BST();

            /* Let us create following BST
             * 50
             * /     \
             * 30      70
             * /  \    /  \
             * 20   40  60   80 */
            Node root = new Node(50);

            bst.Insert(30, root);
            bst.Insert(20, root);
            bst.Insert(40, root);
            bst.Insert(70, root);
            bst.Insert(60, root);
            bst.Insert(80, root);

            Node node = new Node(25);

            node.left  = new Node(18);
            node.right = new Node(50);

            node.left.left  = new Node(29);
            node.left.right = new Node(20);

            node.left.left.right = new Node(15);

            node.left.right.left  = new Node(18);
            node.left.right.right = new Node(28);

            node.left.right.left  = new Node(18);
            node.left.right.right = new Node(25);

            node.right.left  = new Node(35);
            node.right.right = new Node(65);

            node.right.left.left  = new Node(20);
            node.right.left.right = new Node(40);

            node.right.right.left  = new Node(55);
            node.right.right.right = new Node(70);

            node.right.left.left.right = new Node(25);

            //// print inorder traversal of the BST
            //bst.Inorder(root);

            //Console.WriteLine("Delete");

            //bst.Delete(30, root);

            //Console.WriteLine("Inorder");
            //bst.Inorder(root);


            //int[] pre = new int[] { 10, 5, 1, 7, 40, 50 };
            //int size = pre.Length;
            //Node root = bst.CreateBSTFromPreOrder(pre);


            //Node btree = new Node(10);
            //btree.left = new Node(30);
            //btree.right = new Node(15);
            //btree.left.left = new Node(20);
            //btree.right.right = new Node(5);

            //Node createdBST = bst.CreateBSTFromBTree(btree);
            //Console.WriteLine("Inorder traversal of the constructed tree is ");
            //bst.Inorder(createdBST);

            //Node res = bst.CreateBalancedBSTFromLinkedList(new LinkedList<int>(new List<int>() { 1, 2, 3, 4, 5, 6, 7 }));
            //bst.Inorder(res);

            //bst.Inorder(bst.CreateGreaterSumTreeFromBST(root));

            // Console.WriteLine(bst.CreatePossibleBSTFrom1ToN(5));
            // Console.WriteLine(bst.CreatePossibleBSTFrom1ToNUsingRecursion(5));

            // Console.WriteLine(bst.CheckIfBTreeIsBST(root, Int32.MinValue, Int32.MaxValue));

            //Console.WriteLine(bst.LargestBSTInBTree(node).size);

            // Console.WriteLine(bst.KthSmallestElementOrderN(root, 3).key);

            // Console.WriteLine(bst.KthSmallestElementOrderNWithArray(root, 3));

            /**
             *      Implement in order traversal without stack and recursion
             */
            // bst.InOrderWithoutStackAndRecursion(root);

            /**
             * Determine if the given two trees are identical or not
             */
            //Node root1 = new Node(50);
            //bst.Insert(30, root);
            //bst.Insert(25, root);
            //bst.Insert(40, root);
            //bst.Insert(70, root);
            //bst.Insert(60, root);
            //bst.Insert(80, root);
            //Console.WriteLine(bst.checkIfTwoBSTsAreidentical(root, root1));

            /**
             *  Print out all of its roof to leaf paths in a given binary search tree
             */
            // bst.allRootToLeafPathsInAGivenBST(root);

            /**
             *  Find a lowest common ancestor of a given two nodes in a a binary search tree
             */
            // Console.WriteLine(bst.lowestCommonAncestorOfABinaryTree(root, root.left.left, root.left.right).key);

            /**
             * Level order traversal in spiral form
             */
            // bst.levelOrderTraversalOfBSTInSpiralOrderNsqare(root);
            //bst.levelOrderTraversalOfBSTInSpiralOrderN(root);

            /**
             * Find the Diameter of a BST
             */
            // Console.WriteLine(bst.diameterOfBSTOrderNsquare(root));
            // Console.WriteLine(bst.diameterOfBSTOrderN(root, new Tree.BST.Index()));
            // Console.WriteLine(bst.diameterOfBSTOrderNOnlyHeightFuntion(root));

            /**
             * Convert a normal binary search tree to balanced BST.
             */
            Node unbalancedTree = new Node(45);

            bst.Insert(55, unbalancedTree);
            bst.Insert(65, unbalancedTree);
            bst.Insert(75, unbalancedTree);
            bst.Insert(85, unbalancedTree);
            bst.Insert(95, unbalancedTree);
            bst.Insert(105, unbalancedTree);
            //bst.Inorder(bst.ConvertBSTToBalancedTree(unbalancedTree));

            /**
             * Construct a BST from preorder
             */
            //				    75
            //			65	           85
            //		45		70      80	   105
            // pre order - 75, 65, 45, 70, 85, 80, 105
            // inorder - 45, 65, 70, 75, 80, 85, 105
            // post order - 45 70 65 80 105 85 75

            //bst.Inorder(bst.ConstructBSTFromPreorder(new int[] { 75, 65, 45, 70, 85, 80, 105 }, 0, 7));
            // bst.Inorder(bst.CreateBSTFromPreOrderOrderN(new int[] { 75, 65, 45, 70, 85, 80, 105 }, new Index(), Int32.MinValue, Int32.MaxValue));
            // bst.Inorder(bst.CreateBSTFromPreorderUsingStack(new int[] { 75, 65, 45, 70, 85, 80, 105 }));
            PrintPostOrderFromPre(new int[] { 75, 65, 45, 70, 85, 80, 105 }, new Index(), Int32.MinValue, Int32.MaxValue);


            //LNode btree = new LNode(10);
            //btree.left = new LNode(5);
            //btree.right = new LNode(15);
            //btree.left.left = new LNode(3);
            //btree.right.right = new LNode(20);

            // new BacktrackingStringPermutaion().createInputData();
            // new BacktrackingIsHack().setData();
            // new BacktrackingConfidential().RunConfidential();
            // new BacktrackingStairs().StaircaseWays();
            // new BacktrackingSubset().subsets(new List<int>() { 15, 20, 12, 19, 4});
            // new BacktrackingCombination().combine(4, 2);
            // new BacktrackingCommbinationSum().combinationSum(new List<int>() { 8, 10, 6, 11, 1, 16, 8 }, 28);
            // new BSTCount().findCount(new List<int>() { 4, 7, 7, 7, 8, 10, 10 }, 3);

            /**
             * BTree
             */

            // new BTreeDeletion().DeleteNode();

            //BTreeTraverseWithoutStack tree = new BTreeTraverseWithoutStack();
            //tree.root = new Node(25);
            //tree.root.left = new Node(15);
            //tree.root.right = new Node(50);

            //tree.root.left.left = new Node(10);
            //tree.root.left.right = new Node(22);

            //tree.root.left.left.left = new Node(4);
            //tree.root.left.left.right = new Node(12);

            //tree.root.left.right.left = new Node(18);
            //tree.root.left.right.right = new Node(24);

            //tree.root.right.left = new Node(35);
            //tree.root.right.right = new Node(70);


            //tree.root.right.right.left = new Node(66);
            //tree.root.right.right.right = new Node(90);

            //BTreeTraverseWithoutStack.TraverseWithoutStack(tree.root);

            /*
             * BFS Traversal
             */
            //Graph.Graph g = new Graph.Graph(4);

            //g.addEdge(0, 1);
            //g.addEdge(0, 2);
            //g.addEdge(1, 2);
            //g.addEdge(2, 0);
            //g.addEdge(2, 3);
            //g.addEdge(3, 3);

            //Console.WriteLine("Following is Depth First Traversal " + "(starting from vertex 2)");
            //g.DFSTraversal(2);

            //Console.WriteLine("Following is Breadth First Traversal " + "(starting from vertex 2)");
            //g.BFSTraversal(2);

            Console.ReadLine();
        }
 public static int FindClosestValueInBstFn(BST tree, int target)
 {
     // Write your code here.
     return(FindClosest(target, tree, tree.value));
 }