二叉树建立、遍历(前序,中序,后序),求叶节点个数,求节点个数
来源:互联网 发布:马勒第六交响曲知乎 编辑:程序博客网 时间:2024/06/05 17:47
二叉树是笔试面试中考试最频繁的数据结构之一,主要包括,程序建立一个二叉树,三种次序遍历二叉树,返回叶子节点的数目,求二叉树节点的总数等。建立一个二叉树节点的数据结构
typedef struct Node
{
int data;
struct Node *left,*right;
}Node;,结构体内包括数据,左子树,又子树;
一、建立二叉树的程序代码如下
- Node *CreatBTree() //建立二叉树
- {
- Node *t;
- int x;
- scanf("%d",&x);
- if(x==0)
- {
- t=NULL;
- }
- else
- {
- t=(Node*)malloc(sizeof(Node));
- t->data=x;
- printf("\n请输入%d结点的左子结点:",t->data );
- t->left=CreatBTree();
- printf("\n请输入%d结点的右子结点:",t->data );
- t->right=CreatBTree();
- }
- return t;
- }
Node *CreatBTree() //建立二叉树{Node *t;int x;scanf("%d",&x);if(x==0){t=NULL;}else{t=(Node*)malloc(sizeof(Node));t->data=x;printf("\n请输入%d结点的左子结点:",t->data );t->left=CreatBTree();printf("\n请输入%d结点的右子结点:",t->data );t->right=CreatBTree();}return t;}
二、前序遍历二叉树
- void preVisit(Node *T)
- {
- if(T==NULL) return;
- else
- {
- printf("%3d",T->data);
- preVisit(T->left);
- preVisit(T->right);
- }
- }
void preVisit(Node *T){if(T==NULL) return;else{printf("%3d",T->data);preVisit(T->left);preVisit(T->right);}}
三、中序遍历二叉树
- void middVisit(Node *T)
- {
- if(T==NULL) return;
- else
- {
- middVisit(T->left);
- printf("%3d",T->data);
- middVisit(T->right);
- }
- }
void middVisit(Node *T){if(T==NULL) return;else{middVisit(T->left);printf("%3d",T->data);middVisit(T->right);}}
四、后序遍历二叉树
- void lastVisit(Node *T)
- {
- if(T==NULL) return;
- else
- {
- lastVisit(T->left);
- lastVisit(T->right);
- printf("%3d",T->data);
- }
- }
void lastVisit(Node *T){if(T==NULL) return;else{lastVisit(T->left);lastVisit(T->right);printf("%3d",T->data);}}五、返回叶子节点数目
- int leafnum(Node *T)
- {
- if (!T)
- {
- return 0;
- }
- else if ((!T->left)&&(!T->right))
- {
- return 1;
- }
- else
- {
- return ((leafnum(T->left)+leafnum(T->right)));
- }
- }
int leafnum(Node *T){if (!T){return 0;}else if ((!T->left)&&(!T->right)){return 1;}else{return ((leafnum(T->left)+leafnum(T->right)));}}
六、返回节点总数目
- int Nodenum(Node *T)
- {
- if (T)
- {
- return 1+Nodenum(T->left)+Nodenum(T->right);
- }
- if (T==NULL)
- {
- return 0;
- }
- }
int Nodenum(Node *T){if (T){return 1+Nodenum(T->left)+Nodenum(T->right);}if (T==NULL){return 0;}}
七、测试程序;
- int menu();
- void main()
- {
- Node *T=NULL;
- int choice;
- do{
- choice=menu();
- if(choice==1)
- {
- printf("二叉树的建立,以输入“0”表示结束:!\n");
- printf("请输入根结点:\n");
- T=CreatBTree();
- printf("二叉树成功建立");
- }
- else if(choice==2)
- {
- printf("先序遍历二叉树 :\n");
- preVisit(T);
- }
- else if(choice==3)
- {
- printf("中序遍历二叉树:\n");
- middVisit(T);
- }
- else if(choice==4)
- {
- printf("后序遍历二叉树 :\n ");
- lastVisit(T);
- }
- else if(choice==5)
- {
- int ct=10;
- ct=leafnum(T);
- printf(" 二叉树的叶子结点数为 : \n");
- printf("%d\n",ct);
- }
- else if(choice==7)
- {
- int count=Nodenum(T);
- printf("该二叉树总共有%d个结点。\n",count);
- }
- else if(choice==8)
- exit(0);
- }while(choice<=8);
- }
int menu();void main(){Node *T=NULL;int choice;do{choice=menu();if(choice==1){printf("二叉树的建立,以输入“0”表示结束:!\n");printf("请输入根结点:\n");T=CreatBTree();printf("二叉树成功建立");}else if(choice==2){printf("先序遍历二叉树 :\n");preVisit(T);}else if(choice==3){printf("中序遍历二叉树:\n");middVisit(T);}else if(choice==4){printf("后序遍历二叉树 :\n ");lastVisit(T);}else if(choice==5){int ct=10;ct=leafnum(T);printf(" 二叉树的叶子结点数为 : \n");printf("%d\n",ct);}else if(choice==7){int count=Nodenum(T);printf("该二叉树总共有%d个结点。\n",count);}else if(choice==8)exit(0);}while(choice<=8);}
- int menu()
- {
- int choice;
- printf("\n");
- printf(" 二叉树 \n");
- printf(" ***************************\n");
- printf(" * *\n");
- printf(" * 主菜单 *\n");
- printf(" * 1 建立二叉树 *\n");
- printf(" * 2 先序遍历二叉树 *\n");
- printf(" * 3 中序遍历二叉树 *\n");
- printf(" * 4 后序遍历二叉树 *\n");
- printf(" * 5 二叉树的叶子结点数 *\n");
- printf(" * 7 二叉树的所有结点数 *\n");
- printf(" * 8 退出程序运行 *\n");
- printf(" ****************************\n");
- printf(" 请输入您的选择(1,2,3,4,5,6,7,8): \n");
- scanf("%d",&choice);
- return choice;
- }
int menu(){ int choice;printf("\n");printf(" 二叉树 \n");printf(" ***************************\n");printf(" * *\n");printf(" * 主菜单 *\n");printf(" * 1 建立二叉树 *\n");printf(" * 2 先序遍历二叉树 *\n");printf(" * 3 中序遍历二叉树 *\n");printf(" * 4 后序遍历二叉树 *\n");printf(" * 5 二叉树的叶子结点数 *\n");printf(" * 7 二叉树的所有结点数 *\n");printf(" * 8 退出程序运行 *\n");printf(" ****************************\n");printf(" 请输入您的选择(1,2,3,4,5,6,7,8): \n");scanf("%d",&choice);return choice;}
- 二叉树建立、遍历(前序,中序,后序),求叶节点个数,求节点个数
- 二叉树的先序 中序 后序 层次遍历 求叶节点个数、深度、最大元、最小元
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- c语言实现二叉树先序,中序,后序(递归),层次遍历,求叶子节点个数及树的深度,下一篇写非递归的遍历
- [C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现
- 求二叉树的高,节点个数,遍历二叉树
- 先序建树中序后序遍历及求叶子节点个数
- 二叉树求高度、叶子节点个数、总节点个数
- 完全二叉树求叶子节点个数
- 创建二叉树求叶子节点个数
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 图的邻接表表示法及顶点入度、出度的计算方法
- mysql 字符集的问题
- javaweb多行数据提交问题
- 硬实时与软实时
- python学习_多线程
- 二叉树建立、遍历(前序,中序,后序),求叶节点个数,求节点个数
- RMAN进行备份恢复
- 字符串复制问题(一)
- 软件开发项目进度控制浅谈
- 实例阐述MapReduce中的基本概念
- Tales of Mystery and Imagination——2、The Black Cat
- C++ Static
- jquery瀑布流插件
- Segments by ITL Waits 问题及解决