二叉树的基本操作及应用(三)
hl:hr; return (max+1); } else return 0; } void depthxian(BinTree bt,int h1) {//先序遍歷求二叉樹高度 if(bt!=NULL) { if(h1>depth) depth=h1; depthxian(bt->lchild,h1+1); depthxian(bt->rchild,h1+1); } } void PrintTree(BinTree bt,int nlayer) {//樹狀打印二叉樹 if(bt==NULL) return; PrintTree(bt->rchild,nlayer+1); for(int i=0;i<nlayer;i++) printf(" "); printf("%c\n",bt->data); PrintTree(bt->lchild,nlayer+1); } void Inorderxian(BinTree &T) {//先序輸出二叉樹 if(T!=NULL) { printf("%3c",T->data); Inorderxian(T->lchild); Inorderxian(T->rchild); } } void Inorderzhong(BinTree &T) {//中序輸出二叉樹 if(T!=NULL) { Inorderzhong(T->lchild); printf("%3c",T->data); Inorderzhong(T->rchild); } } void Inorderhou(BinTree &T) {//后序輸出二叉樹 if(T!=NULL) { Inorderhou(T->lchild); Inorderhou(T->rchild); printf("%3c",T->data); } } void main() { DataType ch; BinTree root; BinTree T=NULL; BinTree BT=NULL; int d=0,h=0,l=1,n=0; int x,count2=0,count3=0; DataType first[26],mid[26],last[26]; root=(BinTNode *)malloc(sizeof(BinTNode)); printf("*****************************************************************\n"); printf("* 1、擴展先序遍歷創建二叉樹 2、統計二叉樹葉子結點數 *\n"); printf("* 3、先序遍歷求二叉樹高度 4、后序遍歷求二叉樹高度 *\n"); printf("* 5、按樹狀打印二叉樹 7、利用先序和中序創建二叉樹 *\n"); printf("* 8、利用后序和中序創建二叉樹 9、先序輸出二叉樹 *\n"); printf("* 10、中序輸出二叉樹 11、后序輸出二叉樹 *\n"); printf("*****************************************************************\n"); printf("請輸入你的選擇:\n"); //第一種方法創建二叉樹 printf("請依照先序遍歷的順序輸入須要中序遍歷的字符:\n"); createB(root); printf("先序遍歷輸入二叉樹例如以下:"); Inorderxian(root); printf("\n"); printf("中序遍歷輸入二叉樹例如以下:"); Inorderzhong(root); printf("\n"); printf("后序遍歷輸入二叉樹例如以下:"); Inorderhou(root); printf("\n\n"); printf("統計二叉樹葉子數:"); countdef(root,n); printf("count=%d\n",n); printf("先序遍歷輸出二叉樹的高度:"); depthxian(root,h1); printf("depthxain=%d\n",depth); printf("后序遍歷輸出二叉樹的高度:"); printf("depthhou=%d\n",depthhou(root)); printf("打印輸出二叉樹的樹狀結構:\n"); PrintTree(root,1); //另外一種方法創建二叉樹 printf("請輸入先序和中序序列:\n"); scanf("%s%s",first,mid); GetPostOrder(first,mid,T, strlen(first)); printf("打印輸出二叉樹的樹狀結構:\n"); PrintTree(root,1); printf("先序遍歷輸入二叉樹例如以下:"); Inorderxian(T); printf("\n"); printf("中序遍歷輸入二叉樹例如以下:"); Inorderzhong(T); printf("\n"); printf("后序遍歷輸入二叉樹例如以下:"); Inorderhou(T); printf("\n"); printf("統計二叉樹葉子數:"); countdef(T,count2); printf("count2=%d\n",count2); printf("先序遍歷輸出二叉樹的高度:"); depthxian(T,h1); printf("depthxain=%d\n",depth); printf("后序遍歷輸出二叉樹的高度:"); printf("depthhou=%d\n",depthhou(T)); //第三種方法創建二叉樹 printf("請輸入后序和中序序列:\n"); scanf("%s%s",last,mid); GetPreOrder(last,mid,BT,strlen(last)); printf("打印輸出二叉樹的樹狀結構:\n"); PrintTree(BT,1); printf("先序遍歷輸入二叉樹例如以下:"); Inorderxian(BT); printf("\n"); printf("中序遍歷輸入二叉樹例如以下:"); Inorderzhong(BT); printf("\n"); printf("后序遍歷輸入二叉樹例如以下:"); Inorderhou(BT); printf("\n"); printf("統計二叉樹葉子數:"); countdef(BT,count3); printf("count3=%d\n",count3); printf("先序遍歷輸出二叉樹的高度:"); depthxian(BT,h1); printf("depthxain=%d\n",depth); printf("后序遍歷輸出二叉樹的高度:"); printf("depthhou=%d\n",depthhou(BT)); printf("\n"); printf("\n"); }
總結
以上是生活随笔為你收集整理的二叉树的基本操作及应用(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到换灯泡是啥意思
- 下一篇: 梦到吃到屎是什么预兆