Exemplo 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));
        }
Exemplo 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());
        }
Exemplo 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());
        }
Exemplo 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());
        }
Exemplo 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);
        }
Exemplo 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());
        }
Exemplo 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());
        }
Exemplo 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());
        }
Exemplo n.º 9
0
        public void TrueIfRootOnly()
        {
            var tree = new BinarySearchTree
                           {
                               Root = new TreeNode<int> {Value = 1}
                           };

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

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