Beispiel #1
0
        static void Main(string[] args)
        {
            //Stack
            // StackDS<int> mystack = new StackDS<int>();
            // mystack.push(5);
            // mystack.push(4);
            // mystack.push(3);
            // mystack.push(2);
            // mystack.push(1);
            //while(mystack.count > 0)
            // {
            // //    Console.WriteLine(mystack.pop() + " \n");

            // }

            //BinarySearchTree

            //BinaryTree<int> bt = new BinaryTree<int>();
            //bt.Root = new BinaryTreeNode<int>(13);
            //bt.Root.left = new BinaryTreeNode<int>(10, new BinaryTreeNode<int>(2), new BinaryTreeNode<int>(12));
            //bt.Root.right = new BinaryTreeNode<int>(25, new BinaryTreeNode<int>(20), new BinaryTreeNode<int>(31, new BinaryTreeNode<int>(29), null));
            //Console.WriteLine("Preorder Traverse\n");
            // preorder(bt.Root);
            //Console.WriteLine("inorder Traverse\n");
            // inorder(bt.Root);
            // Console.WriteLine("Postorder Traverse\n");
            // postorder(bt.Root);
            // Console.WriteLine("No Of Nodes" + cnt);
            // Console.ReadLine();


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

            tree.AddNode(6);
            tree.AddNode(4);
            tree.AddNode(2);
            tree.AddNode(1);
            tree.AddNode(3);
            tree.AddNode(5);
            tree.AddNode(9);
            tree.AddNode(10);
            tree.AddNode(8);
            tree.AddNode(7);

            tree.InOrderTraversal(tree.Root);
            Console.WriteLine("---------------------");
            tree.PreOrderTraversal(tree.Root);
            Console.WriteLine("---------------------");
            tree.PostOrderTraversal(tree.Root);
            Console.WriteLine("---------------------");
            BinaryNode <int> parent;
            BinaryNode <int> target = tree.FindWithParent(6, out parent);

            tree.RemoveNode(6);

            Console.WriteLine("---------------------");
            tree.PreOrderTraversal(tree.Root);
            Console.ReadLine();
        }
Beispiel #2
0
        public static void Driver()
        {
            BinarySearchTree binarySearchTree = new BinarySearchTree();

            binarySearchTree.InsertRange(new int[] { 16, 8, 24, 4, 12, 20, 28, 2, 6, 10, 14, 18, 22, 26, 30, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
            binarySearchTree.Insert(27);
            binarySearchTree.Insert(29);
            binarySearchTree.Insert(31);

            Console.WriteLine("In ordred traversing of Tree");
            binarySearchTree.InOrderTraversal(binarySearchTree.Root);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Reverse Order traversing of Tree");
            binarySearchTree.ReverseOrderTraversal(binarySearchTree.Root);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Pre Order traversing of Tree");
            binarySearchTree.PreOrderTraversal(binarySearchTree.Root);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Post Order traversing of Tree");
            binarySearchTree.PostOrderTraversal(binarySearchTree.Root);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("After deleting 4");
            binarySearchTree.Remove(binarySearchTree.Root, 4);
            binarySearchTree.InOrderTraversal(binarySearchTree.Root);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("After deleting 27");
            binarySearchTree.Remove(binarySearchTree.Root, 27);
            binarySearchTree.InOrderTraversal(binarySearchTree.Root);
            Console.WriteLine();


            Console.WriteLine();
            Console.WriteLine("Is is valid binary tree {0}", binarySearchTree.ValidBST());
        }