Пример #1
0
        static void RunIteratorPattern()
        {
            var root = new Node <int>(1, new Node <int>(2), new Node <int>(3));

            var it = new InOrderIterator <int>(root);

            while (it.MoveNext())
            {
                Console.WriteLine(it.Current.Value);
            }
            Console.WriteLine();


            var tree = new BinaryTree <int>(root);

            Console.WriteLine(string.Join(',', tree.InOrder.Select(x => x.Value)));

            Console.WriteLine();


            //this foreach loop works because of duck typing
            foreach (var node in tree)
            {
                Console.WriteLine(node.Value);
            }
            Console.WriteLine();
        }
        public static void Run()
        {
            //   1
            //  / \
            // 2   3

            // in-order: 213
            // preorder: 123

            var root = new Node <int>(1, new Node <int>(2), new Node <int>(3));

            var it = new InOrderIterator <int>(root);

            while (it.MoveNext())
            {
                Console.WriteLine(it.Current.Value);
            }

            var tree = new BinaryTree <int>(root);

            Console.WriteLine(string.Join(",", tree.InOrder.Select(x => x.Value)));

            foreach (var node in tree)
            {
                Console.WriteLine(node.Value);
            }
        }
Пример #3
0
        // an iterator that is a separate object
        public static void IteratorDemo()
        {
            //   1
            //  / \
            // 2   3

            // in-order:  213
            // preorder:  123
            // postorder: 231

            var root = new Node <int>(1, new Node <int>(2), new Node <int>(3));

            // C++ style
            var it = new InOrderIterator <int>(root);

            while (it.MoveNext())
            {
                Write(it.Current.Value);
                Write(',');
            }
            WriteLine();

            // C# style
            var tree = new BinaryTree <int>(root);

            WriteLine(string.Join(",", tree.NaturalInOrder.Select(x => x.Value)));

            foreach (var node in tree)
            {
                WriteLine(node.Value);
            }
        }
Пример #4
0
        public void MoveNext_MoveOnce_GetCorrectValue()
        {
            var root = new Node <int>(1,
                                      new Node <int>(2), new Node <int>(3));

            // C++ style
            var it = new InOrderIterator <int>(root);

            it.MoveNext();

            Assert.That(it.Current.Value, Is.EqualTo(2));
        }