public static void TestTreeWithInorderAndPostorder()
        {
            int[] postorder = new int[] { 5, 7, 8, 6, 2, 4, 3, 1 };
            int[] inorder   = new int[] { 5, 2, 7, 6, 8, 1, 3, 4 };
            TreeWithInorderAndPostorder tree = new TreeWithInorderAndPostorder();

            TreeHelperGeneral.PrintATree(tree.GetTree(inorder, 0, inorder.Length - 1, postorder, 0, postorder.Length - 1));
        }
Beispiel #2
0
        public static void TestTreeWithInorderAndPreorder()
        {
            int[] preorder = new int[] { 1, 2, 4, 5, 6, 7, 3, 8 };
            int[] inorder  = new int[] { 4, 2, 6, 5, 7, 1, 3, 8 };
            TreeWithInorderAndPreorder tree = new TreeWithInorderAndPreorder();

            TreeHelperGeneral.PrintATree(tree.GetTree(inorder, 0, inorder.Length - 1, preorder, 0, preorder.Length - 1));
        }
Beispiel #3
0
        public static void TestMirrorATree()
        {
            BinaryTree <int> bt1 = new BinaryTree <int>(new int[] { 8, 4, 12, 1, 5, 9, 13, 0, 2 }, true);

            Console.WriteLine("Initial tree:");
            TreeHelperGeneral.PrintATree(bt1.Head);
            Console.WriteLine("Tree after mirroring");
            TreeHelperGeneral.PrintATree(GetNodeAfterMirroring(bt1.Head));
        }
Beispiel #4
0
        public static void TestMakeBSTFromPreOrder()
        {
            // Test case 1: complete bst
            MakeBSTFromPreOrder createBST = new MakeBSTFromPreOrder();

            int[] arr = new int[] { 4, 2, 1, 3, 6, 5, 7 };
            Console.WriteLine("The preorder travesal is given by:");
            ArrayHelper.PrintArray(arr);
            if (createBST.CanBSTBeCreated(arr))
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", true);
                Console.WriteLine("the tree looks like as shown below");
                TreeHelperGeneral.PrintATree(createBST.GetBSTFromPreOrder(arr));
            }
            else
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", false);
            }

            // Test case 2: skewed bst
            createBST = new MakeBSTFromPreOrder();
            arr       = new int[] { 7, 6, 5, 4, 3, 2, 1, 0 };
            Console.WriteLine("The preorder travesal is given by:");
            ArrayHelper.PrintArray(arr);
            if (createBST.CanBSTBeCreated(arr))
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", true);
                Console.WriteLine("the tree looks like as shown below");
                TreeHelperGeneral.PrintATree(createBST.GetBSTFromPreOrder(arr));
            }
            else
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", false);
            }

            // Test case 4: not bst
            createBST = new MakeBSTFromPreOrder();
            arr       = new int[] { 7, 5, 9, 2, 1, 4, 6 };
            Console.WriteLine("The preorder travesal is given by:");
            ArrayHelper.PrintArray(arr);
            if (createBST.CanBSTBeCreated(arr))
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", true);
                Console.WriteLine("the tree looks like as shown below");
                TreeHelperGeneral.PrintATree(createBST.GetBSTFromPreOrder(arr));
            }
            else
            {
                Console.WriteLine("The bst can be created for the preorder traversal: {0}", false);
            }
        }
Beispiel #5
0
        public static void TestCreateTreeFromExpression()
        {
            Console.WriteLine("Test create tree from well formed expression");
            TreeNode <char> treeHead = CreateTreeFromExpression("a<b<e<>>c<>d<f<>>>".ToCharArray());

            TreeHelperGeneral.PrintATree(treeHead);
            Console.WriteLine("Test create tree from non - well formed expression");
            try
            {
                CreateTreeFromExpression("a<b<e<>>c<<>>d<f<>>>".ToCharArray());
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }