예제 #1
0
        public void FindsNoIntersectingValuesWhenBothTreesAreNull()
        {
            //Arrange
            Tree <string> testTreeOne = new Tree <string>();

            Tree <string> testTreeTwo = new Tree <string>();

            string[] expected = new string[0];

            //Act
            HashSet <string> result = TreeIntersection <string> .GetMatchingValuesFor(testTreeOne, testTreeTwo);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(expected.Length, result.Count);
            bool matching = true;

            foreach (string oneString in expected)
            {
                if (!result.Contains(oneString))
                {
                    matching = false;
                    break;
                }
            }
            Assert.True(matching);
        }
예제 #2
0
        public void Successfully_Throw_Null_Arguement_Exception_If_Tree_Is_Empty()
        {
            BinaryTree <int> testTree = MakeBinaryTestTree(
                new int[] { 150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500 });
            BinaryTree <int> testTree2 = new BinaryTree <int>();

            Assert.Throws <ArgumentNullException>(() => TreeIntersection.Intersection(testTree, testTree2));
        }
예제 #3
0
        public void HashTableTree1_Method__Successfully_Populates_A_HashMap()
        {
            BinaryTree <int> testTree = MakeBinaryTestTree(
                new int[] { 150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500 });
            HashMap <int> testHashMap = TreeIntersection.HashTableTree1(testTree.Root,
                                                                        new HashMap <int>(1024));
            int actual   = testHashMap.counter;
            int expected = 11;

            Assert.Equal(expected, actual);
        }
예제 #4
0
        public void Successfully_Find_Intersecting_Values_Between_Two_Trees()
        {
            BinaryTree <int> testTree1 = MakeBinaryTestTree(
                new int[] { 150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500 });
            BinaryTree <int> testTree2 = MakeBinaryTestTree(
                new int[] { 42, 100, 600, 15, 160, 200, 350, 125, 175, 4, 500 });
            List <int> testList = TreeIntersection.Intersection(testTree1, testTree2);

            int[] actual   = testList.ToArray();
            int[] expected = new int[] { 100, 160, 125, 175, 200, 350, 500 };
            Assert.Equal(expected, actual);
        }
예제 #5
0
        public void FindsNoIntersectingValuesWhenOneTreeIsNull()
        {
            //Arrange
            string        rootStringOne = "A";
            Tree <string> testTreeOne   = new Tree <string>(rootStringOne);

            string        rootLeftChildStringOne = "B";
            Node <string> rootLeftChildOne       = new Node <string>(rootLeftChildStringOne);
            string        BLeftChildStringOne    = "C";

            rootLeftChildOne.LeftChild = new Node <string>(BLeftChildStringOne);
            string BRightChildStringOne = "D";

            rootLeftChildOne.RightChild = new Node <string>(BRightChildStringOne);

            testTreeOne.Root.LeftChild = rootLeftChildOne;

            string        rootRightChildStringOne = "E";
            Node <string> rootRightChildOne       = new Node <string>(rootRightChildStringOne);
            string        ELeftChildStringOne     = "F";

            rootRightChildOne.LeftChild = new Node <string>(ELeftChildStringOne);
            string ERightChildStringOne = "G";

            rootRightChildOne.RightChild = new Node <string>(ERightChildStringOne);

            testTreeOne.Root.RightChild = rootRightChildOne;


            Tree <string> testTreeTwo = new Tree <string>();


            string[] expected = new string[0];


            //Act
            HashSet <string> result = TreeIntersection <string> .GetMatchingValuesFor(testTreeOne, testTreeTwo);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(expected.Length, result.Count);
            bool matching = true;

            foreach (string oneString in expected)
            {
                if (!result.Contains(oneString))
                {
                    matching = false;
                    break;
                }
            }
            Assert.True(matching);
        }
예제 #6
0
        public void CompareTree2_Can_Successfully_Return_A_List_Of_Collisions()
        {
            BinaryTree <int> testTree = MakeBinaryTestTree(
                new int[] { 150, 100, 250, 75, 160, 200, 350, 125, 175, 300, 500 });
            HashMap <int> testHashMap = new HashMap <int>(1024);

            testHashMap.Set(160, 160);
            testHashMap.Set(150, 150);
            List <int> testList = TreeIntersection.CompareTree2(
                testTree.Root, testHashMap, new List <int>());

            int[] actual   = testList.ToArray();
            int[] expected = new int[] { 150, 160 };
            Assert.Equal(expected, actual);
        }
        public void TreeIntersect_returns_duplicate_challenge()
        {
            // Arrange
            BinaryTree <int> tree1 = new BinaryTree <int>();
            BinaryTree <int> tree2 = new BinaryTree <int>();

            tree1.Root                   = new Node <int>(150);
            tree1.Root.Left              = new Node <int>(100);
            tree1.Root.Left.Right        = new Node <int>(160);
            tree1.Root.Left.Left         = new Node <int>(125);
            tree1.Root.Left.Right.Right  = new Node <int>(175);
            tree1.Root.Left.Left         = new Node <int>(75);
            tree1.Root.Right             = new Node <int>(250);
            tree1.Root.Right.Left        = new Node <int>(200);
            tree1.Root.Right.Right       = new Node <int>(350);
            tree1.Root.Right.Right.Right = new Node <int>(500);
            tree1.Root.Right.Right.Left  = new Node <int>(300);

            tree2.Root                   = new Node <int>(42);
            tree2.Root.Left              = new Node <int>(100);
            tree2.Root.Left.Right        = new Node <int>(160);
            tree2.Root.Left.Left         = new Node <int>(125);
            tree2.Root.Left.Right.Right  = new Node <int>(175);
            tree2.Root.Left.Left         = new Node <int>(15);
            tree2.Root.Right             = new Node <int>(600);
            tree2.Root.Right.Left        = new Node <int>(200);
            tree2.Root.Right.Right       = new Node <int>(350);
            tree2.Root.Right.Right.Right = new Node <int>(500);
            tree2.Root.Right.Right.Left  = new Node <int>(4);

            // Act
            List <int> value = TreeIntersection <int> .TreeIntersect(tree1, tree2);

            // Assert
            List <int> expected = new List <int>();

            expected.Add(100);
            expected.Add(160);
            expected.Add(200);
            expected.Add(350);
            expected.Add(175);
            expected.Add(500);

            Assert.Equal(expected, value);
        }
        public void TreeIntersect_returns_duplicate()
        {
            // Arrange
            BinaryTree <int> tree1 = new BinaryTree <int>();
            BinaryTree <int> tree2 = new BinaryTree <int>();

            tree1.Root       = new Node <int>(1);
            tree2.Root       = new Node <int>(1);
            tree1.Root.Left  = new Node <int>(2);
            tree2.Root.Left  = new Node <int>(2);
            tree1.Root.Right = new Node <int>(3);
            tree2.Root.Right = new Node <int>(4);

            // Act
            List <int> value = TreeIntersection <int> .TreeIntersect(tree1, tree2);

            // Assert
            List <int> expected = new List <int>();

            expected.Add(1);
            expected.Add(2);

            Assert.Equal(expected, value);
        }
예제 #9
0
        public void ForTwoTwoTreesWithIdenticalValuesInEveryNodeReturnsOneValue()
        {
            //Arrange
            string        rootStringOne = "G";
            Tree <string> testTreeOne   = new Tree <string>(rootStringOne);

            string        rootLeftChildStringOne = "G";
            Node <string> rootLeftChildOne       = new Node <string>(rootLeftChildStringOne);
            string        BLeftChildStringOne    = "G";

            rootLeftChildOne.LeftChild = new Node <string>(BLeftChildStringOne);
            string BRightChildStringOne = "G";

            rootLeftChildOne.RightChild = new Node <string>(BRightChildStringOne);

            testTreeOne.Root.LeftChild = rootLeftChildOne;

            string        rootRightChildStringOne = "G";
            Node <string> rootRightChildOne       = new Node <string>(rootRightChildStringOne);
            string        ELeftChildStringOne     = "G";

            rootRightChildOne.LeftChild = new Node <string>(ELeftChildStringOne);
            string ERightChildStringOne = "G";

            rootRightChildOne.RightChild = new Node <string>(ERightChildStringOne);

            testTreeOne.Root.RightChild = rootRightChildOne;


            string        rootStringTwo = "G";
            Tree <string> testTreeTwo   = new Tree <string>(rootStringTwo);

            string        rootLeftChildStringTwo = "G";
            Node <string> rootLeftChildTwo       = new Node <string>(rootLeftChildStringTwo);
            string        BLeftChildStringTwo    = "G";

            rootLeftChildTwo.LeftChild = new Node <string>(BLeftChildStringTwo);
            string BRightChildStringTwo = "G";

            rootLeftChildTwo.RightChild = new Node <string>(BRightChildStringTwo);

            testTreeTwo.Root.LeftChild = rootLeftChildTwo;

            string        rootRightChildStringTwo = "G";
            Node <string> rootRightChildTwo       = new Node <string>(rootRightChildStringTwo);
            string        ELeftChildStringTwo     = "G";

            rootRightChildTwo.LeftChild = new Node <string>(ELeftChildStringTwo);
            string ERightChildStringTwo = "G";

            rootRightChildTwo.RightChild = new Node <string>(ERightChildStringTwo);

            testTreeTwo.Root.RightChild = rootRightChildTwo;

            string[] expected = new string[] { "G" };

            //Act
            HashSet <string> result = TreeIntersection <string> .GetMatchingValuesFor(testTreeOne, testTreeTwo);

            //Assert
            Assert.NotNull(result);
            Assert.Equal(expected.Length, result.Count);
            bool matching = true;

            foreach (string oneString in expected)
            {
                if (!result.Contains(oneString))
                {
                    matching = false;
                    break;
                }
            }
            Assert.True(matching);
        }