コード例 #1
0
        static void Main(string[] args)
        {
            BinTreeNode<string> A = new BinTreeNode<string>("A");
            BinTreeNode<string> B = new BinTreeNode<string>("B");
            BinTreeNode<string> C = new BinTreeNode<string>("C");
            BinTreeNode<string> D = new BinTreeNode<string>("D");

            BinTree<string> BT = new BinTree<string>(A);
            BT.Insert(A, B, C);
            BT.Insert(B, D, null);
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());

            BinTreeNode<string> Parent = BT.GetParent(D);
            BinTreeNode<string> LSibling = BT.GetLeftSibling(C);
            BinTreeNode<string> RSibling = BT.GetRightSibling(B);

            if (Parent != null)
                Console.WriteLine("结点{0}的Parent结点:{1}", D.Data, Parent.Data);
            else
                Console.WriteLine("结点{0}无Parent.", D.Data);

            if (LSibling != null)
                Console.WriteLine("结点{0}的左兄弟结点:{1}", C.Data, LSibling.Data);
            else
                Console.WriteLine("结点{0}无左兄弟结点.", C.Data);

            if (LSibling != null)
                Console.WriteLine("结点{0}的右兄弟结点:{1}", B.Data, RSibling.Data);
            else
                Console.WriteLine("结点{0}无右兄弟结点.", B.Data);

            BT.DeleteSubTree(D);
            Console.WriteLine("把结点{0}从二叉树中移除.", D.Data);
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());

            BinTreeNode<string> E = BT.Search("A");
            if (E != null && E.LeftChild != null)
            {
                Console.WriteLine("寻找结点{0}的左孩子为{1}", E.Data, E.LeftChild.Data);
            }
            else
            {
                Console.WriteLine("未找到{0}结点或者{0}结点左孩子为null", A.Data);
            }

            Console.WriteLine("叶子结点的个数:{0}", BT.GetLeafCont());

            BT.Exchange();
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());
        }