数据结构 树 二叉树的建立及遍历 C语言版
来源:互联网 发布:ubuntu删除文件夹命令 编辑:程序博客网 时间:2024/06/05 04:40
//二叉树的建立以及先序、中序、后序遍历算法 以及统计二叉树的叶子结点数目算法#include <stdio.h>#include <stdlib.h>typedef char DataType;typedef struct Node{DataType data;struct Node *LChild;struct Node *RChild;} BiTNode,*BiTree; //定义二叉树void CreateBiTree(BiTree *bt) //用先序遍历创建二叉树{char ch;ch=getchar();if(ch=='.') (*bt)=NULL;else{*bt=(BiTree)malloc(sizeof(BiTNode));(*bt)->data=ch;CreateBiTree(&((*bt)->LChild));CreateBiTree(&((*bt)->RChild));}}void PreOrder(BiTree root) //先序遍历二叉树{if(root!=NULL){printf("%c",root->data);PreOrder(root->LChild);PreOrder(root->RChild);}}void InOrder(BiTree root) //中序遍历二叉树{if(root!=NULL){InOrder(root->LChild);printf("%c",root->data);InOrder(root->RChild);}}void PostOrder(BiTree root) //后序遍历二叉树{if(root!=NULL){PostOrder(root->LChild);PostOrder(root->RChild);printf("%c",root->data);}}int LeafCount=0;void leaf(BiTree root) //求出叶子结点的数目{if(root!=NULL){leaf(root->LChild);leaf(root->RChild);if(root->LChild==NULL && root->RChild==NULL)LeafCount++;}}int main(){BiTree bt;printf("用先序遍历创建二叉树 请输入树的内容 形式如AB..CD...的格式\n") ;CreateBiTree(&bt);PreOrder(bt);printf("\n");InOrder(bt);printf("\n");PostOrder(bt);printf("\n");printf("这棵树的叶子结点数目为:\n");leaf(bt);printf("%d\n",LeafCount);return 0;}
阅读全文
2 0
- 数据结构 树 二叉树的建立及遍历 C语言版
- 二叉树的建立与递归遍历C语言版
- 数据结构实验:二叉树的遍历(C语言版)
- 链式二叉树的遍历C语言版
- 数据结构 二叉树的实现 c语言版
- 数据结构 树 层次遍历二叉树 C语言版
- 数据结构 二叉树的建立及三种遍历方法
- 数据结构之二叉树的建立及中序遍历
- 数据结构 二叉树的建立与遍历
- 递归实现二叉树的建立及前中后序遍历 c
- 二叉树的遍历(递归法)C语言版
- 二叉树的非递归遍历 C语言版
- 二叉树的非递归遍历 C语言版
- 二叉树的非递归遍历 C语言版
- 二叉树的非递归遍历 C语言版
- 二叉树的中序非递归遍历c语言版
- 《数据结构(C语言版)》- 树和二叉树
- 数据结构C语言版之二叉树操作
- 3 单网页表格信息采集
- Android Api demo系列(6) (App>Activity>Presentation)
- SteamVR
- UE4学习笔记20th:扩展游戏模式
- 使用redis作为缓存,数据还需要存入数据库中吗?
- 数据结构 树 二叉树的建立及遍历 C语言版
- 复选框(JCheckBox)
- 网络流24题-5
- 分别用Java和Scala求PV,UV测试程序
- UVA 10706 Number Sequence(模拟)
- Redis + Spring 的集成示例
- Gabor滤波
- reduceByKey和groupByKey区别与用法
- 关于Android的Context的使用和注意,防止内存泄漏和异常