public void ValidateBSTAlgo_WhenTreeIsBST_ReturnsTrue() { // Arrange var root = new BinaryTreeNode <int>(40); var rootLeft = new BinaryTreeNode <int>(25); var rootRight = new BinaryTreeNode <int>(40); root.Left = rootLeft; root.Right = rootRight; var rootLeftLeft = new BinaryTreeNode <int>(24); var rootLeftRight = new BinaryTreeNode <int>(39); rootLeft.Left = rootLeftLeft; rootLeft.Right = rootLeftRight; var rootRightRight = new BinaryTreeNode <int>(100); rootRight.Right = rootRightRight; var rootRightRightLeft = new BinaryTreeNode <int>(60); var rootRightRightRight = new BinaryTreeNode <int>(150); rootRightRight.Left = rootRightRightLeft; rootRightRight.Right = rootRightRightRight; // Act var actual = ValidateBST.ValidateBSTAlgo(root); // Assert actual.Should().BeTrue(); }
void InternalTest(int?[] tree, bool expected) { TreeNode root = TreeNode.Get(tree); bool actual = ValidateBST.IsValidBST(root); Assert.Equal <bool>(expected, actual); }
public void ValidateBSTAlgo_WhenTreeIsNotBST_ReturnsFalse() { // Arrange var root = new BinaryTreeNode <int>(40); var rootLeft = new BinaryTreeNode <int>(40); var rootRight = new BinaryTreeNode <int>(42); root.Left = rootLeft; root.Right = rootRight; // Act var actual = ValidateBST.ValidateBSTAlgo(root); // Assert actual.Should().BeFalse(); }
public static bool ValidateBst(ValidateBST tree, int minValue, int maxValue) { if (tree.value < minValue || tree.value >= maxValue) { return(false); } if (tree.left != null && !ValidateBst(tree.left, minValue, tree.value)) { return(false); } if (tree.right != null && !ValidateBst(tree.right, tree.value, maxValue)) { return(false); } return(true); }
public void Validating_Should_AddNewNodeToTree_When_GivenAnInteger() { //arrange var BST = new BST(10); var sut = new[] { 5, 15, 10 }; foreach (var number in sut) { BST.Insert(number); } //act bool results = ValidateBST.Validating(BST); //assert Assert.That(results, Is.False); }
public void Insert_Should_AddNewNodeToTree_When_GivenAnInteger() { //arrange var BST = new BST(10); var sut = new[] { 15, 22, 13, 14, 5, 5, 2, 1 }; foreach (var number in sut) { BST.Insert(number); } //act bool results = ValidateBST.ValidateInOrder(BST); //assert Assert.That(results, Is.True); }
public void IsValid() { BinarySearchTree <int> bst = new BinarySearchTree <int>(); bst.AddChild(5); bst.AddChild(3); bst.AddChild(2); bst.AddChild(6); bst.AddChild(1); bst.AddChild(4); Assert.AreEqual(true, ValidateBST.IsValid(bst)); var newNode = new BinaryTreeNode <int>(-2); newNode.Left = bst.root.Left; newNode.Right = bst.root.Right; bst.root = newNode; Assert.AreEqual(false, ValidateBST.IsValid(bst)); }
// O(n) time | O(d) space public static bool ValidateBst(ValidateBST tree) { return(ValidateBst(tree, Int32.MinValue, Int32.MaxValue)); }
public void ValidateBSTTest(TreeNode <int> root, bool expected) { bool result = ValidateBST.IsValidBST(root, int.MinValue, int.MaxValue); Assert.Equal(expected, result); }