private static void PrintTree(TreeNode root)
        {
            BTreePrinter.Print(root);
            var isBalanced1 = IsBalanced(root);

            if (isBalanced1)
            {
                Console.WriteLine("Tree is balanced");
            }
            else
            {
                Console.WriteLine("Tree is not balalnced");
            }

            var isBalanced2 = IsBalancedBook(root);

            if (IsBalancedBook(root))
            {
                Console.WriteLine("Tree is balanced");
            }
            else
            {
                Console.WriteLine("Tree is not balalnced");
            }
        }
예제 #2
0
        private static void PrintTree(TreeNode root)
        {
            BTreePrinter.PrintNode(root);
            var watch       = System.Diagnostics.Stopwatch.StartNew();
            var isBalanced1 = IsBalanced(root);

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            if (isBalanced1)
            {
                Console.WriteLine("Tree is balanced");
            }
            else
            {
                Console.WriteLine("Tree is not balalnced");
            }
            Console.WriteLine($"Elapsed milliconds {elapsedMs}");

            watch = System.Diagnostics.Stopwatch.StartNew();
            var isBalanced2 = IsBalancedBook(root);

            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;

            if (IsBalancedBook(root))
            {
                Console.WriteLine("Tree is balanced");
            }
            else
            {
                Console.WriteLine("Tree is not balalnced");
            }
            Console.WriteLine($"Elapsed milliconds {elapsedMs}");
        }
        public override void Run()
        {
            var root = Create(0, 1, 2, 3, 4, 5, 6);

            BTreePrinter.Print(root);
            Console.WriteLine("Solution 1");
            var sol1   = new Solution1();
            var result = sol1.Convert(root);

            Display(result.Head);

            Console.WriteLine("Solution 2");
            root = Create(0, 1, 2, 3, 4, 5, 6);
            var sol2    = new Solution2();
            var result2 = sol2.Convert(root);

            Display(result2);

            Console.WriteLine("Solution 3");
            root = Create(0, 1, 2, 3, 4, 5, 6);
            var sol3    = new Solution3();
            var result3 = sol3.Convert(root);

            Display(result3);
        }
        static void Main(string[] args)
        {
            BTree bTree = new BTree();

            bTree.AddItem(10);

            bTree.AddItem(5);
            bTree.AddItem(15);

            bTree.AddItem(2);
            bTree.AddItem(7);
            bTree.AddItem(12);
            bTree.AddItem(17);

            bTree.AddItem(1);
            bTree.AddItem(3);
            bTree.AddItem(6);
            bTree.AddItem(8);
            bTree.AddItem(11);
            bTree.AddItem(13);
            bTree.AddItem(16);
            bTree.AddItem(18);

            BTreePrinter.Print(bTree.GetRoot(), 0, 32);

            var b = BTreeSearch.BFS(bTree.GetRoot(), 18);
            var d = BTreeSearch.DFS(bTree.GetRoot(), 18);
        }
예제 #5
0
        public static void PrintToConsole(BTree <int> tree)
        {
            Console.WriteLine("\n\nPrinting to Console.");
            PrintParentIndetifier();
            ConsolePrinter     printer     = new ConsolePrinter();
            BTreePrinter <int> treePrinter = new BTreePrinter <int>(printer);

            treePrinter.PrintBTree(tree);
        }
예제 #6
0
        public override void Run()
        {
            var      tree          = Q4_02_CreateMinimalBSTfromSortedUniqueArray.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            var      nodeToReplace = tree.Find(6);
            TreeNode newTree       = nodeToReplace.Replace(11);

            BTreePrinter.Print(tree);
            BTreePrinter.Print(newTree);
        }
예제 #7
0
        public override void Run()
        {
            var root = Q4_02_CreateMinimalBSTfromSortedUniqueArray.Create(1, 2, 3);

            BTreePrinter.Print(root);
            var results = AllSequences(root);

            foreach (var list in results)
            {
                AssortedMethods.PrintList(list);
            }
        }
예제 #8
0
 private static void Print(TreeNode tree, TreeNode subTree)
 {
     BTreePrinter.Print(tree);
     BTreePrinter.Print(subTree);
     if (ContainsTree(tree, subTree))
     {
         Console.WriteLine("It is a sub tree");
     }
     else
     {
         Console.WriteLine("It is not a sub tree");
     }
 }
        private static void PrintTree(TreeNode root)
        {
            BTreePrinter.Print(root);
            var isValidBST = IsValidBST(root);

            if (isValidBST)
            {
                Console.WriteLine("Tree is a valid BST");
            }
            else
            {
                Console.WriteLine("Tree is not a valid BST");
            }
        }
예제 #10
0
        public void Run()
        {
            TreeNode root = new TreeNode(1);

            root.Left        = new TreeNode(2);
            root.Right       = new TreeNode(3);
            root.Left.Left   = new TreeNode(4);
            root.Left.Right  = new TreeNode(5);
            root.Right.Right = new TreeNode(6);
            BTreePrinter.Inorder(root);
            var result = GenerateMirrorTree(root);

            BTreePrinter.Inorder(result);
        }
        public void Run()
        {
            var tree         = Q4_02_CreateMinimalBSTfromSortedUniqueArray.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            var listOfDepths = ListOfDepths(tree);

            BTreePrinter.PrintNode(tree);
            foreach (var list in listOfDepths)
            {
                foreach (var sbList in list)
                {
                    Console.Write($"{sbList.Data},");
                }
                Console.WriteLine();
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            var tree = TreeNode.Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

            var listofDepths = ListOfDepths(tree);

            BTreePrinter.Print(tree);

            //foreach (var item in listofDepths)
            //{
            //    foreach (var sblist in item)
            //    {
            //        Console.WriteLine($"{sblist.Data}");
            //    }
            //    Console.WriteLine();
            //}
        }
예제 #13
0
        public static void PrintToFile(BTree <int> tree)
        {
            Console.WriteLine("\n\nPrinting to file.");
            PrintParentIndetifier();
            string outPath = GetWorkingDirPath("output.txt");

            try
            {
                using (FilePrinter printer = new FilePrinter(outPath))
                {
                    BTreePrinter <int> treePrinter = new BTreePrinter <int>(printer);
                    treePrinter.PrintBTree(tree);
                    Console.WriteLine("Wrote to file at {0}", outPath);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Error writing to file, make sure the file {0} is available for writing.", outPath);
            }
        }
예제 #14
0
        public void Run()
        {
            /* Let us construct below tree
             *  1
             * / \
             * 2   3
             * / \   \
             * 4   5   6    */
            var root = new TreeNode(1);

            root.Left        = new TreeNode(2);
            root.Right       = new TreeNode(3);
            root.Left.Left   = new TreeNode(4);
            root.Left.Right  = new TreeNode(5);
            root.Right.Right = new TreeNode(6);

            UpdateTree(root);

            Console.WriteLine("Inorder traversal of the modified tree is \n");
            BTreePrinter.Inorder(root);
        }
예제 #15
0
        public void Run()
        {
            /*
             *        50                            50
             *     /     \         delete(20)      /   \
             *    30      70       --------->    30     70
             *   /  \    /  \                     \    /  \
             * 20   40  60   80                   40  60   80
             */

            TreeNode root = new TreeNode(50);

            root.Left        = new TreeNode(30);
            root.Right       = new TreeNode(70);
            root.Left.Left   = new TreeNode(20);
            root.Left.Right  = new TreeNode(40);
            root.Right.Left  = new TreeNode(60);
            root.Right.Right = new TreeNode(80);
            DeleteNode(root, 20);
            BTreePrinter.Inorder(root);
        }
        public override void Run()
        {
            var root = Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

            BTreePrinter.Print(root);
        }
예제 #17
0
        public static void PrintToConsole(BTree<int> tree)
        {
            Console.WriteLine("\n\nPrinting to Console.");
            PrintParentIndetifier();
            ConsolePrinter printer = new ConsolePrinter();
            BTreePrinter<int> treePrinter = new BTreePrinter<int>(printer);

            treePrinter.PrintBTree(tree);
        }
예제 #18
0
        public static void PrintToFile(BTree<int> tree)
        {
            Console.WriteLine("\n\nPrinting to file.");
            PrintParentIndetifier();
            string outPath = GetWorkingDirPath("output.txt");

            try
            {
                using (FilePrinter printer = new FilePrinter(outPath))
                {
                    BTreePrinter<int> treePrinter = new BTreePrinter<int>(printer);
                    treePrinter.PrintBTree(tree);
                    Console.WriteLine("Wrote to file at {0}", outPath);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Error writing to file, make sure the file {0} is available for writing.", outPath);
            }
        }
예제 #19
0
 public void Print()
 {
     BTreePrinter.Print(root);
 }
 public void printTree()
 {
     BTreePrinter.print(head);
 }
        public void Run()
        {
            var root = Create(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

            BTreePrinter.PrintNode(root);
        }