public void ShouldReturnNodeIfValueExistUsingNonRecursion()
        {
            var rootNode = TreeTestData.GetSimpleTreeRootNode();

            // finding node which is available in left side of the tree
            IAlgorithm <TreeDSNode, TreeDSNode> searchNodeByValue = new SearchNodesByValue(4);
            var actual = searchNodeByValue.UsingNonRecursion(rootNode);

            Assert.AreEqual(actual.Value, 4);

            // finding node which is available in right side of the tree
            ((SearchNodesByValue)searchNodeByValue).valueToFind = 6;
            var actualNodeFor6 = searchNodeByValue.UsingNonRecursion(rootNode);

            Assert.AreEqual(actualNodeFor6.Value, 6);

            // finding node which is available in root of the tree
            ((SearchNodesByValue)searchNodeByValue).valueToFind = 1;
            var actualRootNode = searchNodeByValue.UsingNonRecursion(rootNode);

            Assert.AreEqual(actualRootNode.Value, 1);

            // finding node which is not available in the tree
            ((SearchNodesByValue)searchNodeByValue).valueToFind = 99;
            var nullValueForNotFound = searchNodeByValue.UsingNonRecursion(rootNode);

            Assert.AreEqual(nullValueForNotFound, null);
        }
Exemplo n.º 2
0
        public void ShouldReturnSizeUsingNonRecursion()
        {
            TreeDSNode rootNode   = TreeTestData.GetSimpleTreeRootNode();
            var        treeSize   = new TreeSize();
            var        actualSize = treeSize.UsingNonRecursion(rootNode);

            Assert.AreEqual(6, actualSize);
        }
        public void ShouldPrintAllNodeFromRootToLeaf()
        {
            var expectedOutputs = new List <String>()
            {
                "Tree path: 1,2,4", "Tree path: 1,2,5", "Tree path: 1,3,6"
            };
            IConsoleWriter writerMock            = MockWriter.GetInstanceForString(expectedOutputs);
            var            rootNode              = TreeTestData.GetSimpleTreeRootNode();
            PrintingTreeNodesFromRootToLeaf tree = new PrintingTreeNodesFromRootToLeaf(writerMock);

            tree.Print(rootNode, new List <int>());
        }
        public void ShouldPrintTreeInLevelOrder()
        {
            var expectedOutputs = new List <int>()
            {
                6, 5, 4, 3, 2, 1
            };
            IConsoleWriter writerMock = MockWriter.GetInstance(expectedOutputs);
            var            rootNode   = TreeTestData.GetSimpleTreeRootNode();
            var            levelOrderTraversalInReverse = new LevelOrderTraversalInReverse(writerMock);

            levelOrderTraversalInReverse.Print(rootNode);
        }
Exemplo n.º 5
0
        public void ShouldPrintTreeNodesInZigZagTraversal()
        {
            var expectedOutputs = new List <int>()
            {
                1, 3, 2, 4, 5, 6
            };
            IConsoleWriter  writerMock      = MockWriter.GetInstance(expectedOutputs);
            TreeDSNode      rootNode        = TreeTestData.GetSimpleTreeRootNode();
            ZigZagTraversal zigzagTraversal = new ZigZagTraversal(rootNode, writerMock);

            zigzagTraversal.Travel();
        }
Exemplo n.º 6
0
        public void ShouldPrintTreeInLevelOrder()
        {
            var expectedOutputs = new List <int>()
            {
                1, 2, 3, 4, 5, 6
            };
            IConsoleWriter writerMock = MockWriter.GetInstance(expectedOutputs);
            TreeDSNode     rootNode   = TreeTestData.GetSimpleTreeRootNode();
            TreeTraversal  tree       = new TreeTraversal(rootNode, writerMock);

            tree.LevelOrderTraversal();
        }