Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var bTree = new BinaryTree <String>("A");
            var root  = bTree.Root;

            bTree.InsertLeft(root, "B");
            bTree.InsertRight(root, "C");

            var B = bTree.Root.lchild;

            bTree.InsertLeft(B, "D");
            bTree.InsertRight(B, "E");

            Console.WriteLine("前序遍历:根->左->右");
            bTree.PreOrder(root);
            Console.WriteLine("中序遍历:左->根->右");
            bTree.MidOrder(root);
            Console.WriteLine("后序遍历:左->右->根");
            bTree.PostOrder(root);

            Console.WriteLine("非递归前序遍历:根->左->右");
            bTree.PreOrderNR(root);
            Console.WriteLine("非递归中序遍历:左->根->右");
            bTree.MidOrderNR(root);

            var a = bTree.GetDepth(root);

            //bTree.InsertLeft(root, "Test");

            Console.ReadLine();
        }
Example #2
0
        static void MyBinaryTreeBasicTest()
        {
            // 构造一颗二叉树,根节点为"A"
            BinaryTree <string> bTree    = new BinaryTree <string>("A");
            Node <string>       rootNode = bTree.Root;

            // 向根节点"A"插入左孩子节点"B"和右孩子节点"C"
            bTree.InsertLeft(rootNode, "B");
            bTree.InsertRight(rootNode, "C");
            // 向节点"B"插入左孩子节点"D"和右孩子节点"E"
            Node <string> nodeB = rootNode.lchild;

            bTree.InsertLeft(nodeB, "D");
            bTree.InsertRight(nodeB, "E");
            // 向节点"C"插入右孩子节点"F"
            Node <string> nodeC = rootNode.rchild;

            bTree.InsertRight(nodeC, "F");
            // 计算二叉树目前的深度
            Console.WriteLine("二叉树目前的深度是: {0}", bTree.GetDepth(bTree.Root));



            // 前序遍历
            Console.WriteLine("---------前序遍历---------");
            bTree.PreOrder(bTree.Root);
            // 中序遍历
            Console.WriteLine();
            Console.WriteLine("---------中序遍历---------");
            bTree.MidOrder(bTree.Root);
            // 后序遍历
            Console.WriteLine();
            Console.WriteLine("---------后续遍历---------");
            bTree.PostOrder(bTree.Root);
            Console.WriteLine();



            // 前序遍历(非递归)
            Console.WriteLine("---------前序遍历(非递归)---------");
            bTree.PreOrderNoRecurise(bTree.Root);
            // 中序遍历(非递归)
            Console.WriteLine();
            Console.WriteLine("---------中序遍历(非递归)---------");
            bTree.MidOrderNoRecurise(bTree.Root);
            // 后序遍历(非递归)
            Console.WriteLine();
            Console.WriteLine("---------后序遍历(非递归)---------");
            bTree.PostOrderNoRecurise(bTree.Root);
            Console.WriteLine();
        }