static void Main(string[] args)
        {
            BinarySearchTree tree = new BinarySearchTree();


            tree.Add(34, tree.Root);
            tree.Add(3, tree.Root);
            tree.Add(32, tree.Root);
            tree.Add(14, tree.Root);
            tree.Add(27, tree.Root);
            tree.Add(67, tree.Root);
            tree.Add(234, tree.Root);

            Console.WriteLine("Binary Search Tree Add: 34, 3, 67");
            Console.WriteLine();
            Console.WriteLine("Root.Value:");
            Console.WriteLine(tree.Root.Value);
            Console.WriteLine("RightChild.Value");
            Console.WriteLine(tree.Root.RightChild.Value);
            Console.WriteLine("LeftChild.Value:");
            Console.WriteLine(tree.Root.LeftChild.Value);


            Console.WriteLine("Contins: 3");
            bool truth = tree.Contains(3, tree.Root);

            Console.WriteLine(truth);

            Console.WriteLine("Binary Tree: PreOrder");
            BinaryTree <int> bT    = new BinaryTree <int>();
            Node <int>       node1 = new Node <int>()
            {
                Value = 1
            };
            Node <int> node2 = new Node <int>()
            {
                Value = 2
            };
            Node <int> node3 = new Node <int>()
            {
                Value = 3
            };
            Node <int> node4 = new Node <int>()
            {
                Value = 4
            };
            Node <int> node5 = new Node <int>()
            {
                Value = 5
            };
            Node <int> node6 = new Node <int>()
            {
                Value = 6
            };
            Node <int> node7 = new Node <int>()
            {
                Value = 7
            };

            bT.Root                       = node1;
            bT.Root.LeftChild             = node2;
            bT.Root.RightChild            = node3;
            bT.Root.LeftChild.LeftChild   = node4;
            bT.Root.LeftChild.RightChild  = node5;
            bT.Root.RightChild.LeftChild  = node6;
            bT.Root.RightChild.RightChild = node7;

            List <int> pre     = new List <int>();
            List <int> inorder = new List <int>();
            List <int> post    = new List <int>();

            bT.PreOrder(bT.Root, pre);
            bT.InOrder(bT.Root, inorder);
            bT.PostOrder(bT.Root, post);


            pre.ForEach(i => Console.Write("{0} ", i));
            Console.WriteLine();
            Console.WriteLine("Binary Tree: InOrder");
            inorder.ForEach(i => Console.Write("{0} ", i));
            Console.WriteLine();
            Console.WriteLine("Binary Tree: PostOrder");
            post.ForEach(i => Console.Write("{0} ", i));
        }
Example #2
0
        static void Main(string[] args)
        {
            int[] arr = { 30, 12, 40, 25, 50, 64, 77, 83, 100, 90, 110, 120, 140, 150, 130, 160, 171, 180, 200, 190 };
            var   bs  = new BinarySearch();

            bs.Search(-5, arr);
            bs.Search(205, arr);
            bs.Search(13, arr);
            bs.Search(181, arr);
            bs.Search(83, arr);
            bs.Search(200, arr);
            Console.WriteLine("-------------------------------------");


            var tree =
                new Tree <int>(7,
                               new Tree <int>(19,
                                              new Tree <int>(1),
                                              new Tree <int>(12),
                                              new Tree <int>(31)),
                               new Tree <int>(21),
                               new Tree <int>(14,
                                              new Tree <int>(23),
                                              new Tree <int>(6))
                               );

            tree.TraverseDFS();
            Console.WriteLine("-------------------------------------");


            var bst = new BinarySearchTree <string>();

            bst.Insert("5");
            bst.Insert("2");
            bst.Insert("3");
            bst.Insert("7");
            bst.Insert("1");
            bst.Insert("6");
            bst.Insert("4");
            bst.Insert("9");
            bst.Insert("8");
            bst.Insert("10");
            bst.Insert("22");
            bst.PrintTreeDFS();
            Console.WriteLine("5 exists = {0}", bst.Contains("5"));
            bst.Remove("5");
            Console.WriteLine("5 exists = {0}", bst.Contains("5"));
            bst.PrintTreeDFS();
            Console.WriteLine("-------------------------------------");


            string path = "D:\\Install";

            DirectoryTraverserDFSRecursion.TraverseDir(new DirectoryInfo(path));
            Console.WriteLine("-------------------------------------");

            DirectoryTraverserDFS.TraverseDir(path);
            Console.WriteLine("-------------------------------------");

            DirectoryTraverserBFS.TraverseDir(path);
            Console.WriteLine("-------------------------------------");


            for (int v = 0; v < GraphTraverseRecursion.graph.Size; v++)
            {
                if (!GraphTraverseRecursion.visited[v])
                {
                    GraphTraverseRecursion.TraverseDFS(v);
                    Console.WriteLine();
                }
            }
            Console.WriteLine("-------------------------------------");


            var comparer = new Point3DEqualityComparer();
            var dict     = new Dictionary <Point3D, int>(comparer);

            dict[new Point3D(4, 2, 5)]  = 5;
            dict[new Point3D(1, 2, 3)]  = 11;
            dict[new Point3D(3, 1, -1)] = 3;
            dict[new Point3D(1, 2, 3)]  = 100;

            foreach (var entry in dict)
            {
                Console.WriteLine("{0} --> {1}", entry.Key, entry.Value);
            }

            Console.ReadLine();
        }
Example #3
0
        public void Run()
        {
            BinaryTree <int> btree = new BinaryTree <int>();

            btree.Root       = new BinaryTreeNode <int>(1);
            btree.Root.Left  = new BinaryTreeNode <int>(2);
            btree.Root.Right = new BinaryTreeNode <int>(3);

            btree.Root.Left.Left = new BinaryTreeNode <int>(4);

            btree.Root.Right.Right = new BinaryTreeNode <int>(5);

            btree.Root.Left.Left.Right = new BinaryTreeNode <int>(6);

            btree.Root.Right.Right.Right = new BinaryTreeNode <int>(7);

            btree.Root.Right.Right.Right.Right = new BinaryTreeNode <int>(8);


            BinaryTree <int> btreeOther = new BinaryTree <int>();

            btreeOther.Root       = new BinaryTreeNode <int>(1);
            btreeOther.Root.Left  = new BinaryTreeNode <int>(2);
            btreeOther.Root.Right = new BinaryTreeNode <int>(3);

            btreeOther.Root.Left.Left   = new BinaryTreeNode <int>(4);
            btreeOther.Root.Right.Right = new BinaryTreeNode <int>(5);

            btreeOther.Root.Left.Left.Right = new BinaryTreeNode <int>(6);

            btreeOther.Root.Right.Right.Right = new BinaryTreeNode <int>(7);

            btreeOther.Root.Right.Right.Right.Right = new BinaryTreeNode <int>(8);

            btree.PreorderTraversalRecursive();
            btree.PreOrderTraversalIterative();
            btree.PreOrderTraversalIterativeCompilerMimic();

            btree.PostorderTraversalRecursive();
            btree.PostOrderTraversalIterative();
            btree.PostOrderTraversalIterativeCompilerMimic();

            btree.InorderTraversalRecursive();
            btree.InorderTraversalIterative();
            btree.InOrderTraversalIterativeCompilerMimic();

            btree.PrintLevelOrderTranversal();
            Console.WriteLine("Size of tree:" + btree.GetSizeOfTree());

            Console.WriteLine("Are identical: " + btree.AreIdentical(btreeOther));

            Console.WriteLine("Max depth of tree: " + btree.MaxDepth());

            Console.WriteLine("Tree contains 5: " + btree.Contains(5));
            Console.WriteLine("Tree contains 10: " + btree.Contains(10));

            //btree.MirrorTree();
            //btree.PrintLevelOrderTranversal();

            btree.PrintRootToLeafPaths();

            Console.WriteLine("LCA for binary tree: " + btree.FindLowestCommonAncestor(7, 6).Data);
            Console.WriteLine("Largest BST subtree size: " + btree.LargestBstSubtree());

            Console.WriteLine("----------------BST----------------");

            BinarySearchTree <int> binarySearchTree = new BinarySearchTree <int>();

            binarySearchTree.Insert(50);
            binarySearchTree.Insert(30);
            binarySearchTree.Insert(20);
            binarySearchTree.Insert(40);
            binarySearchTree.Insert(70);
            binarySearchTree.Insert(60);
            binarySearchTree.Insert(80);

            int temp = binarySearchTree.Root.Left.Data;

            binarySearchTree.Root.Left.Data  = binarySearchTree.Root.Right.Data;
            binarySearchTree.Root.Right.Data = temp;

            binarySearchTree.InorderTraversalRecursive();
            binarySearchTree.CorrectBst();
            binarySearchTree.InorderTraversalRecursive();


            binarySearchTree.InorderTraversalRecursive();
            Console.WriteLine("BST contain 60 : " + binarySearchTree.Contains(60));
            Console.WriteLine("BST contain 66 : " + binarySearchTree.Contains(66));
            Console.WriteLine("MinValue in BST: " + binarySearchTree.MinValue());

            Console.WriteLine("Is BST: " + binarySearchTree.IsBst());
            Console.WriteLine("InOrderSuccessor of root(50): " + binarySearchTree.InOrderSuccessor(binarySearchTree.Root).Data);
            Console.WriteLine("5th node: " + binarySearchTree.KthSmallestNode(5).Data);
            Console.WriteLine("Largest BST subtree size: " + binarySearchTree.LargestBstSubtree());
            Console.WriteLine("Ceil for 50:" + binarySearchTree.Ceil(50));
            Console.WriteLine("CeilIterative for 51:" + binarySearchTree.CeilIterative(51));
            Console.WriteLine("FloorIterative for 49:" + binarySearchTree.FloorIterative(49));

            SinglyLinkedList <int> sortedLinkedList = new SinglyLinkedList <int>();

            sortedLinkedList.PushToHead(70);
            sortedLinkedList.PushToHead(60);
            sortedLinkedList.PushToHead(50);
            sortedLinkedList.PushToHead(40);
            sortedLinkedList.PushToHead(30);
            sortedLinkedList.PushToHead(20);
            sortedLinkedList.PushToHead(10);
            BinarySearchTree <int> bstFromSll = BinarySearchTree <int> .GetBstFromSortedList(sortedLinkedList);

            bstFromSll.InorderTraversalRecursive();

            BinarySearchTree <int> .PrintCommonNodes(binarySearchTree, bstFromSll);

            binarySearchTree.AddGreaterNodeSum();
            binarySearchTree.InorderTraversalRecursive();


            int[] sortedArray = new int[7] {
                1, 2, 3, 4, 5, 6, 7
            };
            BinarySearchTree <int> bstFromArray = BinarySearchTree <int> .GetBstFromSortedArray(sortedArray);

            bstFromArray.InorderTraversalRecursive();

            binarySearchTree.GetInorderTraversal();

            BinarySearchTree <int> mergeBst = BinarySearchTree <int> .GetMergeTree(bstFromSll, bstFromArray);

            mergeBst.InorderTraversalRecursive();

            int[] preOrderOfSomeTreeWithOnlyONeChildAtEachNode = new int[] { 20, 10, 11, 13, 12 };
            Console.WriteLine("preOrderOfSomeTreeWithOnlyONeChildAtEachNode:" +
                              BinarySearchTree <int> .IsPreOrderWithOnlyOneChildSatisfyBstProperty(preOrderOfSomeTreeWithOnlyONeChildAtEachNode));

            int[] preOrderOfTree = new int[] { 8, 10, 12, 15, 16, 20, 25 };
            BinarySearchTree <int> bstFromPreOrder = BinarySearchTree <int> .ConstructBstFromPreOrder(preOrderOfTree);

            bstFromPreOrder.InorderTraversalRecursive();

            //bstFromPreOrder.BstToDll();
            //dll.Print();

            BinarySearchTree <int> .FindPairWithSumAs(bstFromPreOrder.Root, 33);

            bstFromPreOrder.RemoveOutsideRangeNodes(10, 26);
            bstFromPreOrder.InorderTraversalRecursive();

            AugmentedBst <int> augmentedBst = new AugmentedBst <int>();

            augmentedBst.Insert(50);
            augmentedBst.Insert(30);
            augmentedBst.Insert(20);
            augmentedBst.Insert(40);
            augmentedBst.Insert(70);
            augmentedBst.Insert(60);
            augmentedBst.Insert(80);

            augmentedBst.InorderTraversalRecursive();
            Console.WriteLine("5th element: " + augmentedBst.KthSmallestNodeInOrderOfHeightTime(5).Data);


            Console.ReadLine();
        }