static void Main(string[] args) { Console.WriteLine("Hello Breadth First Traversal"); Trees.Classes.Node root = new Trees.Classes.Node(2); Trees.Classes.Node nodeTwo = new Trees.Classes.Node(4); Trees.Classes.Node nodeThree = new Trees.Classes.Node(6); Trees.Classes.Node nodeFour = new Trees.Classes.Node(8); Trees.Classes.Node nodeFive = new Trees.Classes.Node(10); Trees.Classes.Node nodeSix = new Trees.Classes.Node(12); Trees.Classes.Node nodeSeven = new Trees.Classes.Node(14); Trees.Classes.Node nodeEight = new Trees.Classes.Node(16); Trees.Classes.Node nodeNine = new Trees.Classes.Node(18); Trees.Classes.Node nodeTen = new Trees.Classes.Node(21); BinaryTree breadthTree = new BinaryTree(root); breadthTree.Root.LeftChild = nodeTwo; breadthTree.Root.RightChild = nodeThree; breadthTree.Root.LeftChild.LeftChild = nodeFour; breadthTree.Root.LeftChild.RightChild = nodeFive; breadthTree.Root.RightChild.LeftChild = nodeSix; breadthTree.Root.RightChild.RightChild = nodeSeven; breadthTree.Root.LeftChild.RightChild.LeftChild = nodeEight; breadthTree.Root.LeftChild.RightChild.RightChild = nodeNine; breadthTree.Root.RightChild.RightChild.RightChild = nodeTen; BreadthFirst(root); Console.ReadLine(); }
public void TestNoIntersections() { // creating nodes Trees.Classes.Node one = new Trees.Classes.Node(1); Trees.Classes.Node seven = new Trees.Classes.Node(7); Trees.Classes.Node ten = new Trees.Classes.Node(10); Trees.Classes.Node two = new Trees.Classes.Node(2); one.Left = seven; seven.Right = ten; one.Right = two; BinaryTree bt1 = new BinaryTree(one); // create second tree Trees.Classes.Node four2 = new Trees.Classes.Node(4); Trees.Classes.Node eight2 = new Trees.Classes.Node(8); Trees.Classes.Node nine2 = new Trees.Classes.Node(9); four2.Left = eight2; four2.Right = nine2; BinaryTree bt2 = new BinaryTree(four2); Assert.True(TreeIntersection(bt1, bt2).Count == 0); }
static void Main(string[] args) { WriteLine("Creating BT1: 1, 7, 10, 2"); // creating nodes Trees.Classes.Node one = new Trees.Classes.Node(1); Trees.Classes.Node seven = new Trees.Classes.Node(7); Trees.Classes.Node ten = new Trees.Classes.Node(10); Trees.Classes.Node two = new Trees.Classes.Node(2); one.Left = seven; seven.Right = ten; one.Right = two; BinaryTree bt1 = new BinaryTree(one); // create second tree Trees.Classes.Node two2 = new Trees.Classes.Node(2); Trees.Classes.Node seven2 = new Trees.Classes.Node(7); Trees.Classes.Node nine2 = new Trees.Classes.Node(9); two2.Left = seven2; two2.Right = nine2; BinaryTree bt2 = new BinaryTree(two2); WriteLine("Running tree intersect, should return: 2, 7"); // loops through the returned list to show answer foreach (string returned in TreeIntersection(bt1, bt2)) { WriteLine(returned); } }
/// <summary> /// List is used for testing xUnit only. /// Takes a binary tree through breadthFirst approach. /// </summary> /// <param name="Root">Node root</param> /// <returns>values in breadthFirst order</returns> public static List <int> BreadthFirst(Trees.Classes.Node Root) { List <int> testList = new List <int>(); Queue <Trees.Classes.Node> breadthQueue = new Queue <Trees.Classes.Node>(); breadthQueue.Enqueue(Root); try { while (breadthQueue.Peek() != null) { //BinaryTree front = new BinaryTree(); Trees.Classes.Node front = breadthQueue.Dequeue(); Console.WriteLine($"{front.Value}"); testList.Add(front.Value); if (front.LeftChild != null) { breadthQueue.Enqueue(front.LeftChild); } if (front.RightChild != null) { breadthQueue.Enqueue(front.RightChild); } } return(testList); } catch (Exception error) { Console.WriteLine($"Now the Queue is empty: {error.Message}"); } return(testList); }
public void TreeSizesAreDifferentTest() { // Arrange Trees.Classes.Node n1 = new Trees.Classes.Node(10); Trees.Classes.Node n2 = new Trees.Classes.Node(20); Trees.Classes.Node n3 = new Trees.Classes.Node(30); Trees.Classes.Node n4 = new Trees.Classes.Node(40); Trees.Classes.Node n5 = new Trees.Classes.Node(50); Trees.Classes.Node n6 = new Trees.Classes.Node(60); BinaryTree b1 = new BinaryTree(n1); b1.Add(n1, n2); b1.Add(n1, n3); b1.Add(n1, n4); b1.Add(n1, n5); b1.Add(n1, n6); Trees.Classes.Node n1v2 = new Trees.Classes.Node(20); Trees.Classes.Node n2v2 = new Trees.Classes.Node(15); Trees.Classes.Node n3v2 = new Trees.Classes.Node(25); BinaryTree b2 = new BinaryTree(n1v2); b1.Add(n1v2, n2v2); b1.Add(n1v2, n3v2); // Act List <int> intersect = Program.TreeIntersection(n1, n1v2); // Assert Assert.Equal(20, intersect[0]); }
/// <summary> /// This method takes in two tree nodes and returns a list of the values that are the same. /// </summary> /// <param name="n1">The node for the first tree</param> /// <param name="n2">The node for the second tree</param> /// <returns>A list of same values</returns> public static List <int> TreeIntersection(Trees.Classes.Node n1, Trees.Classes.Node n2) { List <int> answers = new List <int>(); HashTable hashTable = new HashTable(); Queue <Trees.Classes.Node> queue = new Queue <Trees.Classes.Node>(); queue.Enqueue(n1); while (queue.TryPeek(out n1)) { Trees.Classes.Node current = queue.Dequeue(); try { hashTable.Add(current.Value.ToString(), 1); } catch (Exception) { answers.Add(current.Value); } finally { if (current.LeftChild != null) { queue.Enqueue(current.LeftChild); } if (current.RightChild != null) { queue.Enqueue(current.RightChild); } } } queue.Enqueue(n2); while (queue.TryPeek(out n2)) { Trees.Classes.Node current = queue.Dequeue(); try { hashTable.Add(current.Value.ToString(), 1); } catch (Exception) { answers.Add(current.Value); } finally { if (current.LeftChild != null) { queue.Enqueue(current.LeftChild); } if (current.RightChild != null) { queue.Enqueue(current.RightChild); } } } return(answers); }
public void AllTreeValuesTheSameButInDifferentLocationsTest() { // Arrange Trees.Classes.Node n1 = new Trees.Classes.Node(10); Trees.Classes.Node n2 = new Trees.Classes.Node(20); Trees.Classes.Node n3 = new Trees.Classes.Node(30); Trees.Classes.Node n4 = new Trees.Classes.Node(40); Trees.Classes.Node n5 = new Trees.Classes.Node(50); Trees.Classes.Node n6 = new Trees.Classes.Node(60); BinaryTree b1 = new BinaryTree(n1); b1.Add(n1, n2); b1.Add(n1, n3); b1.Add(n1, n4); b1.Add(n1, n5); b1.Add(n1, n6); Trees.Classes.Node n1v2 = new Trees.Classes.Node(20); Trees.Classes.Node n2v2 = new Trees.Classes.Node(30); Trees.Classes.Node n3v2 = new Trees.Classes.Node(10); Trees.Classes.Node n4v2 = new Trees.Classes.Node(60); Trees.Classes.Node n5v2 = new Trees.Classes.Node(40); Trees.Classes.Node n6v2 = new Trees.Classes.Node(50); BinaryTree b2 = new BinaryTree(n1v2); b1.Add(n1v2, n2v2); b1.Add(n1v2, n3v2); b1.Add(n1v2, n4v2); b1.Add(n1v2, n5v2); b1.Add(n1v2, n6v2); // Act List <int> intersect = Program.TreeIntersection(n1, n1v2); List <int> expectedList = new List <int> { 20, 30, 10, 60, 40, 50 }; // Assert Assert.Equal(expectedList, intersect); }
static void Main(string[] args) { Trees.Classes.Node n1 = new Trees.Classes.Node(10); Trees.Classes.Node n2 = new Trees.Classes.Node(20); Trees.Classes.Node n3 = new Trees.Classes.Node(30); Trees.Classes.Node n4 = new Trees.Classes.Node(40); Trees.Classes.Node n5 = new Trees.Classes.Node(50); Trees.Classes.Node n6 = new Trees.Classes.Node(60); BinaryTree b1 = new BinaryTree(n1); b1.Add(n1, n2); b1.Add(n1, n3); b1.Add(n1, n4); b1.Add(n1, n5); b1.Add(n1, n6); Trees.Classes.Node n1v2 = new Trees.Classes.Node(15); Trees.Classes.Node n2v2 = new Trees.Classes.Node(20); Trees.Classes.Node n3v2 = new Trees.Classes.Node(25); Trees.Classes.Node n4v2 = new Trees.Classes.Node(30); Trees.Classes.Node n5v2 = new Trees.Classes.Node(35); Trees.Classes.Node n6v2 = new Trees.Classes.Node(40); BinaryTree b2 = new BinaryTree(n1v2); b1.Add(n1v2, n2v2); b1.Add(n1v2, n3v2); b1.Add(n1v2, n4v2); b1.Add(n1v2, n5v2); b1.Add(n1v2, n6v2); List <int> intersect = TreeIntersection(n1, n1v2); foreach (int value in intersect) { Console.WriteLine($"An intersection is: {value}"); } }
public void NoTreeValuesAreTheSameTest() { // Arrange Trees.Classes.Node n1 = new Trees.Classes.Node(10); Trees.Classes.Node n2 = new Trees.Classes.Node(20); Trees.Classes.Node n3 = new Trees.Classes.Node(30); Trees.Classes.Node n4 = new Trees.Classes.Node(40); Trees.Classes.Node n5 = new Trees.Classes.Node(50); Trees.Classes.Node n6 = new Trees.Classes.Node(60); BinaryTree b1 = new BinaryTree(n1); b1.Add(n1, n2); b1.Add(n1, n3); b1.Add(n1, n4); b1.Add(n1, n5); b1.Add(n1, n6); Trees.Classes.Node n1v2 = new Trees.Classes.Node(1); Trees.Classes.Node n2v2 = new Trees.Classes.Node(2); Trees.Classes.Node n3v2 = new Trees.Classes.Node(3); Trees.Classes.Node n4v2 = new Trees.Classes.Node(4); Trees.Classes.Node n5v2 = new Trees.Classes.Node(5); Trees.Classes.Node n6v2 = new Trees.Classes.Node(6); BinaryTree b2 = new BinaryTree(n1v2); b1.Add(n1v2, n2v2); b1.Add(n1v2, n3v2); b1.Add(n1v2, n4v2); b1.Add(n1v2, n5v2); b1.Add(n1v2, n6v2); // Act List <int> intersect = Program.TreeIntersection(n1, n1v2); // Assert Assert.Empty(intersect); }