public void Should_Check_Remove_Multiple() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(38); tree.Insert(5); tree.Insert(45); tree.Insert(1); tree.Insert(9); tree.Insert(47); tree.Insert(8); tree.Insert(15); tree.Insert(46); tree.Insert(13); //act tree.Remove(9); tree.Remove(5); tree.Remove(15); //assert tree.Count.ShouldBeEquivalentTo(7); tree.Root.Data.ShouldBeEquivalentTo(38); tree.Root.Left.Data.ShouldBeEquivalentTo(1); tree.Root.Right.Data.ShouldBeEquivalentTo(45); tree.Root.Left.Right.Data.ShouldBeEquivalentTo(8); tree.Root.Right.Right.Data.ShouldBeEquivalentTo(47); tree.Root.Left.Right.Right.Data.ShouldBeEquivalentTo(13); tree.Root.Right.Right.Left.Data.ShouldBeEquivalentTo(46); }
public void FalseIfValueNotInSearchTree() { Node <int> node1 = new Node <int>(10); Node <int> node2 = new Node <int>(20); Node <int> node3 = new Node <int>(30); Node <int> node4 = new Node <int>(40); Node <int> node5 = new Node <int>(50); Node <int> node6 = new Node <int>(9); Node <int> node7 = new Node <int>(8); Node <int> node8 = new Node <int>(7); Node <int> node9 = new Node <int>(6); MyBinarySearchTree tree = new MyBinarySearchTree(); tree.Add(node1); tree.Add(node2); tree.Add(node3); tree.Add(node4); tree.Add(node5); tree.Add(node6); tree.Add(node7); tree.Add(node8); tree.Add(node9); Assert.False(tree.Contains(node1, 47)); }
public void Should_Check_PostOrder_Traversal() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(38); tree.Insert(5); tree.Insert(45); tree.Insert(1); tree.Insert(9); tree.Insert(47); tree.Insert(8); tree.Insert(15); tree.Insert(46); tree.Insert(13); var postOrderTraversal = new int[] { 1, 8, 13, 15, 9, 5, 46, 47, 45, 38 }; //act var result = tree.PostOrderTraversal().ToArray(); //assert tree.Count.ShouldBeEquivalentTo(10); postOrderTraversal.Length.ShouldBeEquivalentTo(result.Length); for (int i = 0; i < postOrderTraversal.Length; i++) { postOrderTraversal[i].ShouldBeEquivalentTo(result[i]); } }
public void Should_Check_Remove_Helper_Leaf_Node_Right() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(38); tree.Insert(5); tree.Insert(45); tree.Insert(1); tree.Insert(9); tree.Insert(47); tree.Insert(8); tree.Insert(15); tree.Insert(48); tree.Insert(13); //act tree.Remove(48); //assert tree.Count.ShouldBeEquivalentTo(9); tree.Root.Data.ShouldBeEquivalentTo(38); tree.Root.Left.Data.ShouldBeEquivalentTo(5); tree.Root.Right.Data.ShouldBeEquivalentTo(45); tree.Root.Left.Left.Data.ShouldBeEquivalentTo(1); tree.Root.Left.Right.Data.ShouldBeEquivalentTo(9); tree.Root.Right.Right.Data.ShouldBeEquivalentTo(47); tree.Root.Left.Right.Left.Data.ShouldBeEquivalentTo(8); tree.Root.Left.Right.Right.Data.ShouldBeEquivalentTo(15); tree.Root.Right.Right.Right.ShouldBeEquivalentTo(null); tree.Root.Left.Right.Right.Left.Data.ShouldBeEquivalentTo(13); }
public void WillReturnNullIfOneTreeEmpty() { MyBinarySearchTree treeOne = new MyBinarySearchTree(); MyBinarySearchTree treeTwo = new MyBinarySearchTree(); Node <int> nodeOne1 = new Node <int>(10); Node <int> nodeOne2 = new Node <int>(20); Node <int> nodeOne3 = new Node <int>(30); Node <int> nodeOne4 = new Node <int>(45); Node <int> nodeOne5 = new Node <int>(50); Node <int> nodeOne6 = new Node <int>(5); Node <int> nodeOne7 = new Node <int>(8); Node <int> nodeOne8 = new Node <int>(7); Node <int> nodeOne9 = new Node <int>(6); treeOne.Add(nodeOne1); treeOne.Add(nodeOne2); treeOne.Add(nodeOne3); treeOne.Add(nodeOne4); treeOne.Add(nodeOne5); treeOne.Add(nodeOne6); treeOne.Add(nodeOne7); treeOne.Add(nodeOne8); treeOne.Add(nodeOne9); Assert.Null(TreeIntersection.Program.TreeIntersection(treeOne, treeTwo)); }
public void FirstCommonAncestor_Should_Check_Multiple_Depth_One_Root() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(10); tree.Insert(6); tree.Insert(15); tree.Insert(4); tree.Insert(8); tree.Insert(13); tree.Insert(17); tree.Insert(3); tree.Insert(5); tree.Insert(7); tree.Insert(14); tree.Insert(2); //act var result = _treesGraphs.FirstCommonAncestor(tree, tree.Root.Left.Left.Left.Left, tree.Root.Left.Left); //assert result.ShouldBeEquivalentTo(tree.Root.Left); }
public void BSTSequences_Should_Check_Example() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(2); tree.Insert(1); tree.Insert(3); var res = new List <int[]>() { new int[] { 2, 1, 3 }, new int[] { 2, 3, 1 } }; //act var result = _treesGraphs.BSTSequences(tree); //assert result.Count.ShouldBeEquivalentTo(res.Count); for (int i = 0; i < result.Count; i++) { res[i].Length.ShouldBeEquivalentTo(result[i].Length); for (int j = 0; j < result[i].Length; j++) { res[i][j].ShouldBeEquivalentTo(result[i][j]); } } }
public void ValidateBST_Should_Multiple_Depth_False() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(10); tree.Insert(6); tree.Insert(15); tree.Insert(4); tree.Insert(8); tree.Insert(13); tree.Insert(17); tree.Insert(3); tree.Insert(5); tree.Insert(7); tree.Insert(14); tree.Insert(2); tree.Root.Left.Data = 8; //act var result = _treesGraphs.ValidateBST(tree); //assert result.ShouldBeEquivalentTo(false); }
public void CheckSubtree_Should_Check_Multiple_Depth_False() { //arrange var biggerTree = new MyBinarySearchTree <int>(); var smallerTree = new MyBinarySearchTree <int>(); biggerTree.Insert(10); biggerTree.Insert(6); biggerTree.Insert(15); biggerTree.Insert(4); biggerTree.Insert(8); biggerTree.Insert(13); biggerTree.Insert(17); biggerTree.Insert(3); biggerTree.Insert(5); biggerTree.Insert(7); biggerTree.Insert(14); biggerTree.Insert(2); smallerTree.Root = biggerTree.Root.Left.Left; biggerTree.Root = biggerTree.Root.Right; //act var result = _treesGraphs.CheckSubtree(biggerTree, smallerTree); //assert result.ShouldBeEquivalentTo(false); }
public bool ValidateBST(MyBinarySearchTree <int> tree) { if (tree == null) { throw new ArgumentNullException(); } return(ValidateBSTHelper(tree.Root)); }
/// <summary> /// Takes in two Binary Search Trees and uses a HashTable to find any values that are shared between the trees. Returns a List of all common values. If no values are shared returns null. /// </summary> /// <param name="treeOne">First Tree to compare against.</param> /// <param name="treeTwo">Second Tree to compare against.</param> /// <returns>List of shared values, or null if no values shared.</returns> public static List <int> TreeIntersection(MyBinarySearchTree treeOne, MyBinarySearchTree treeTwo) { if (treeOne.Root == null || treeTwo.Root == null) { return(null); } MyHashTable hashTable = new MyHashTable(); List <int> returnList = new List <int>(); Queue <Node <int> > queue = new Queue <Node <int> >(); queue.Enqueue(treeOne.Root); while (queue.Count != 0) { Node <int> node = queue.Dequeue(); if (node.LChild != null) { queue.Enqueue(node.LChild); } if (node.RChild != null) { queue.Enqueue(node.RChild); } hashTable.Add(node.Value.ToString(), ""); } queue.Enqueue(treeTwo.Root); while (queue.Count != 0) { Node <int> node = queue.Dequeue(); if (node.LChild != null) { queue.Enqueue(node.LChild); } if (node.RChild != null) { queue.Enqueue(node.RChild); } if (hashTable.Contains(node.Value.ToString())) { returnList.Add(node.Value); hashTable.Remove(node.Value.ToString(), ""); } } if (returnList.Count > 0) { return(returnList); } return(null); }
public bool CheckBalanced(MyBinarySearchTree <int> tree) { if (tree == null) { throw new ArgumentNullException(); } return(CheckBalancedHelper(tree.Root) != Int32.MinValue); }
public List <int[]> BSTSequences(MyBinarySearchTree <int> tree) { if (tree == null) { throw new ArgumentNullException(); } var result = BSTSequencesHelper(tree.Root); return(result.Select(x => x.ToArray()).ToList()); }
public MyBinarySearchTreeNode <int> FirstCommonAncestor(MyBinarySearchTree <int> tree, MyBinarySearchTreeNode <int> firstNode, MyBinarySearchTreeNode <int> secondNode) { if (tree == null || firstNode == null || secondNode == null) { throw new ArgumentNullException(); } return(FirstCommonAncestorHelper(tree.Root, firstNode, secondNode)); }
public void BSTSequences_Should_Check_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.BSTSequences(tree); //assert result.Count.ShouldBeEquivalentTo(0); }
public void Should_Check_Contains_When_Empty_Tree() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = tree.Contains(5); //assert result.ShouldBeEquivalentTo(false); }
public void ValidateBST_Should_Check_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.ValidateBST(tree); //assert result.ShouldBeEquivalentTo(true); }
public void CheckBalanced_Should_Check_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.CheckBalanced(tree); //assert result.ShouldBeEquivalentTo(true); }
public void PathsWithSum_Should_Check_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.PathsWithSum(tree, 1); //assert result.ShouldBeEquivalentTo(0); }
public void RandomNode_Should_Check_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.RandomNode(tree); //assert result.ShouldBeEquivalentTo(null); }
public void Should_Create_Default() { //arrange //act var result = new MyBinarySearchTree <int>(); //assert result.Count.ShouldBeEquivalentTo(0); result.Root.ShouldBeEquivalentTo(null); }
//First implementation public int PathsWithSum(MyBinarySearchTree <int> tree, int target) { if (tree == null) { throw new ArgumentNullException(); } var map = new Dictionary <MyBinarySearchTreeNode <int>, List <int> >(); PathsWithSumHelper(tree.Root, map, null); return(map.Sum(x => x.Value.Count(y => y == target))); }
public void Should_Check_Remove_When_Is_Empty() { //arrange var tree = new MyBinarySearchTree <int>(); //act tree.Remove(3); //assert tree.Count.ShouldBeEquivalentTo(0); tree.Root.ShouldBeEquivalentTo(null); }
public void CheckSubtree_Should_Check_Two_Empty() { //arrange var biggerTree = new MyBinarySearchTree <int>(); var smallerTree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.CheckSubtree(biggerTree, smallerTree); //assert result.ShouldBeEquivalentTo(true); }
public void ListOfDepth_Should_Empty_Tree() { //arrange var tree = new MyBinarySearchTree <int>(); //act var result = _treesGraphs.ListOfDepth(tree); //assert result.Should().NotBeNull(); result.Length.ShouldBeEquivalentTo(0); }
public MyBinarySearchTreeNode <int> Successor(MyBinarySearchTree <int> tree, MyBinarySearchTreeNode <int> node) { if (tree == null || node == null) { throw new ArgumentNullException(); } bool isNext = false; return(SuccessorHelper(tree.Root, node, ref isNext)); }
public void WillReturnListOfSharedValues() { MyBinarySearchTree treeOne = new MyBinarySearchTree(); MyBinarySearchTree treeTwo = new MyBinarySearchTree(); Node <int> nodeOne1 = new Node <int>(10); Node <int> nodeOne2 = new Node <int>(20); Node <int> nodeOne3 = new Node <int>(30); Node <int> nodeOne4 = new Node <int>(45); Node <int> nodeOne5 = new Node <int>(50); Node <int> nodeOne6 = new Node <int>(5); Node <int> nodeOne7 = new Node <int>(8); Node <int> nodeOne8 = new Node <int>(7); Node <int> nodeOne9 = new Node <int>(6); Node <int> nodeTwo1 = new Node <int>(10); Node <int> nodeTwo2 = new Node <int>(18); Node <int> nodeTwo3 = new Node <int>(30); Node <int> nodeTwo4 = new Node <int>(40); Node <int> nodeTwo5 = new Node <int>(50); Node <int> nodeTwo6 = new Node <int>(9); Node <int> nodeTwo7 = new Node <int>(8); Node <int> nodeTwo8 = new Node <int>(7); Node <int> nodeTwo9 = new Node <int>(1); treeOne.Add(nodeOne1); treeOne.Add(nodeOne2); treeOne.Add(nodeOne3); treeOne.Add(nodeOne4); treeOne.Add(nodeOne5); treeOne.Add(nodeOne6); treeOne.Add(nodeOne7); treeOne.Add(nodeOne8); treeOne.Add(nodeOne9); treeTwo.Add(nodeTwo1); treeTwo.Add(nodeTwo2); treeTwo.Add(nodeTwo3); treeTwo.Add(nodeTwo4); treeTwo.Add(nodeTwo5); treeTwo.Add(nodeTwo6); treeTwo.Add(nodeTwo7); treeTwo.Add(nodeTwo8); treeTwo.Add(nodeTwo9); int[] expectedArr = new int[] { 10, 8, 30, 7, 50 }; List <int> resultList = TreeIntersection.Program.TreeIntersection(treeOne, treeTwo); int[] resultArr = resultList.ToArray(); Assert.Equal(expectedArr, resultArr); }
public void TestAdd() { var bst = new MyBinarySearchTree <int>(50); Assert.IsNotNull(bst.RootNode); Assert.AreEqual(50, (int)bst.RootNode.Data, "Root node is not the expected value 50. Instead, its value is {0}", bst.RootNode.Data); //try to add a value already there; should get back the same node var expectedRootNode = bst.AddNode(50); Assert.ReferenceEquals(expectedRootNode, bst.RootNode); var fortyNode = bst.AddNode(40); var rootNode = bst.RootNode; Assert.IsNotNull(rootNode.LeftNode, "Root's left node is null when it should not be. Node 40 added incorrectly"); Assert.IsNull(rootNode.RightNode, "Root's right node is not null when it should be. Node 40 added incorrectly"); Assert.IsTrue(bst.IsDataPresent(40), "Could not find added node 40"); Assert.AreEqual(2, bst.Count, $"Expected count 2. Actual count is {bst.Count}"); var sixtyNode = bst.AddNode(60); Assert.IsNotNull(rootNode.RightNode, "Root's right node is null when it should not be. Node 60 added incorrectly"); Assert.IsTrue(bst.IsDataPresent(60), "Could not find added node 60"); Assert.AreEqual(3, bst.Count, $"Expected count 3. Actual count is {bst.Count}"); //add another "greater than" node, to the right of 60 var seventyNode = bst.AddNode(70); Assert.IsNotNull(rootNode.RightNode.RightNode, "Right node of root's right node is null when it should not be. Node 70 added incorrectly"); //add a node on the left of the root's right node var fiftyFiveNode = bst.AddNode(55); Assert.IsNotNull(rootNode.RightNode.LeftNode, "Left node of root's right is null when it should not be. Node 55 added incorrectly"); //add a node to the right of the root's left node var fortyFiveNode = bst.AddNode(45); Assert.IsNotNull(rootNode.LeftNode.RightNode, "Right node of root's left is null when it should not be. Node 45 added incorrectly"); //add a node to the left of the root's left node var thirtyFiveNode = bst.AddNode(35); Assert.IsNotNull(rootNode.LeftNode.LeftNode, "Left node of root's left is null when it should not be. Node 35 added incorrectly"); //try to add a null node var bst2 = new MyBinarySearchTree <String>("hello"); Assert.ThrowsException <ArgumentNullException>(() => bst2.AddNode(null)); }
public void Successor_Should_Check_Length_One() { //arrange var tree = new MyBinarySearchTree <int>(); tree.Insert(2); //act var result = _treesGraphs.Successor(tree, tree.Root); //assert result.ShouldBeEquivalentTo(null); }
public MyBinarySearchTree <int> MinimalTree(int[] values) { if (values == null) { throw new ArgumentNullException(); } var tree = new MyBinarySearchTree <int>(); MinimalTreeHelper(values, 0, values.Length - 1, tree); return(tree); }