Esempio n. 1
0
        public void NextSmallest()
        {
            /*
             *       5
             *    2
             *  1   3
             */
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int>
                {
                    Value = 5,
                    Left = new TreeNode<int>
                    {
                        Value = 2,
                        Left = new TreeNode<int> { Value = 1 },
                        Right = new TreeNode<int> { Value = 3 }
                    }
                }
            };

            Assert.AreEqual(3, tree.NextSmallest(2));
            Assert.AreEqual(2, tree.NextSmallest(1));
            Assert.AreEqual(5, tree.NextSmallest(3));
            Assert.AreEqual(-1, tree.NextSmallest(5));
        }
Esempio n. 2
0
        public void FalseSmallerRight()
        {
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5, Right = new TreeNode<int> { Value = 2 } }
            };

            Assert.IsFalse(tree.IsValidBST());
        }
Esempio n. 3
0
        public void FalseLargerLeft()
        {
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5, Left = new TreeNode<int> { Value = 7 } }
            };

            Assert.IsFalse(tree.IsValidBST());
        }
Esempio n. 4
0
        public void FalseMultiLevelAbsoluteLimit()
        {
            /*
             *       5
             *    2
             *  1   8
             */
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5,
                    Left = new TreeNode<int> { Value = 2,
                        Left = new TreeNode<int> { Value = 1 },
                        Right = new TreeNode<int> { Value = 8 } } }
            };

            Assert.IsFalse(tree.IsValidBST());
        }
Esempio n. 5
0
        public void SerializeDeserialize()
        {
            /*
             *       5
             *    2
             *  1   3
             */
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int>
                {
                    Value = 5,
                    Left = new TreeNode<int>
                    {
                        Value = 2,
                        Left = new TreeNode<int> { Value = 1 },
                        Right = new TreeNode<int> { Value = 3 }
                    }
                }
            };

            List<int> data = tree.SerializeTree();

            tree.Deserialize(data);

            Assert.IsNotNull(tree.Root);
            Assert.AreEqual(5, tree.Root.Value);
            Assert.AreEqual(2, tree.Root.Left.Value);
            Assert.AreEqual(1, tree.Root.Left.Left.Value);
            Assert.AreEqual(3, tree.Root.Left.Right.Value);
        }
Esempio n. 6
0
        public void TrueSmallerLeft()
        {
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5, Left = new TreeNode<int> { Value = 2 } }
            };

            Assert.IsTrue(tree.IsValidBST());
        }
Esempio n. 7
0
        public void TrueMultiLevel()
        {
            /*
             *       5
             *    2
             *  1   3
             */
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5,
                    Left = new TreeNode<int> { Value = 2,
                        Left = new TreeNode<int> { Value = 1 },
                        Right = new TreeNode<int> { Value = 3 }
                    }
                }
            };

            Assert.IsTrue(tree.IsValidBST());
        }
Esempio n. 8
0
        public void TrueLargerRight()
        {
            var tree = new BinarySearchTree
            {
                Root = new TreeNode<int> { Value = 5, Right = new TreeNode<int> { Value = 7 } }
            };

            Assert.IsTrue(tree.IsValidBST());
        }
Esempio n. 9
0
        public void TrueIfRootOnly()
        {
            var tree = new BinarySearchTree
                           {
                               Root = new TreeNode<int> {Value = 1}
                           };

            Assert.IsTrue(tree.IsValidBST());
        }
Esempio n. 10
0
        public void TrueIfNull()
        {
            var tree = new BinarySearchTree();

            Assert.IsTrue(tree.IsValidBST());
        }