鍵盤輸入一個字串,利用二叉樹前序遍歷的結果建成一棵二叉樹,并用三種遍歷方法列印,比較是否與自己預先想象的相一致。再求樹的深度、1度結點數、2度節點數,交換二叉樹的左右子樹并輸出交換后的中序遍歷結果驗證交換的正確性。找到二叉樹中序遍歷最后一個結點并輸出結點值。
(1)void visit(BiTree p) /*輸出p指標指向的結點*/
(2)void Preorder(BiTree T) /*前序遍歷*/
(3)void Inorder(BiTree T) /*中序遍歷*/
(4)void Postorder(BiTree T) /*后序遍歷*/
(5)BiTree CreateTree( ) /*以前序遍歷的順序建立二叉樹*/
(6)int deep(BiTree T) /*求二叉樹深度*/
(7)int leaf(BiTree T) /*求葉子結點數*/
(8)int OneChild(BiTree T) /*求1度結點數*/
(9)int TwoChild(BiTree T) /*求2度結點數*/
(10)void Exchange(BiTree T) /*二叉樹左右子樹交換*/
(11)BiTree InorderLastNode(BiTree T); /*找二叉樹中序遍歷最后一個結點*/
int main()
{
BiTree T;
printf("\n以前序遍歷的二叉樹:",CreateTree());
printf("\n先序遍歷:");
Preorder(T);
printf("\n中序遍歷:");
Inorder(T);
printf("\n后序遍歷:");
Postorder(T);
printf("\n");
printf("樹的深度=%d\n",deep(T));
printf("1度結點數=%d\n",OneChild(T));
printf("2度結點數=%d\n",TwoChild(T));
Exchange(T);
Inorder(T);
printf("二叉樹中序遍歷最后一個結點=%c\n",InorderLastNode(T));
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78746.html
標籤:數據庫及相關技術
上一篇:單目測距 標示距離的問題
