當前位置: 首頁>>代碼示例>>C#>>正文


C# BinaryTree.GetPreOrderEnumerator方法代碼示例

本文整理匯總了C#中BinaryTree.GetPreOrderEnumerator方法的典型用法代碼示例。如果您正苦於以下問題:C# BinaryTree.GetPreOrderEnumerator方法的具體用法?C# BinaryTree.GetPreOrderEnumerator怎麽用?C# BinaryTree.GetPreOrderEnumerator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BinaryTree的用法示例。


在下文中一共展示了BinaryTree.GetPreOrderEnumerator方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: MainTree2

        static void MainTree2()
        {
            var tree = new BinaryTree<int> {Root = new Node<int> {Value = 1}};
            var node2 = tree.Root.Right = new Node<int>(2);
            var node3 = node2.Right = new Node<int>(3);
            var node4 = node3.Right = new Node<int>(4);
            var node5 = node4.Right = new Node<int>(5);
            var node6 = node5.Right = new Node<int>(6);
            var node7 = node6.Right = new Node<int>(7);
            var node8 = node7.Right = new Node<int>(8);

            var tree2 = new BinaryTree<int> {Root = new Node<int> {Value = 1}};
            var node2b = tree2.Root.Left = new Node<int>(2);
            var node3b = node2b.Left = new Node<int>(3);
            var node4b = node3b.Left = new Node<int>(4);
            var node5b = node4b.Left = new Node<int>(5);
            var node6b = node5b.Left = new Node<int>(6);
            var node7b = node6b.Left = new Node<int>(7);
            var node8b = node7b.Left = new Node<int>(8);

            // 1
            //  \
            //   2
            //    \
            //    ...
            //      \
            //       8

            // Depth PreOrder: 1 2 3 4 5 6 7 8
            Console.WriteLine("PreOrder: ");
            var preOrder = tree.GetPreOrderEnumerator();
            while (preOrder.MoveNext())
                Console.Write(" {0} ", preOrder.Current.Value);
            Console.WriteLine();
            var preOrder2 = tree2.GetPreOrderEnumerator();
            while (preOrder2.MoveNext())
                Console.Write(" {0} ", preOrder2.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth InOrder: 1 2 3 4 5 6 7 8
            Console.WriteLine("InOrder: ");
            var inOrder = tree.GetInOrderEnumerator();
            while (inOrder.MoveNext())
                Console.Write(" {0} ", inOrder.Current.Value);
            Console.WriteLine();
            var inOrder2 = tree2.GetInOrderEnumerator();
            while (inOrder2.MoveNext())
                Console.Write(" {0} ", inOrder2.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth LevelOrder: 1 2 3 4 5 6 7 8
            Console.WriteLine("LevelOrder: ");
            var levelOrder = tree.GetLevelOrderEnumerator();
            while (levelOrder.MoveNext())
                Console.Write(" {0} ", levelOrder.Current.Value);
            Console.WriteLine();
            var levelOrder2 = tree2.GetLevelOrderEnumerator();
            while (levelOrder2.MoveNext())
                Console.Write(" {0} ", levelOrder2.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("String representation: ");
            Console.WriteLine(BinaryTreeStringSerializer.AsString(tree));
            Console.WriteLine(BinaryTreeStringSerializer.AsStringCompact(tree));
            Console.WriteLine();
            Console.WriteLine(BinaryTreeStringSerializer.AsString(tree2));
            Console.WriteLine(BinaryTreeStringSerializer.AsStringCompact(tree2));

            Console.ReadLine();
        }
開發者ID:saguiitay,項目名稱:BackToBasics,代碼行數:76,代碼來源:Program.cs

示例2: MainTree

        static void MainTree()
        {
            var tree = new BinaryTree<int>();
            tree.Root = new Node<int> { Value = 1};

            var node2 = tree.Root.Left = new Node<int>(2);
            var node3 = tree.Root.Right = new Node<int>(3);

            var node4 = node2.Left = new Node<int>(4);
            var node5 = node2.Right = new Node<int>(5);

            var node6 = node4.Right = new Node<int>(6);
            var node7 = node4.Left = new Node<int>(7);

            var node0 = node3.Right = new Node<int>(0);
            var node8 = node0.Left = new Node<int>(8);

            //        1
            //      /  \
            //     2     3
            //    / \     \
            //   4   5     0
            //  / \       /
            // 7   6     8

            for (int i = 0; i < 12; i++)
                Console.WriteLine("Contains {0}: {1}", i, tree.FindNode(i) != null);

            Console.WriteLine();

            // Depth PreOrder: 1 2 4 7 6 5 3 0 8
            Console.WriteLine("PreOrder: ");
            var preOrder = tree.GetPreOrderEnumerator();
            while (preOrder.MoveNext())
                Console.Write(" {0} ", preOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth InOrder: 7 4 6 2 5 1 3 8 0
            Console.WriteLine("InOrder: ");
            var inOrder = tree.GetInOrderEnumerator();
            while (inOrder.MoveNext())
                Console.Write(" {0} ", inOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            // Depth LevelOrder: 1 2 3 4 5 0 7 6 8
            Console.WriteLine("LevelOrder: ");
            var levelOrder = tree.GetLevelOrderEnumerator();
            while (levelOrder.MoveNext())
                Console.Write(" {0} ", levelOrder.Current.Value);

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("String representation: ");
            var str = BinaryTreeStringSerializer.AsString(tree);
            Console.WriteLine(str);

            var loadedTree = BinaryTreeStringSerializer.FromString(str);
            Console.WriteLine(BinaryTreeStringSerializer.AsString(loadedTree));

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("String representation Compact: ");
            var strCompact = BinaryTreeStringSerializer.AsStringCompact(tree);
            Console.WriteLine(strCompact);

            var loadedTreeCompact = BinaryTreeStringSerializer.FromStringCompact(strCompact);
            Console.WriteLine(BinaryTreeStringSerializer.AsStringCompact(loadedTreeCompact));
            Console.WriteLine(BinaryTreeStringSerializer.AsString(loadedTreeCompact));

            var pre = new[] {1, 2, 4, 7, 6, 5, 3, 0, 8};
            var ino = new[] {7, 4, 6, 2, 5, 1, 3, 8, 0};

            var reconstructed = BinaryTreeStringSerializer.Reconstruct(pre, ino);
            Console.WriteLine(BinaryTreeStringSerializer.AsString(reconstructed));

            Console.ReadLine();
        }
開發者ID:saguiitay,項目名稱:BackToBasics,代碼行數:83,代碼來源:Program.cs


注:本文中的BinaryTree.GetPreOrderEnumerator方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。