Esempio n. 1
0
        public void GenUniqueBST()
        {
            TreeQ treeQ = new TreeQ();
            var   roots = treeQ.GenUniqueBST(3);

            Assert.That(roots.Count == 5);
        }
Esempio n. 2
0
        public void SizeCompletedTree()
        {
            Tree <int> tree = new Tree <int>
            {
                Root = new Tree <int> .Node(1)
                {
                    //Left = new Tree<int>.Node(2)
                    //{
                    //    Left = new Tree<int>.Node(3)
                    //    {
                    //        Left = new Tree<int>.Node(9),
                    //        Right = new Tree<int>.Node(10),
                    //    },
                    //    Right = new Tree<int>.Node(4)
                    //    {
                    //        Left = new Tree<int>.Node(11),
                    //        Right = new Tree<int>.Node(12)
                    //    }
                    //},
                    //Right = new Tree<int>.Node(5)
                    //{
                    //    Left = new Tree<int>.Node(6)
                    //    {
                    //        Left = new Tree<int>.Node(13),
                    //    },
                    //    Right = new Tree<int>.Node(7)
                    //}
                }
            };

            TreeQ treeQ = new TreeQ();

            Assert.IsTrue(treeQ.SizeCompleteBinaryTree(tree.Root) == 1);
        }
Esempio n. 3
0
        public void MaxDepthTest()
        {
            var      testClass = new TreeQ();
            TreeNode input1    = null;

            Assert.AreEqual(0, testClass.MaxDepth(input1), "Testcase 01 Failed");

            input1 = new TreeNode(0);
            Assert.AreEqual(1, testClass.MaxDepth(input1), "Testcase 02 Failed");

            input1 = new TreeNode(0)
            {
                left = new TreeNode(1), right = new TreeNode(1)
            };
            Assert.AreEqual(2, testClass.MaxDepth(input1), "Testcase 03 Failed");

            input1 = new TreeNode(0)
            {
                left = new TreeNode(1), right = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            Assert.AreEqual(3, testClass.MaxDepth(input1), "Testcase 04 Failed");

            input1 = new TreeNode(0)
            {
                right = new TreeNode(1), left = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            Assert.AreEqual(3, testClass.MaxDepth(input1), "Testcase 05 Failed");
        }
Esempio n. 4
0
        public static void InvertTree()
        {
            BST <int> bst = new BST <int>(int.MinValue, int.MaxValue)
            {
                Root = new Tree <int> .Node(10)
                {
                    Left = new Tree <int> .Node(5)
                    {
                        Left  = new Tree <int> .Node(2),
                        Right = new Tree <int> .Node(7)
                        {
                            Left = new Tree <int> .Node(6)
                        },
                    },
                    Right = new Tree <int> .Node(15)
                    {
                        Left = new Tree <int> .Node(12)
                        {
                            Right = new Tree <int> .Node(13)
                        },
                        Right = new Tree <int> .Node(17)
                    }
                }
            };

            TreeQ treeQ = new TreeQ();
            var   root  = treeQ.InvertTree(bst.Root);
        }
Esempio n. 5
0
        public void IsSymeticTreeTest()
        {
            var testClass = new TreeQ();
            var input1    = new TreeNode(0)
            {
                left = new TreeNode(1), right = new TreeNode(1)
            };

            Assert.AreEqual(true, testClass.IsSymmetric(input1), "Testcase 01 Failed");

            input1 = new TreeNode(0)
            {
                left = new TreeNode(1), right = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            Assert.AreEqual(false, testClass.IsSymmetric(input1), "Testcase 02 Failed");

            input1 = new TreeNode(0)
            {
                right = new TreeNode(1), left = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            Assert.AreEqual(false, testClass.IsSymmetric(input1), "Testcase 03 Failed");

            input1 = new TreeNode(0)
            {
                right = new TreeNode(1), left = new TreeNode(2)
            };
            Assert.AreEqual(false, testClass.IsSymmetric(input1), "Testcase 04 Failed");
        }
Esempio n. 6
0
        public void IsSameTreeTest()
        {
            var testClass = new TreeQ();
            var input1    = new TreeNode(1)
            {
                left = new TreeNode(2), right = new TreeNode(3)
            };
            var input2 = new TreeNode(1)
            {
                left = new TreeNode(2), right = new TreeNode(3)
            };

            Assert.AreEqual(true, testClass.IsSameTree(input1, input2), "testcase 01 failed");

            input1 = new TreeNode(1)
            {
                left = new TreeNode(2), right = new TreeNode(3)
            };
            input2 = new TreeNode(1)
            {
                left = new TreeNode(3), right = new TreeNode(2)
            };
            Assert.AreEqual(false, testClass.IsSameTree(input1, input2), "testcase 02 failed");

            input1 = new TreeNode(1)
            {
                right = new TreeNode(3)
            };
            input2 = new TreeNode(1)
            {
                left = new TreeNode(3)
            };
            Assert.AreEqual(false, testClass.IsSameTree(input1, input2), "testcase 03 failed");
        }
Esempio n. 7
0
 public void SortedArrayToBST()
 {
     List <int> list = new List <int>()
     {
         1, 2
     };
     var root = TreeQ.SortedArrayToBST(list);
 }
Esempio n. 8
0
        public void LevelOrderBottomTest()
        {
            var TestClass = new TreeQ();
            var input1    = new TreeNode(0)
            {
                left = new TreeNode(1), right = new TreeNode(1)
            };
            var output         = TestClass.LevelOrderBottom(input1);
            var oCol           = output.SelectMany(x => x).ToList();
            var expectedOutput = new List <int>()
            {
                1, 1, 0
            };

            CollectionAssert.AreEqual(expectedOutput, oCol, "Testcase 01 Failed");

            TestClass.treeValueCol = new Dictionary <int, IList <int> >();
            input1 = new TreeNode(0)
            {
                right = new TreeNode(1), left = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            output         = TestClass.LevelOrderBottom(input1);
            oCol           = oCol = output.SelectMany(x => x).ToList();
            expectedOutput = new List <int>()
            {
                3, 3, 1, 1, 0
            };
            CollectionAssert.AreEqual(expectedOutput, oCol, "Testcase 02 Failed");


            TestClass.treeValueCol = new Dictionary <int, IList <int> >();
            input1 = new TreeNode(0)
            {
                right = new TreeNode(1)
                {
                    left = new TreeNode(4), right = new TreeNode(5)
                }, left = new TreeNode(1)
                {
                    left = new TreeNode(3), right = new TreeNode(3)
                }
            };
            output         = TestClass.LevelOrderBottom(input1);
            oCol           = oCol = output.SelectMany(x => x).ToList();
            expectedOutput = new List <int>()
            {
                3, 3, 4, 5, 1, 1, 0
            };
            CollectionAssert.AreEqual(expectedOutput, oCol, "Testcase 03 Failed");
        }
Esempio n. 9
0
        public void ZigZag()
        {
            Tree <int> tree = new Tree <int>
            {
                Root = new Tree <int> .Node(1)
                {
                    Left = new Tree <int> .Node(2)
                    {
                        Left  = new Tree <int> .Node(3),
                        Right = new Tree <int> .Node(4),
                    },
                    Right = new Tree <int> .Node(5)
                    {
                        Right = new Tree <int> .Node(6)
                    }
                }
            };

            var treeQ = new TreeQ();
            var res   = treeQ.ZigzagLevelOrder(tree.Root);
        }
Esempio n. 10
0
        public static void BST()
        {
            //              10
            //      5               15
            //  2       7       12      17
            //      6               13

            BST <int> bst = new BST <int>(int.MinValue, int.MaxValue)
            {
                Root = new Tree <int> .Node(10)
                {
                    Left = new Tree <int> .Node(5)
                    {
                        Left  = new Tree <int> .Node(2),
                        Right = new Tree <int> .Node(7)
                        {
                            Left = new Tree <int> .Node(6)
                        },
                    },
                    Right = new Tree <int> .Node(15)
                    {
                        Left = new Tree <int> .Node(12)
                        {
                            Right = new Tree <int> .Node(13)
                        },
                        Right = new Tree <int> .Node(17)
                    }
                }
            };

            TreeQ treeQ = new TreeQ();

            Assert.That(treeQ.isValidBST(bst.Root) == true);
            Assert.That(bst.MaxHeight() == 4);

            Assert.That(bst.InOrder() == "2,5,6,7,10,12,13,15,17,");
            Assert.That(bst.InOrderWithoutRecursive() == "2,5,6,7,10,12,13,15,17,");
            Assert.That(bst.Search(13) != null);
            Assert.That(bst.Search(100) == null);
            Assert.That(bst.SearchRecurcive(13) != null);
            Assert.That(bst.SearchRecurcive(100) == null);

            var node  = bst.Insert(1);
            var node1 = bst.InsertRecurcive(3);

            bst.InsertRecurcive(20);
            bst.InsertRecurcive(25);
            Assert.That(bst.MaxHeight() == 5);

            //        4
            //      /   \ 
            //     2      5
            //  1    5
            BST <int> notBST = new BST <int>(int.MinValue, int.MaxValue)
            {
                Root = new Tree <int> .Node(4)
                {
                    Left = new Tree <int> .Node(2)
                    {
                        Left  = new Tree <int> .Node(1),
                        Right = new Tree <int> .Node(5),
                    },
                    Right = new Tree <int> .Node(5)
                }
            };

            Assert.That(treeQ.isValidBST(notBST.Root) == false);
        }
Esempio n. 11
0
 public void SortedArrayToBSTTest()
 {
     var testclass = new TreeQ();
     var input     = new int[] { -10, -3, 0, 5, 9 };
     var output    = testclass.SortedArrayToBST(input);
 }