Example #1
0
        static void Main(string[] args)
        {
            BinaryTree bTree = new BinaryTree("xvbc[#;a#RE##U");

            Console.Write("先序遍历:");
            bTree.PreOrder(bTree.Head);
            Console.WriteLine();
            Console.Write("中序遍历:");
            bTree.MidOrder(bTree.Head);
            Console.WriteLine();
            Console.Write("后序遍历:");
            bTree.AfterOrder(bTree.Head);
            Console.WriteLine();
            Console.Write("广度优先遍历:");
            bTree.LevelOrder();
            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("TREE - Binary");
            BinaryTree tree = new BinaryTree();

            tree.Add(5);
            tree.Add(7);
            tree.Add(2);
            tree.Add(1);
            tree.Add(6);
            tree.Add(3);

            //traversal
            //Depth first
            Console.WriteLine("\nPreorder");
            tree.Preorder(tree.Root, 0);
            Console.WriteLine("\nPostorder");
            tree.Postorder(tree.Root, 0);
            Console.WriteLine("\nInorder");
            tree.Inorder(tree.Root, 0);

            //Breadth first
            Console.WriteLine("\nUsing given level");
            DateTime startTime = DateTime.Now;

            tree.LevelOrder(tree.Root);
            Console.Write($" in {(DateTime.Now - startTime).TotalSeconds}s\n");

            Console.WriteLine("\nUsing queue");
            startTime = DateTime.Now;
            tree.LevelOrderWithQueue(tree.Root);
            Console.Write($" in {(DateTime.Now - startTime).TotalSeconds}s\n");

            //Search
            Console.WriteLine("\nSearching (Preorder)");
            string result = tree.Search(tree.Root, 6) != null ? "Found" : "Not found";

            Console.WriteLine($"Search for 6: {result}");
            result = tree.Search(tree.Root, 9) != null ? "Found" : "Not found";
            Console.WriteLine($"Search for 9: {result}");
        }