コード例 #1
0
        public static void Iterator()
        {
            var root =
                new Node
                    {
                        Value = "ROOT",

                        Left =
                            new Node
                                {
                                    Value = "LEFT"
                                },

                        Right =
                            new Node
                                {
                                    Value = "RIGHT"
                                }
                    };

            var tree = new BreadthFirstTree(root);

            var traversal = new StringBuilder();
            using (IEnumerator<Node> iterator = tree.GetEnumerator())
            {
                while (iterator.MoveNext())
                {
                    string currentValue = iterator.Current.Value;
                    traversal.Append(currentValue);
                    traversal.Append(";");
                }
            }
            Console.WriteLine(traversal.ToString());
        }
コード例 #2
0
        public void BreadthFirstTree_Iterator_Using_IEnumerator_Test()
        {
            const string treeStr = @"          (ROOT)          " +
                                   "          /    \\         " +
                                   "        (A)    (B)        " +
                                   "        / \\     /        " +
                                   "      (C) (D)  (E)        ";

            const string expected = "ROOT;B;A;E;D;C;";

            var rootNode = BinaryTreeParser.Parse(treeStr);
            var tree = new BreadthFirstTree(rootNode);

            var actual = new StringBuilder();
            using (var iterator = tree.GetEnumerator())
            {
                while (iterator.MoveNext())
                {
                    var currentValue = iterator.Current.Value;
                    actual.Append(currentValue);
                    actual.Append(";");
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }