示例#1
0
        static void Main(string[] args)
        {
            //   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)));

            // duck typing!
            foreach (var node in tree)
            {
                WriteLine(node.Value);
            }
        }
        static void Main(string[] args)
        {
            /*  pravimo drvo
             *      3
             *    /   \
             *   8     5
             */
            // hoćemo da ga iscitamo sa lijeva na desno 835

            var root = new Node <int>(3, new Node <int>(8), new Node <int>(5));

            //test za iterator
            var it = new InOrderIterator <int>(root);

            while (it.MoveNext())
            {
                Console.WriteLine(it.Current.Value);
            }
            //test za binarytree
            var bt = new BinaryTree <int>(root);

            foreach (var node in bt.InOrder)
            {
                Console.WriteLine(node.Value);
            }
            //ako nam treba samo jedan nacin za iteraciju mozemo ovako da pozovemo ako imamo:
            //klasu iterator koja u sebi ima property Current i bool metod MoveNext()
            //i u klasi BinaryTree metod GetEnumerator, da bi BinaryTree bio enumerable
            foreach (var node in bt)
            {
                Console.WriteLine(node.Value);
            }
            Console.ReadLine();
        }
示例#3
0
        private static void IteratorObjectExample()
        {
            var root = new Node <int>(1, new Node <int>(2), new Node <int>(3));
            var it   = new InOrderIterator <int>(root);

            while (it.MoveNext())
            {
                Console.Write($"{it.Current.Value},");
            }
            Console.WriteLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            //     1
            //    / \
            //   2   3
            var root = new Node <int>(1, new Node <int>(2), new Node <int>(3));
            var it   = new InOrderIterator <int>(root);

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

            Console.WriteLine();

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

            Console.WriteLine(string.Join(",", tree.InOrder.Select(x => x.Value)));
        }
        static void Main(string[] args)
        {
            //      1
            //    /   \
            //   2     3

            // in order: 213

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

            // low level approach
            var it = new InOrderIterator <int>(root);

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

            // high level approach
            var tree = new BinaryTree <int>(root);

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

            // iterators and Duck typing
            var newTree = new BinaryTreeNew <int>(root);

            foreach (var node in newTree)
            {
                Write("{0},", node.Value);
            }
            WriteLine();

            // exercise
            WriteLine(string.Join(",", root.PreOrder.Select(x => x.Value)));
        }