二叉树的基本运算
来源:互联网 发布:手游代充平台 知乎 编辑:程序博客网 时间:2024/05/21 16:59
#include<stdio.h>#include<stdlib.h>//二叉树结点定义typedef struct BT{ char data; struct BT *lchild; struct BT *rchild;}BT;//建立二叉树(前序创建)BT *createtree(){ BT *bt; char x; scanf("%c",&x); if (x == '0') bt = NULL; else { bt = (BT *)malloc(sizeof(BT)); bt->data = x; bt->lchild = createtree(); bt->rchild = createtree(); } return (bt);}//前序递归遍历void Preorder(BT *bt){ if (bt != NULL) { printf("%c ",bt->data); Preorder(bt->lchild); Preorder(bt->rchild); }}int count = 0;//统计二叉树叶子结点数void leafnum(BT *bt) //开始时,bt指向根结点,count(初值为0)作为全局变量用来统计个数{ if (bt) //判断bt是否为空 { if (bt->lchild == NULL && bt->rchild == NULL) { count++; } leafnum(bt->lchild); //递归遍历左子树 leafnum(bt->rchild); //递归遍历右子树 }}//统计二叉树结点个数void Nodenum(BT *bt){ if (bt) { count++; //count(初值为0)为全局变量 Nodenum(bt->lchild); //递归遍历左子树 Nodenum(bt->rchild); //递归遍历右子树 }}//求二叉树的深度int treedepth(BT *bt){ int ldepth, rdepth; if (bt == NULL) return 0; else { ldepth = treedepth(bt->lchild); rdepth = treedepth(bt->rchild); if (ldepth > rdepth) return (ldepth + 1); else return (rdepth + 1); }}//查找数据元素BT *search(BT *bt, char x) //查找数据x{ if (bt->data == x) //当查找成功时返回该结点的指针 return bt; //否则,在左右子树分别查找 if (bt->lchild != NULL) return (search(bt->lchild, x)); if (bt->rchild != NULL) return (search(bt->rchild, x)); return NULL; //查找失败,返回空指针}
阅读全文
1 0
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算实验
- 二叉树的基本运算实验
- 遍历二叉树的基本运算
- 二叉树的一些基本运算
- 数据结构学习-二叉树的基本运算
- Java实现二叉树的基本运算
- 二叉树各种基本运算的算法
- 数据结构---二叉树的基本运算
- 二叉树的各种基本运算
- 二叉树的基本运算及其实现的例程
- 实现二叉树的各种基本运算的算法
- 数据结构 - 树和二叉树的基本运算实现
- 树和二叉树的基本运算实现
- 实现二叉树各种基本运算的算法
- HttpServletRequest 中 getRequestURL和getRequestURI的区别
- Maven使用tomcat8-maven-plugin插件
- JavaScript中undefined与null的区别
- JAVA中转义字符
- 实例详解抽象类和接口的区别
- 二叉树的基本运算
- listview+DrawerLayout
- 线程池初探(一)
- UITableview布局 消除顶部空白contentInset
- SVN客户端TortoiseSVN增加提交SVN时需强制添加内容说明的方法
- 二叉树遍历(前序,中序,后序)
- Gitee码云通过WebHooks实现自动同步代码部署
- 17-18-19-对象的构造函数
- java static关键字