public void GetEnumerator_CopyTreeToListWithForEach_TreeAndListAreSimilarByValues(int i1, int i2, int i3, int i4, int i5, int i6) { //Arrange MyBinaryTree <int> tree = new MyBinaryTree <int>(); List <int> l = new List <int>() { i1, i2, i3, i4, i5, i6 }; foreach (var item in l) { tree.Insert(item); } l.Sort(); List <int> res = new List <int>(); //Act foreach (var item in tree) { res.Add(item); } //Assert Assert.Equal(l, res); }
public static bool HasPathSum(MyBinaryTree root, int targetSum) { return(root != null && HasPathSumRecursively(root, 0, targetSum)); bool HasPathSumRecursively(MyBinaryTree root, int current, int target) { var complete = false; current += root.Value; if (root.Left == null && root.Right == null && current == target) { return(true); } if (root.Left != null) { complete = HasPathSumRecursively(root.Left, current, target); } if (root.Right != null) { complete = complete || HasPathSumRecursively(root.Right, current, target); } return(complete); } }
private static void TestTree() { var myBinaryTree = new MyBinaryTree <int, string>(); myBinaryTree.Add(47, "Math"); myBinaryTree.Add(40, "Physics"); myBinaryTree.Add(22, "Biology"); myBinaryTree.Add(61, "English"); myBinaryTree.Add(95, "Geography"); myBinaryTree.Add(9, "Science"); myBinaryTree.Add(93, "Social Studies"); myBinaryTree.Add(39, "Literature"); myBinaryTree.Add(43, "Statistics"); myBinaryTree.Add(38, "Chemistry"); myBinaryTree.Add(59, "Economics"); myBinaryTree.Add(25, "History"); myBinaryTree.Add(60, "Law"); myBinaryTree.Add(71, "French"); myBinaryTree.Add(32, "Psychology"); myBinaryTree.Remove(47); myBinaryTree.Remove(22); myBinaryTree.Remove(61); Console.WriteLine(myBinaryTree.Get(38)); Console.WriteLine(myBinaryTree.Get(60)); Console.WriteLine(myBinaryTree.Get(32)); Console.WriteLine(); Console.WriteLine($"Maximum value is {myBinaryTree.GetMax()}"); }
public MainForm() { InitializeComponent(); _binaryTree = new MyBinaryTree <int>(); _binaryTree.Add(50); _binaryTree.Add(25); _binaryTree.Add(70); _binaryTree.Add(15); _binaryTree.Add(44); _binaryTree.Add(85); _binaryTree.Add(71); _binaryTree.Add(2); _binaryTree.Add(60); _binaryTree.Add(30); _binaryTree.Add(45); _binaryTree.Add(18); _binaryTree.Add(4); _binaryTree.Add(16); _binaryTree.Add(99); _binaryTree.Add(86); _binaryTree.Add(100); _binaryTree.Add(55); _binaryTree.Add(65); Invalidate(); }
public void LevelOrderTraversalTestCase2() { var tree = new MyBinaryTree(1); tree.Left = new MyBinaryTree(2); tree.Left.Left = new MyBinaryTree(3); tree.Left.Left.Left = new MyBinaryTree(4); tree.Left.Left.Left.Left = new MyBinaryTree(5); var result = BinaryTreeLevelOrderTraversal.LevelOrderTraversal(tree); result[0].Should().ContainInOrder(new List <int> { 1 }); result[1].Should().ContainInOrder(new List <int> { 2 }); result[2].Should().ContainInOrder(new List <int> { 3 }); result[3].Should().ContainInOrder(new List <int> { 4 }); result[4].Should().ContainInOrder(new List <int> { 5 }); }
public void LevelOrderTraversalTestCase1() { var tree = new MyBinaryTree(6); tree.Left = new MyBinaryTree(2); tree.Left.Left = new MyBinaryTree(1); tree.Left.Right = new MyBinaryTree(4); tree.Left.Right.Left = new MyBinaryTree(3); tree.Left.Right.Right = new MyBinaryTree(5); tree.Right = new MyBinaryTree(7); tree.Right.Right = new MyBinaryTree(9); tree.Right.Right.Left = new MyBinaryTree(8); var result = BinaryTreeLevelOrderTraversal.LevelOrderTraversal(tree); result[0].Should().ContainInOrder(new List <int> { 6 }); result[1].Should().ContainInOrder(new List <int> { 2, 7 }); result[2].Should().ContainInOrder(new List <int> { 1, 4, 9 }); result[3].Should().ContainInOrder(new List <int> { 3, 5, 8 }); }
private static IEnumerable <IList <MyBinaryTree> > GetSubTree(MyBinaryTree root) { var result = new List <List <MyBinaryTree> >(); var level = GetLevel(new[] { root }); while (level.Any(n => n != null)) { result.Add(level.ToList()); level = GetLevel(level); } return(result); List <MyBinaryTree> GetLevel(IEnumerable <MyBinaryTree> parents) { var level = new List <MyBinaryTree>(); foreach (var node in parents) { level.Add(node?.Left); level.Add(node?.Right); } return(level); } }
private static bool IsSymmetricIteratively(MyBinaryTree root) { if (root == null) { return(false); } var left = root.Left; var right = root.Right; if (left?.Value != right?.Value) { return(false); } var leftSubtree = GetSubTree(left).ToArray(); var rightSubtree = GetSubTree(right).ToArray(); for (int i = 0; i < leftSubtree.Length; i++) { var leftNodes = leftSubtree[i]; var rightNodes = rightSubtree[i].Reverse().ToList(); for (int j = 0; j < leftNodes.Count; j++) { if (leftNodes[j]?.Value != rightNodes[j]?.Value) { return(false); } } } return(true); }
public void CanFizzBuzzTreeWithNonInts() { Node <object> node1 = new Node <object>("nope"); Node <object> node2 = new Node <object>(10); Node <object> node3 = new Node <object>(15); Node <object> node4 = new Node <object>(2); Node <object> node5 = new Node <object>(3); Node <object> node6 = new Node <object>(21); Node <object> node7 = new Node <object>(7); MyBinaryTree <object> tree = new MyBinaryTree <object>(); MyBinaryTree <object> resultsTree = new MyBinaryTree <object>(); List <object> list = new List <object>(); tree.Root = node1; tree.Root.LChild = node2; tree.Root.RChild = node3; tree.Root.LChild.LChild = node4; tree.Root.LChild.RChild = node5; tree.Root.RChild.LChild = node6; tree.Root.RChild.RChild = node7; resultsTree = FizzBuzz(tree); List <object> expectedList = new List <object>() { "nope", "Buzz", 2, "Fizz", "FizzBuzz", "Fizz", 7 }; List <object> actualList = resultsTree.PreOrder(resultsTree.Root, list); Assert.Equal(expectedList, actualList); }
public void PathSumTestCase3() { var tree = new MyBinaryTree(1); tree.Left = new MyBinaryTree(2); PathSum.HasPathSum(tree, 1).Should().Be(false); }
public void MaximumDepthOfBinaryTreeTestCase3() { var tree = new MyBinaryTree(1); tree.Right = new MyBinaryTree(2); MaximumDepthOfBinaryTree.MaxDepth(tree).Should().Be(2); }
/// <summary> /// Takes in two Binary 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(MyBinaryTree <int> treeOne, MyBinaryTree <int> 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 void InorderTraversalSingleNodeTest() { var tree = new MyBinaryTree(1); BinaryTreeInorderTraversal.InorderTraversal(tree).Should().ContainInOrder(new List <int> { 1 }); }
public void CanCreateTreeWithOneRoot() { Node <string> node = new Node <string>("root"); MyBinaryTree <string> tree = new MyBinaryTree <string>(); tree.Root = node; Assert.Equal("root", tree.Root.Value); }
public MyBinaryTree <int> CreateDummySortedBinaryTree() { var binaryTree = new MyBinaryTree <int>(); binaryTree.Root = new MyBinaryTreeNode <int>(4); binaryTree.Root.AddChildNodes(2, 6); binaryTree.Root.Left.AddChildNodes(1, 3); binaryTree.Root.Right.AddChildNodes(5, 7); return(binaryTree); }
public void Find_TryFindValue_IfTreeIsEmtyReturnNull() { //Arrange MyBinaryTree <int> tree = new MyBinaryTree <int>(); //Act var res = tree.Find(5); //Assert Assert.Null(res); }
public void MyBinaryTree_CreateInstance_InstanceIsNotNull() { //Arrange MyBinaryTree <string> tree = null; //Act tree = new MyBinaryTree <string>(); //Assert Assert.NotNull(tree); }
public static MyBinaryTree <object> FizzBuzz(MyBinaryTree <object> tree) { if (tree.Root != null) { PreOrderMod(tree.Root); return(tree); } return(null); }
public void CanAddRightChild() { Node <string> node1 = new Node <string>("root"); Node <string> node2 = new Node <string>("right"); MyBinaryTree <string> tree = new MyBinaryTree <string>(); tree.Root = node1; tree.Root.RChild = node2; Assert.Equal("right", tree.Root.RChild.Value); }
public void SymmetricTreeTestCase4() { var tree = new MyBinaryTree(1); tree.Left = new MyBinaryTree(2); tree.Left.Right = new MyBinaryTree(3); tree.Right = new MyBinaryTree(2); tree.Right.Left = new MyBinaryTree(3); SymmetricTree.IsSymmetric(tree).Should().Be(true); }
public void Remove_RemoveRootValue_IfTreeContainsOnlyRootItChangesToNull() { //Arrange MyBinaryTree <int> tree = new MyBinaryTree <int>(); tree.Insert(5); //Act tree.Remove(5); //Assert Assert.Null(tree.Root); }
public void Given3Numbers_WhenAddedToBinaryTree_ShouldReturnSizeThree() { MyBinaryTree <int> myBinaryTree = new MyBinaryTree <int>(); myBinaryTree.add(56); myBinaryTree.add(30); myBinaryTree.add(70); int size = myBinaryTree.getSize(); Assert.AreEqual(3, size); }
public void MaximumDepthOfBinaryTreeTestCase6() { var tree = new MyBinaryTree(1); tree.Left = new MyBinaryTree(2); tree.Left.Left = new MyBinaryTree(3); tree.Left.Left.Left = new MyBinaryTree(4); tree.Left.Left.Left.Left = new MyBinaryTree(5); MaximumDepthOfBinaryTree.MaxDepth(tree).Should().Be(5); }
public void MaximumDepthOfBinaryTreeTestCase2() { var tree = new MyBinaryTree(3); tree.Left = new MyBinaryTree(9); tree.Right = new MyBinaryTree(20); tree.Right.Left = new MyBinaryTree(15); tree.Right.Right = new MyBinaryTree(7); MaximumDepthOfBinaryTree.MaxDepth(tree).Should().Be(3); }
public void PostorderTraversalTestCase2() { var tree = new MyBinaryTree(1); tree.Right = new MyBinaryTree(2); tree.Right.Left = new MyBinaryTree(3); BinaryTreePostorderTraversal.PostorderTraversal(tree).Should().ContainInOrder(new List <int> { 3, 2, 1 }); }
// nhập từ FILE public static void FileToBST(MyBinaryTree <int> root) { using (var sr = new StreamReader("BST.txt")) { var n = int.Parse(sr.ReadLine()); for (int i = 0; i < n; i++) { var key = int.Parse(sr.ReadLine()); root.Insert(key); } } }
public void Remove_RemoveRootValue_IfTreeIsEmptyDontThrowException() { //Arrange MyBinaryTree <int> tree = new MyBinaryTree <int>(); //Act tree.Remove(5); //Assert var res = tree.Find(5); Assert.Null(res); }
public void InorderTraversalTestCase2() { var tree = new MyBinaryTree(2); tree.Left = new MyBinaryTree(3); tree.Left.Left = new MyBinaryTree(1); BinaryTreeInorderTraversal.InorderTraversal(tree).Should().ContainInOrder(new List <int> { 1, 3, 2 }); }
public void Insert_InsertValueToRoot_TreeContainsValue() { //Arrange MyBinaryTree <int> tree = new MyBinaryTree <int>(); //Act tree.Insert(5); //Assert var res = tree.Find(5); Assert.NotNull(res); }
public void PreorderTraversalTestCase2() { var tree = new MyBinaryTree(1); tree.Left = new MyBinaryTree(4); tree.Left.Left = new MyBinaryTree(2); tree.Right = new MyBinaryTree(3); BinaryTreePreorderTraversal.PreorderTraversal(tree).Should().ContainInOrder(new List <int> { 1, 4, 2, 3 }); }