日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二叉树的操作(二叉树的创建、先序遍历---先根、中序遍历----先左、后续遍历---后根)

發布時間:2025/3/16 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树的操作(二叉树的创建、先序遍历---先根、中序遍历----先左、后续遍历---后根) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stdlib.h" #include "stdio.h" #include "Windows.h" #include <algorithm> using namespace std; /*引入頭文件*/ struct tnode /*定義二叉樹存儲結構*/ {char data;struct tnode*lchild;struct tnode*rchild; }; struct tnode tree; /*定義二叉樹指針*/void createtree(struct tnode*t) /*創建函數*/ {struct tnode*p=t; /*把二叉樹指針給p*/char check;if(p->lchild==NULL||p->rchild==NULL) /*判斷左右子樹是否為空*/{printf("please enter the data:"); /*輸入根結點數據*/scanf("%c",&(p->data));getchar();}if(p->lchild==NULL){printf("%c leftchild is null.Add? y/n\n",p->data); /*左子樹空,詢問是否創建*/scanf("%c",&check);getchar();if(check=='y'){struct tnode*q=(struct tnode*)malloc(sizeof(struct tnode)); /*開辟空間*/q->lchild=NULL;q->rchild=NULL;p->lchild=q;createtree(q);}}if(p->rchild==NULL) {printf("%c rightchild is NULL.Add? y/n\n",p->data); /*右子樹空,詢問是否創建*/scanf("%c",&check);getchar();if(check=='y'){struct tnode*q=(struct tnode*)malloc(sizeof(struct tnode)); /*開辟空間*/q->lchild=NULL;q->rchild=NULL;p->rchild=q; /*連到二叉樹上*/createtree(q);}} }void preorder(struct tnode*t) /*先序遍歷函數*/ {if(t){printf("%c ",t->data); /*輸出根結點數據*/preorder(t->lchild);preorder(t->rchild);} }void inorder(struct tnode*t) /*中序遍歷函數*/ {if(t){inorder(t->lchild);printf("%c ",t->data);inorder(t->rchild);} } void postorder(struct tnode*t) /*后序遍歷函數*/ {if(t){postorder(t->lchild);postorder(t->rchild);printf("%c ",t->data);} }void main() {//clrscr(); /*清屏函數*/tree.lchild=NULL; /*左子樹*/tree.rchild=NULL; /*右子樹*/createtree(&tree); /*創建二叉樹*/preorder(&tree); /*先序遍歷*/printf("\n");inorder(&tree); /*中序遍歷*/printf("\n");postorder(&tree); /*后序遍歷*/ }

總結

以上是生活随笔為你收集整理的二叉树的操作(二叉树的创建、先序遍历---先根、中序遍历----先左、后续遍历---后根)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。