Exemple #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();
        }
        static void Main(string[] args)
        {
            /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/
            ImplementQueueWithTwoStacks iqTS = new ImplementQueueWithTwoStacks();

            iqTS.Insert(10);
            iqTS.Insert(20);
            iqTS.Insert(30);
            iqTS.Insert(40);

            Console.WriteLine(iqTS.Delete()); // 10
            Console.WriteLine(iqTS.Delete()); // 20
            Console.WriteLine(iqTS.Delete()); // 30
            /********************************************************** IMPLEMENT QUEUE WITH TWO STACKS ***********************************************************************/

            // Console.WriteLine(new InfixToPostFixConversion(7).ConvertToPostfix("a+b*(c-d)"));
            Console.WriteLine(new InfixToPostFixConversion("a+b*(c^d-e)^(f+g*h)-i".Length).ConvertToPostfix("a+b*(c^d-e)^(f+g*h)-i")); // abcd^e-fgh*+^*+i-

            /**************************************************************** Trying to implement complete binary tree   *****************************************************************/
            TreePractice tr = new TreePractice();

            tr.AddNode_BinaryTree(10, tr.root);
            tr.AddNode_BinaryTree(20, tr.root);
            tr.AddNode_BinaryTree(30, tr.root);
            tr.AddNode_BinaryTree(40, tr.root);
            tr.AddNode_BinaryTree(50, tr.root);
            tr.AddNode_BinaryTree(60, tr.root);
            /**************************************************************** Trying to implement complete binary tree   *****************************************************************/

            TreeTraversals trTraversal = new TreeTraversals();

            trTraversal.Inorder(tr.root);

            Console.WriteLine(new TreePractice().CountNumberOfNodes(tr.root));

            Console.WriteLine(new TreePractice().CountNumberOfNodes_Meth2(tr.root));

            Console.WriteLine(new TreePractice().CountNumberOfLeaves(tr.root));

            Console.WriteLine("No of leaves:" + new TreePractice().CountNumberofLeaves_Meth2(tr.root));

            Console.WriteLine("No of non-leaves:" + new TreePractice().CountNumberofNonLeaves_Meth2(tr.root));

            /************************************************* Binary Search Tree ************************************/
            BinarySearchTree bTree = new BinarySearchTree();

            /* Add elements in Binary Search Tree */
            bTree.AddNode(10, bTree.root);
            bTree.AddNode(20, bTree.root);
            bTree.AddNode(30, bTree.root);
            bTree.AddNode(40, bTree.root);
            bTree.AddNode(25, bTree.root);
            bTree.AddNode(15, bTree.root);
            bTree.AddNode(5, bTree.root);
            bTree.AddNode(8, bTree.root);
            bTree.AddNode(9, bTree.root);

            Console.WriteLine("Maximum Element: " + bTree.FindMax(bTree.root));

            Console.WriteLine("Minimum Element: " + bTree.FindMin(bTree.root));

            bTree.DeleteNode(25, bTree.root);
            NodePractice afterDelete = bTree.DeleteNode(40, bTree.root);

            // bTree.DeleteNode(10, bTree.root);

            Console.WriteLine(afterDelete.data);

            trTraversal.Inorder(bTree.root); // should print all elements in a sorted order

            Console.ReadLine();
        }