private void ListOfDepthsTest() { TreeBinaryNode <int> tree = CreateBinarySearchTree <TreeBinaryNode <int> >(11); List <LinkedList <TreeBinaryNode <int> > > l = Q4_3ListOfDepths.getListOfDepths <int>(tree); List <LinkedList <TreeBinaryNode <int> > > l2 = Q4_3ListOfDepthsIterative.getListOfDepths <int>(tree); int[][] result = { new int[1] { 5 }, new int[2] { 2, 8 }, new int[4] { 0, 3, 6, 9 }, new int[4] { 1, 4, 7, 10 } }; checkValues(l, result); checkValues(l2, result); }
private void FCADDescendantOfItselfWithoutLinkParentTest() { TreeBinaryNode <int> firstNode = tree.right; TreeBinaryNode <int> secondtNode = tree.right; TreeBinaryNode <int> ancestor = commonAncestorWithoutLinkParent(tree, firstNode, secondtNode); Assert.Equal(tree.right, ancestor); }
public Tree() { NumericTree = CreateBinaryTree <TreeBinaryNode <int>, int>(new int[] { 7, 2, 3, 5, 9, 23 }); CharTree = CreateBinaryTree <TreeBinaryNode <char>, char>(new char[] { 'A', 'B', 'C', 'D' }); ParentTree = CreateBinaryTree <TreeBinaryParentNode <int>, int>(new int[] { 7, 2, 3, 5, 9, 23 }); SearchTree = CreateBinarySearchTree <TreeBinaryNode <int> >(5); SearchParentTree = CreateBinarySearchTree <TreeBinaryParentNode <int> >(5); }
private void FCADifferentLevelWithoutLinkParentTest() { TreeBinaryNode <int> firstNode = tree.right; TreeBinaryNode <int> secondNode = tree.right.right.right; TreeBinaryNode <int> ancestor = commonAncestorWithoutLinkParent(tree, firstNode, secondNode); Assert.Equal(tree.right, ancestor); }
private void validateBSTTest() { TreeBinaryNode <int> tree = CreateBinarySearchTree <TreeBinaryNode <int> >(13); TreeBinaryNode <int> tree2 = new TreeBinaryNode <int>(13, null, tree.right); Assert.True(bst.isBST(tree)); Assert.True(bst2.isBST(tree)); Assert.False(bst.isBST(tree2)); Assert.False(bst2.isBST(tree2)); }
private void minimalTreeCharTest() { char[] result = new char[5] { 'Z', 'C', 'A', 'B', 'E' }; char[] array = new char[5] { 'C', 'A', 'Z', 'B', 'E' }; TreeBinaryNode <char> tree = MinimalTree(array); checkValues(result, tree); }
private void FCANodeOfAnotherTreeWithoutLinkParentTest() { TreeBinaryNode <int> tree2 = CreateBinaryTree <TreeBinaryNode <int>, int>(new int[5] { 1, 1, 2, 3, 0 }); TreeBinaryNode <int> firstNode = tree.right; TreeBinaryNode <int> secondtNode = tree2.left; TreeBinaryNode <int> ancestor = commonAncestorWithoutLinkParent(tree, firstNode, secondtNode); Assert.Equal(null, ancestor); }
private static void TreeBinaryNodeDefaultTest() { TreeBinaryNode <int> tbn = new TreeBinaryNode <int>(6); TreeBinaryNode <int> left = new TreeBinaryNode <int>(61); tbn.left = new TreeBinaryNode <int>(61); tbn.right = new TreeBinaryNode <int>(43); Assert.True(tbn.value.Equals(6)); Assert.True(tbn.left.right == null); Assert.True(tbn.right.value.Equals(43)); }
private void minimalTreeEvenTest() { const int size = 8; int[] result = new int[size] { 7, 3, 1, 5, 11, 9, 13, 15 }; int[] array = buildSortedArray(size); TreeBinaryNode <int> tree = MinimalTree(array); checkValues(result, tree); }
private void minimalTreeOddTest() { const int size = 5; int[] result = new int[size] { 5, 1, 3, 7, 9 }; int[] array = buildSortedArray(5); TreeBinaryNode <int> tree = MinimalTree(array); checkValues(result, tree); }
private static void TreeBinaryNodeDifferentConstructorTest() { TreeBinaryNode <int> tbn = new TreeBinaryNode <int>(1, new TreeBinaryNode <int>(2), new TreeBinaryNode <int>(3)); Assert.True(tbn.value.Equals(1)); Assert.True(tbn.left.value.Equals(2)); Assert.True(tbn.right.value.Equals(3)); TreeBinaryNode <int> leftNode = tbn.left; leftNode.left = new TreeBinaryNode <int>(4); Assert.True(leftNode.left.value.Equals(4)); Assert.True(tbn.left.right == null); }
private static TreeBinaryParentNode <T> leftMostChild(TreeBinaryNode <T> node, TreeBinaryNode <T> lastNode) { TreeBinaryParentNode <T> lmc = null; if (node != null) { lmc = leftMostChild(node.left, node); } else { lmc = (TreeBinaryParentNode <T>)lastNode; } return(lmc); }
public static bool IsBST(TreeBinaryNode <int> tree) { if (tree == null) { return(true); } if (!IsBST(tree.left) || (lastNumber != null && tree.value <= lastNumber)) { return(false); } lastNumber = tree.value; return(IsBST(tree.right)); }
private void validateBSTTest() { Func <TreeBinaryNode <int>, bool>[] funcToRun = new Func <TreeBinaryNode <int>, bool>[] { bst.IsBST, bst2.IsBST, bst3.IsBST }; TreeBinaryNode <int> tree = CreateBinarySearchTree <TreeBinaryNode <int> >(13); TreeBinaryNode <int> tree2 = new TreeBinaryNode <int>(13, null, tree.right); foreach (var f in funcToRun) { Assert.True(f(tree)); Assert.False(f(tree2)); Assert.True(f(null)); } }
private void checkBalancedTest() { TreeBinaryNode <int> tree = CreateBinarySearchTree <TreeBinaryNode <int> >(15); //Differ by 0 TreeBinaryNode <int> tree2 = CreateBinarySearchTree <TreeBinaryNode <int> >(13); //Differ by 1 TreeBinaryNode <int> tree3 = new TreeBinaryNode <int>(100, tree, new TreeBinaryNode <int>(700)); TreeBinaryNode <int> tree4 = new TreeBinaryNode <int>(1); //Without Tweaks Assert.True(cb.isBalanced(tree)); Assert.True(cb.isBalanced(tree2)); Assert.False(cb.isBalanced(tree3)); Assert.True(cb.isBalanced(tree4)); //With Tweaks Assert.True(cbTweak.isBalanced(tree)); Assert.True(cbTweak.isBalanced(tree2)); Assert.False(cbTweak.isBalanced(tree3)); Assert.True(cbTweak.isBalanced(tree4)); }
public virtual T Visit(TreeBinaryNode node, T state) { return(DefaultVisit(node, state)); }
public static bool IsBST(TreeBinaryNode <int> root, int?min, int?max) => (root == null) ? true : (min == null || root.value > min) && (max == null || root.value < max) && IsBST(root.left, min, root.value) && IsBST(root.right, root.value, max);
public static bool IsBST(TreeBinaryNode <int> tree) { return(IsBST(tree, null, null)); }
public TreeBinaryNode(T v, TreeBinaryNode <T> left, TreeBinaryNode <T> right) { this.value = v; this.left = left; this.right = right; }
/// <summary> /// Determine if the values of a TreeBinaryNode are the same as the values provided. ///</summary> /// <param name="result"> Array of values to check.</param> /// <param name="tree"> TreeBinaryNode to check.</param> /// <param name="type"> Specify how to visit the TreeBinaryNode.</param> public static void checkValues <T>(T[] result, TreeBinaryNode <T> tree, Type type = Type.preOrder) { Traversal <T> t = new Traversal <T>(type, tree); Assert.True(t.EqualsToArray(result)); }
private void minimalTreeNullTest() { TreeBinaryNode <int> tree = MinimalTree <int>(null); Assert.Null(tree); }
private void minimalTreeEmptyTest() { TreeBinaryNode <int> tree = MinimalTree(new int[] { }); Assert.Null(tree); }
private void FCANullValuesWithoutLinkParentTest() { TreeBinaryNode <int> ancestor = commonAncestorWithoutLinkParent(tree, null, null); Assert.Equal(null, ancestor); }
private void FCARootWithoutLinkParentTest() { TreeBinaryNode <int> ancestor = commonAncestorWithoutLinkParent(tree, tree, tree); Assert.Equal(tree, ancestor); }