public void CurrentSum() { TreeNode n = new TreeNode(5) { Left = new TreeNode(-3) { Left = new TreeNode(5) { Left = new TreeNode(-3) } }, Right = new TreeNode(8) { Left = new TreeNode(-2) { Left = new TreeNode(-9) }, Right = new TreeNode(-11) } }; var result = TreesAndGraphs.PathSum(n, 2); Assert.AreEqual(5, result); }
public void BSTSequence() { TreeNode n = new TreeNode(20) { Left = new TreeNode(10) { Left = new TreeNode(5) }, Right = new TreeNode(30) { Left = new TreeNode(25), Right = new TreeNode(40) } }; var result = TreesAndGraphs.BSTSequence(n); foreach (var r in result) { Trace.WriteLine(""); foreach (var x in r) { Trace.Write(x + ", "); } } }
public void TestCheckSubtree() { // Arrange BTNode <int> root1 = new BTNode <int>(8); root1.Right = new BTNode <int>(9); root1.Left = new BTNode <int>(2); root1.Left.Left = new BTNode <int>(1); root1.Left.Right = new BTNode <int>(4); root1.Left.Right.Left = new BTNode <int>(3); root1.Left.Right.Right = new BTNode <int>(6); root1.Left.Right.Right.Right = new BTNode <int>(7); BTNode <int> root2 = new BTNode <int>(2); root2.Left = new BTNode <int>(1); root2.Right = new BTNode <int>(4); root2.Right.Left = new BTNode <int>(3); root2.Right.Right = new BTNode <int>(6); // Act bool isSubtree = TreesAndGraphs.CheckSubtree(root1.Left, root2); // Assert Assert.IsTrue(isSubtree); }
public void RouteBetweenNodes(string nodes, string edges, int startNode, int endNode, bool exptectedResult) { Graph g = new Graph(); GraphNode <int> n1 = null; GraphNode <int> n2 = null; nodes.Split(',').Select(int.Parse).ToList().ForEach(x => { var node = g.AddNode(x); if (x == startNode) { n1 = node; } if (x == endNode) { n2 = node; } }); edges.Split('|').ToList().ForEach(x => { if (x != "") { var y = x.Split(','); g.AddEdge(int.Parse(y[0]), int.Parse(y[1])); } }); var actualResult = TreesAndGraphs.RouteBetweenNodes(n1, n2); Assert.AreEqual(exptectedResult, actualResult); }
public void FistCommonAncestor() { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(1); TreeNode c = new TreeNode(1); TreeNode d = new TreeNode(1); TreeNode e = new TreeNode(1); a.Left = b; b.Left = c; b.Right = d; a.Right = e; var result = TreesAndGraphs.FirstCommonAncestor(a, a, a); Assert.AreSame(a, result); result = TreesAndGraphs.FirstCommonAncestor(a, a, b); Assert.AreSame(a, result); result = TreesAndGraphs.FirstCommonAncestor(a, b, c); Assert.AreSame(b, result); result = TreesAndGraphs.FirstCommonAncestor(a, a, c); Assert.AreSame(a, result); result = TreesAndGraphs.FirstCommonAncestor(a, d, e); Assert.AreSame(a, result); }
public void TestGetVisibleNodesFromLeft() { TreeNode <int> root_1 = new TreeNode <int>(8); root_1.LeftNode = new TreeNode <int>(3); root_1.RightNode = new TreeNode <int>(10); root_1.LeftNode.LeftNode = new TreeNode <int>(1); root_1.LeftNode.RightNode = new TreeNode <int>(6); root_1.RightNode.RightNode = new TreeNode <int>(14); root_1.LeftNode.RightNode.LeftNode = new TreeNode <int>(4); root_1.LeftNode.RightNode.RightNode = new TreeNode <int>(7); root_1.RightNode.RightNode.LeftNode = new TreeNode <int>(13); int expected_1 = 4; int output_1 = TreesAndGraphs.GetVisibleNodesFromLeft(root_1); Assert.AreEqual(expected_1, output_1); TreeNode <int> root_2 = new TreeNode <int>(10); root_2.LeftNode = new TreeNode <int>(8); root_2.RightNode = new TreeNode <int>(15); root_2.LeftNode.LeftNode = new TreeNode <int>(4); root_2.LeftNode.LeftNode.RightNode = new TreeNode <int>(5); root_2.LeftNode.LeftNode.RightNode.RightNode = new TreeNode <int>(6); root_2.RightNode.LeftNode = new TreeNode <int>(14); root_2.RightNode.RightNode = new TreeNode <int>(16); int expected_2 = 5; int output_2 = TreesAndGraphs.GetVisibleNodesFromLeft(root_2); Assert.AreEqual(expected_2, output_2); }
public void Successor() { var n = new TreeNode(4) { Left = new TreeNode(2) { Left = new TreeNode(1), Right = new TreeNode(3) }, Right = new TreeNode(6) { Left = new TreeNode(5), Right = new TreeNode(7) } }; var result = TreesAndGraphs.Successor(n); Assert.AreEqual(result.Value, 5); result = TreesAndGraphs.Successor(n.Left.Right); Assert.AreEqual(result.Value, 4); result = TreesAndGraphs.Successor(n.Right.Right); Assert.AreEqual(result, null); result = TreesAndGraphs.Successor(n.Right); Assert.AreEqual(result.Value, 7); result = TreesAndGraphs.Successor(n.Left.Left); Assert.AreEqual(result.Value, 2); }
public void GetHeightOfTreeTest() { // crate a tree TreeNode <int> root = new TreeNode <int>(3); TreesAndGraphs.Insert(root, 2); TreesAndGraphs.Insert(root, 1); TreesAndGraphs.Insert(root, 5); TreesAndGraphs.Insert(root, 4); TreesAndGraphs.Insert(root, 6); TreesAndGraphs.Insert(root, 7); var height = TreesAndGraphs.GetHeightOfTree(root); Assert.AreEqual(3, height); root = new TreeNode <int>(3); TreesAndGraphs.Insert(root, 2); TreesAndGraphs.Insert(root, 1); TreesAndGraphs.Insert(root, 5); TreesAndGraphs.Insert(root, 4); TreesAndGraphs.Insert(root, 6); TreesAndGraphs.Insert(root, 7); TreesAndGraphs.Insert(root, 8); TreesAndGraphs.Insert(root, 20); TreesAndGraphs.Insert(root, 19); TreesAndGraphs.Insert(root, 18); TreesAndGraphs.Insert(root, 16); TreesAndGraphs.Insert(root, 15); TreesAndGraphs.Insert(root, 17); height = TreesAndGraphs.GetHeightOfTree(root); Assert.AreEqual(9, height); }
public void ListOfDepths() { int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var x = TreesAndGraphs.MinimalTree(array); var result = TreesAndGraphs.ListOfDepths(x); Assert.Inconclusive(); }
public void MinimalTree() { int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var x = TreesAndGraphs.MinimalTree(array); Assert.Inconclusive(); }
public void IsBST() { var n = new TreeNode(2); n.Left = new TreeNode(0); n.Right = new TreeNode(3); n.Right.Right = new TreeNode(4); var result = TreesAndGraphs.IsBST(n); Assert.Inconclusive(); }
public void TesIfABinaryTreeIsBalancedOrNot() { // create a new tree sructure TreeNode <int> root = new TreeNode <int>(3); TreesAndGraphs.Insert(root, 2); TreesAndGraphs.Insert(root, 1); TreesAndGraphs.Insert(root, 3); TreesAndGraphs.Insert(root, 5); TreesAndGraphs.Insert(root, 4); TreesAndGraphs.Insert(root, 6); List <LinkedList <TreeNode <int> > > list = new List <LinkedList <TreeNode <int> > >(); var output = TreesAndGraphs.IsBalanced(root); }
public void TestValidateBST() { // Arrange // null case BTNode <int> nullRoot = null; // single node BTNode <int> singleNode = new BTNode <int>(1); // common case true BTNode <int> commonCaseTrueRoot = new BTNode <int>(5); commonCaseTrueRoot.Left = new BTNode <int>(2); commonCaseTrueRoot.Left.Left = new BTNode <int>(1); commonCaseTrueRoot.Left.Right = new BTNode <int>(3); commonCaseTrueRoot.Right = new BTNode <int>(8); commonCaseTrueRoot.Right = new BTNode <int>(6); // common case false 1 BTNode <int> commonCaseFalseRoot1 = new BTNode <int>(3); commonCaseFalseRoot1.Left = new BTNode <int>(1); commonCaseFalseRoot1.Left.Left = new BTNode <int>(0); commonCaseFalseRoot1.Left.Right = new BTNode <int>(4); commonCaseFalseRoot1.Right = new BTNode <int>(5); commonCaseFalseRoot1.Right.Left = new BTNode <int>(5); commonCaseFalseRoot1.Right.Right = new BTNode <int>(7); // common case false 2 BTNode <int> commonCaseFalseRoot2 = new BTNode <int>(4); commonCaseFalseRoot2.Left = new BTNode <int>(2); commonCaseFalseRoot2.Right = new BTNode <int>(6); commonCaseFalseRoot2.Right.Left = new BTNode <int>(1); // Act bool nullRootResult = TreesAndGraphs.ValidateBST(nullRoot); bool singleNodeResult = TreesAndGraphs.ValidateBST(singleNode); bool commonCaseTrueResult = TreesAndGraphs.ValidateBST(commonCaseTrueRoot); bool commonCaseFalseResult1 = TreesAndGraphs.ValidateBST(commonCaseFalseRoot1); bool commonCaseFalseResult2 = TreesAndGraphs.ValidateBST(commonCaseFalseRoot2); // Assert Assert.IsFalse(nullRootResult); Assert.IsTrue(singleNodeResult); Assert.IsTrue(commonCaseTrueResult); Assert.IsFalse(commonCaseFalseResult1); Assert.IsFalse(commonCaseFalseResult2); }
public void CreateLevelLinkedList() { // create a new tree sructure TreeNode <int> root = new TreeNode <int>(3); TreesAndGraphs.Insert(root, 2); TreesAndGraphs.Insert(root, 1); TreesAndGraphs.Insert(root, 3); TreesAndGraphs.Insert(root, 5); TreesAndGraphs.Insert(root, 4); TreesAndGraphs.Insert(root, 6); List <LinkedList <TreeNode <int> > > list = new List <LinkedList <TreeNode <int> > >(); TreesAndGraphs.CreateLevelLinkedList(root, list, 0); // View the content of the list to see the linkedlist structure }
public void TestBuildOrderException() { // Arrange List <char> case1Projects = new List <char> { 'a', 'b', 'c', 'd', 'e', 'f' }; List <Tuple <char, char> > case1Dependencies = new List <Tuple <char, char> > { new Tuple <char, char> ('a', 'd'), new Tuple <char, char> ('f', 'b'), new Tuple <char, char> ('b', 'd'), new Tuple <char, char> ('f', 'a'), new Tuple <char, char> ('d', 'c'), new Tuple <char, char> ('c', 'a'), }; // Act List <char> case1Result = TreesAndGraphs.BuildOrder(case1Projects, case1Dependencies); }
public void TestFirstCommonAncestor() { // Arrange // null case BTNode <int> nullRoot = null; BTNode <int> nullCaseA = new BTNode <int>(1); BTNode <int> nullCaseB = new BTNode <int>(2); // common cases BTNode <int> commonCaseRoot = new BTNode <int>(6); BTNode <int> commonNode4 = new BTNode <int>(4); BTNode <int> commonNode8 = new BTNode <int>(8); commonCaseRoot.Left = commonNode4; commonCaseRoot.Right = commonNode8; BTNode <int> commonNode1 = new BTNode <int>(1); BTNode <int> commonNode5 = new BTNode <int>(5); commonNode4.Left = commonNode1; commonNode4.Right = commonNode5; BTNode <int> commonNode0 = new BTNode <int>(0); BTNode <int> commonNode3 = new BTNode <int>(3); commonNode1.Left = commonNode0; commonNode1.Right = commonNode3; BTNode <int> commonNode7 = new BTNode <int>(7); BTNode <int> commonNode9 = new BTNode <int>(9); commonNode8.Left = commonNode7; commonNode8.Right = commonNode9; // Act BTNode <int> nullResult = TreesAndGraphs.FirstCommonAncestor(nullRoot, nullCaseA, nullCaseB); BTNode <int> commonCaseResult1 = TreesAndGraphs.FirstCommonAncestor(commonCaseRoot, commonNode3, commonNode5); BTNode <int> commonCaseResult2 = TreesAndGraphs.FirstCommonAncestor(commonCaseRoot, commonNode0, commonNode9); BTNode <int> commonCaseResult3 = TreesAndGraphs.FirstCommonAncestor(commonCaseRoot, commonNode9, commonNode7); // Assert Assert.IsNull(nullResult); Assert.AreEqual(commonNode4, commonCaseResult1); Assert.AreEqual(commonCaseRoot, commonCaseResult2); Assert.AreEqual(commonNode8, commonCaseResult3); }
public void BuildOrder() { DirectedGraph g = new DirectedGraph(); g.AddNode(1); g.AddNode(2); g.AddNode(3); g.AddNode(4); g.AddNode(5); g.AddNode(6); g.AddNode(7); g.AddNode(8); g.AddEdge(1, 2); g.AddEdge(2, 3); g.AddEdge(2, 4); g.AddEdge(3, 5); g.AddEdge(6, 7); g.AddEdge(7, 8); var result = TreesAndGraphs.BuildOrder(g); Assert.IsNotNull(result); foreach (var x in result) { Trace.Write(x.Data.ToString() + "->"); } g = new DirectedGraph(); g.AddNode(1); g.AddNode(2); g.AddNode(3); g.AddNode(4); g.AddEdge(1, 2); g.AddEdge(2, 3); g.AddEdge(4, 2); g.AddEdge(3, 4); result = TreesAndGraphs.BuildOrder(g); Assert.IsNull(result); }
public void TestListOfDepths() { // Arrange // null case BTNode <int> nullNode = null; // common case BTNode <int> commonCaseRoot = new BTNode <int>(3); BTNode <int> firstLevelLeft = new BTNode <int>(1); BTNode <int> firstLevelRight = new BTNode <int>(8); commonCaseRoot.Left = firstLevelLeft; commonCaseRoot.Right = firstLevelRight; BTNode <int> secondLevelLeftLeft = new BTNode <int>(0); BTNode <int> secondLevelLeftRight = new BTNode <int>(2); firstLevelLeft.Left = secondLevelLeftLeft; firstLevelLeft.Right = secondLevelLeftRight; BTNode <int> secondLevelRightLeft = new BTNode <int>(7); BTNode <int> secondLevelRightRight = new BTNode <int>(9); firstLevelRight.Left = secondLevelRightLeft; firstLevelRight.Right = secondLevelRightRight; // Act List <SLLNode> nullNodeResult = TreesAndGraphs.ListOfDepths(nullNode); List <SLLNode> commonCaseResult = TreesAndGraphs.ListOfDepths(commonCaseRoot); // Assert Assert.IsNull(nullNodeResult); Assert.AreEqual(3, commonCaseResult.Count); Assert.AreEqual(3, commonCaseResult[0].Data); Assert.IsNull(commonCaseResult[0].Next); Assert.AreEqual(1, commonCaseResult[1].Data); Assert.AreEqual(8, commonCaseResult[1].Next.Data); Assert.IsNull(commonCaseResult[1].Next.Next); Assert.AreEqual(0, commonCaseResult[2].Data); Assert.AreEqual(2, commonCaseResult[2].Next.Data); Assert.AreEqual(7, commonCaseResult[2].Next.Next.Data); Assert.AreEqual(9, commonCaseResult[2].Next.Next.Next.Data); Assert.IsNull(commonCaseResult[2].Next.Next.Next.Next); }
public void TestBuildOrder() { // Arrange List <char> case1Projects = new List <char> { 'a', 'b', 'c', 'd', 'e', 'f' }; List <Tuple <char, char> > case1Dependencies = new List <Tuple <char, char> > { new Tuple <char, char> ('a', 'd'), new Tuple <char, char> ('f', 'b'), new Tuple <char, char> ('b', 'd'), new Tuple <char, char> ('f', 'a'), new Tuple <char, char> ('d', 'c') }; List <char> case2Projects = new List <char> { 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; List <Tuple <char, char> > case2Dependencies = new List <Tuple <char, char> > { new Tuple <char, char> ('a', 'd'), new Tuple <char, char> ('f', 'b'), new Tuple <char, char> ('b', 'd'), new Tuple <char, char> ('f', 'a'), new Tuple <char, char> ('d', 'c'), new Tuple <char, char> ('g', 'b') }; // Act List <char> case1Result = TreesAndGraphs.BuildOrder(case1Projects, case1Dependencies); List <char> case2Result = TreesAndGraphs.BuildOrder(case2Projects, case2Dependencies); // Assert List <char> case1Expected = new List <char> { 'f', 'e', 'b', 'a', 'd', 'c' }; CollectionAssert.AreEqual(case1Expected, case1Result); List <char> case2Expected = new List <char> { 'f', 'g', 'e', 'b', 'a', 'd', 'c' }; CollectionAssert.AreEqual(case2Expected, case2Result); }
public void IsBalanced() { //uses minimal tree as a helper.. (good unit tests wouldn't depend on anoter) int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; var x = TreesAndGraphs.MinimalTree(array); var result = TreesAndGraphs.IsBalanced(x); Assert.IsTrue(result); TreeNode t = new TreeNode(1); t.Left = new TreeNode(2); t.Left.Left = new TreeNode(3); result = TreesAndGraphs.IsBalanced(t); Assert.IsFalse(result); t.Right = new TreeNode(4); result = TreesAndGraphs.IsBalanced(t); Assert.IsTrue(result); }
public void TestPathsWithSum() { // Arrange BTNode <int> root = new BTNode <int>(5); root.Left = new BTNode <int>(2); root.Left.Right = new BTNode <int>(-7); root.Left.Right.Left = new BTNode <int>(5); root.Right = new BTNode <int>(-20); root.Right.Left = new BTNode <int>(9); root.Right.Left.Right = new BTNode <int>(4); root.Right.Left.Right.Left = new BTNode <int>(7); root.Right.Right = new BTNode <int>(8); int sum = -12; int expected = 1; // Act int actual = TreesAndGraphs.PathsWithSum(root, sum); // Assert Assert.AreEqual(expected, actual); }
public void TestCheckBalanced() { // Arrange // null case BTNode <int> nullRoot = null; // single node case BTNode <int> singleNode = new BTNode <int>(1); // common case true BTNode <int> trueRoot = new BTNode <int>(1); trueRoot.Left = new BTNode <int>(2); trueRoot.Left.Left = new BTNode <int>(4); trueRoot.Right = new BTNode <int>(3); // common case false BTNode <int> falseRoot = new BTNode <int>(1); falseRoot.Left = new BTNode <int>(2); falseRoot.Right = new BTNode <int>(3); falseRoot.Left.Right = new BTNode <int>(4); falseRoot.Left.Right.Left = new BTNode <int>(5); // Act bool nullRootResult = TreesAndGraphs.CheckBalanced(nullRoot); bool singleNodeResult = TreesAndGraphs.CheckBalanced(singleNode); bool commonCaseTrueResult = TreesAndGraphs.CheckBalanced(trueRoot); bool commonCaseFalseResult = TreesAndGraphs.CheckBalanced(falseRoot); // Assert Assert.IsFalse(nullRootResult); Assert.IsTrue(singleNodeResult); Assert.IsTrue(commonCaseTrueResult); Assert.IsFalse(commonCaseFalseResult); }
public void TestcountOfNodes() { // Testcase 2 int n_2 = 7, q_2 = 3; String s_2 = "abaacab"; GeneralTreeNode <int> root_2 = new GeneralTreeNode <int>(1); root_2.ChildNodes.Add(new GeneralTreeNode <int>(2)); root_2.ChildNodes.Add(new GeneralTreeNode <int>(3)); root_2.ChildNodes.Add(new GeneralTreeNode <int>(7)); root_2.ChildNodes[0].ChildNodes.Add(new GeneralTreeNode <int>(4)); root_2.ChildNodes[0].ChildNodes.Add(new GeneralTreeNode <int>(5)); root_2.ChildNodes[1].ChildNodes.Add(new GeneralTreeNode <int>(6)); var queries_2 = new List <Query>(); queries_2.Add(new Query(1, 'a')); queries_2.Add(new Query(2, 'b')); queries_2.Add(new Query(3, 'a')); int[] output_2 = TreesAndGraphs.CountOfNodes(root_2, queries_2, s_2); int[] expected_2 = { 4, 1, 2 }; Assert.AreEqual(expected_2, output_2); //Testcase 1 int n_1 = 3, q_1 = 1; String s_1 = "aba"; GeneralTreeNode <int> root_1 = new GeneralTreeNode <int>(1); root_1.ChildNodes.Add(new GeneralTreeNode <int>(2)); root_1.ChildNodes.Add(new GeneralTreeNode <int>(3)); var queries_1 = new List <Query>(); queries_1.Add(new Query(1, 'a')); int[] output_1 = TreesAndGraphs.CountOfNodes(root_1, queries_1, s_1); int[] expected_1 = { 2 }; Assert.AreEqual(expected_1, output_1); }
public void TestSuccessor() { // Arrange // null node BSTPNode nullNode = null; // single node BSTPNode singleNode = new BSTPNode(1, null); // common case BSTPNode commonCaseRoot = new BSTPNode(10, null); BSTPNode commonCaseLeft = new BSTPNode(7, commonCaseRoot); BSTPNode commonCaseLeftRight = new BSTPNode(9, commonCaseLeft); BSTPNode commonCaseLeftRightLeft = new BSTPNode(8, commonCaseLeftRight); BSTPNode commonCaseRight = new BSTPNode(11, commonCaseRoot); commonCaseRoot.Left = commonCaseLeft; commonCaseRoot.Right = commonCaseRight; commonCaseLeft.Right = commonCaseLeftRight; commonCaseLeftRight.Left = commonCaseLeftRightLeft; // Act BSTPNode nullNodeResult = TreesAndGraphs.Successor(nullNode); BSTPNode singleNodeResult = TreesAndGraphs.Successor(singleNode); BSTPNode commonCaseResult1 = TreesAndGraphs.Successor(commonCaseLeft); BSTPNode commonCaseResult2 = TreesAndGraphs.Successor(commonCaseLeftRight); BSTPNode commonCaseResult3 = TreesAndGraphs.Successor(commonCaseRight); // Assert Assert.IsNull(nullNodeResult); Assert.IsNull(singleNodeResult); Assert.AreEqual(commonCaseLeftRightLeft, commonCaseResult1); Assert.AreEqual(commonCaseRoot, commonCaseResult2); Assert.IsNull(commonCaseResult3); }
public void TestRouteExists() { // Arrange // null DirectedGraphNode nullSource = null; DirectedGraphNode nullDest = new DirectedGraphNode(1); // source == dest DirectedGraphNode sameSource = new DirectedGraphNode(1); DirectedGraphNode sameDest = sameSource; // common case DirectedGraphNode commonCaseSource = new DirectedGraphNode(1); DirectedGraphNode commonCase2 = new DirectedGraphNode(2); DirectedGraphNode commonCase3 = new DirectedGraphNode(3); DirectedGraphNode commonCaseDest = new DirectedGraphNode(4); DirectedGraphNode commonCase5 = new DirectedGraphNode(5); commonCaseSource.Children.Add(commonCase2); commonCaseSource.Children.Add(commonCase3); commonCase2.Children.Add(commonCase3); commonCase3.Children.Add(commonCaseDest); // Act bool nullResult = TreesAndGraphs.RouteExists(nullSource, nullDest); bool sameResult = TreesAndGraphs.RouteExists(sameSource, sameDest); bool commonCaseResult = TreesAndGraphs.RouteExists(commonCaseSource, commonCaseDest); bool commonCaseFalseResult = TreesAndGraphs.RouteExists(commonCaseSource, commonCase5); // Assert Assert.IsFalse(nullResult); Assert.IsTrue(sameResult); Assert.IsTrue(commonCaseResult); Assert.IsFalse(commonCaseFalseResult); }
public void TestMinimalTree() { // Arrange // empty list List <int> emptyList = new List <int>(); List <int> nullList = null; // single val base case List <int> singleValList = new List <int>(); singleValList.Add(1); // 2 values base case List <int> twoValueList = new List <int>(); twoValueList.Add(1); twoValueList.Add(2); // common case List <int> commonCaseList1 = new List <int>(); commonCaseList1.Add(1); commonCaseList1.Add(2); commonCaseList1.Add(3); commonCaseList1.Add(4); List <int> commonCaseList2 = new List <int>(); commonCaseList2.Add(1); commonCaseList2.Add(2); commonCaseList2.Add(2); commonCaseList2.Add(3); commonCaseList2.Add(4); // Act BSTNode emptyListResult = TreesAndGraphs.MinimalTree(emptyList); BSTNode nullListResult = TreesAndGraphs.MinimalTree(nullList); BSTNode singleValResult = TreesAndGraphs.MinimalTree(singleValList); BSTNode twoValueResult = TreesAndGraphs.MinimalTree(twoValueList); BSTNode commonCaseList1Result = TreesAndGraphs.MinimalTree(commonCaseList1); BSTNode commonCaseList2Result = TreesAndGraphs.MinimalTree(commonCaseList2); // Assert Assert.IsNull(emptyListResult); Assert.IsNull(nullListResult); Assert.AreEqual(1, singleValResult.Data); Assert.IsNull(singleValResult.Left); Assert.IsNull(singleValResult.Right); Assert.AreEqual(2, twoValueResult.Data); Assert.AreEqual(1, twoValueResult.Left.Data); Assert.IsNull(twoValueResult.Right); Assert.AreEqual(3, commonCaseList1Result.Data); Assert.AreEqual(2, commonCaseList1Result.Left.Data); Assert.AreEqual(1, commonCaseList1Result.Left.Left.Data); Assert.IsNull(commonCaseList1Result.Left.Left.Right); Assert.IsNull(commonCaseList1Result.Left.Left.Left); Assert.IsNull(commonCaseList1Result.Left.Right); Assert.AreEqual(4, commonCaseList1Result.Right.Data); Assert.IsNull(commonCaseList1Result.Right.Left); Assert.IsNull(commonCaseList1Result.Right.Right); Assert.AreEqual(2, commonCaseList2Result.Data); Assert.AreEqual(2, commonCaseList2Result.Left.Data); Assert.AreEqual(1, commonCaseList2Result.Left.Left.Data); Assert.IsNull(commonCaseList2Result.Left.Right); Assert.IsNull(commonCaseList2Result.Left.Left.Left); Assert.IsNull(commonCaseList2Result.Left.Left.Right); Assert.AreEqual(4, commonCaseList2Result.Right.Data); Assert.AreEqual(3, commonCaseList2Result.Right.Left.Data); Assert.IsNull(commonCaseList2Result.Right.Right); Assert.IsNull(commonCaseList2Result.Right.Left.Left); Assert.IsNull(commonCaseList2Result.Right.Left.Right); }
public void CheckSubtree() { TreeNode n = new TreeNode(4) { Left = new TreeNode(1) { Left = new TreeNode(4), Right = new TreeNode(11) }, Right = new TreeNode(5) { Left = new TreeNode(444), Right = new TreeNode(115) { Right = new TreeNode(33) { Right = new TreeNode(22) } } } }; var subtree = new TreeNode(5) { Left = new TreeNode(444), Right = new TreeNode(115) { Right = new TreeNode(33) { Right = new TreeNode(22) } } }; var result = TreesAndGraphs.CheckSubtree(n, subtree); Assert.IsTrue(result); n = new TreeNode(4) { Left = new TreeNode(1) { Left = new TreeNode(4) { Left = new TreeNode(1) }, } }; subtree = new TreeNode(4) { Left = new TreeNode(1) }; result = TreesAndGraphs.CheckSubtree(n, subtree); Assert.IsTrue(result); n.Left.Value = 2; result = TreesAndGraphs.CheckSubtree(n, subtree); Assert.IsTrue(result); n.Left.Left.Left.Value = 2; result = TreesAndGraphs.CheckSubtree(n, subtree); Assert.IsFalse(result); }