예제 #1
0
        public void IterativeTestWithEmptyTree()
        {
            BinaryTreeLevelOrderTraversal <int> .TreeNode root = null;
            IList <IList <int> > levelorder = BinaryTreeLevelOrderTraversal <int> .IterativeLevelOrder(root);

            Assert.Equal(0, levelorder.Count);
        }
예제 #2
0
        public void IterativeTestWithThreeLevels()
        {
            // Arrange
            BinaryTreeLevelOrderTraversal <int> .TreeNode root = new BinaryTreeLevelOrderTraversal <int> .TreeNode(3);

            root.left = new BinaryTreeLevelOrderTraversal <int> .TreeNode(5);

            root.right = new BinaryTreeLevelOrderTraversal <int> .TreeNode(9);

            root.right.left = new BinaryTreeLevelOrderTraversal <int> .TreeNode(17);

            root.right.right = new BinaryTreeLevelOrderTraversal <int> .TreeNode(20);

            IList <IList <int> > levelorder = BinaryTreeLevelOrderTraversal <int> .IterativeLevelOrder(root);

            IList <IList <int> > expectList = new List <IList <int> >();

            for (int i = 0; i < 3; i++)
            {
                expectList.Add(new List <int>());
            }
            expectList[0].Add(3);
            expectList[1].Add(5);
            expectList[1].Add(9);
            expectList[2].Add(17);
            expectList[2].Add(20);

            // Assert
            int index = 0;

            Console.WriteLine("Iterative Level Order: ");
            foreach (IList <int> level in levelorder)
            {
                for (int i = 0; i < level.Count; i++)
                {
                    int val = level[i];
                    Assert.Equal(val, expectList[index][i]);
                    Console.Write(val + " ");
                }
                index++;
                Console.WriteLine();
            }
        }