二叉树的其他操作
来源:互联网 发布:windows7安装apache 编辑:程序博客网 时间:2024/06/05 16:28
二叉树的操作
- 树中节点的数目
int Size(bt_node *ptr) // 树中所有节点的数目{ if(ptr == NULL) return 0; else return 1+Size(ptr->leftchild)+Size(ptr->rightchild);}int SizeLeaf(bt_node *ptr) // 树中叶子节点的数目{ if(ptr == NULL) return 0; else if(ptr->leftchild == NULL && ptr->rightchild == NULL) { return 1; }else { return SizeLeaf(ptr->leftchild)+SizeLeaf(ptr->rightchild); }}int SizeOneBranch(bt_node *ptr) // 树中单分支节点的数目{ if(ptr == NULL) return 0; else if((ptr->leftchild != NULL && ptr->rightchild == NULL)||ptr->leftchild == NULL && ptr->rightchild != NULL)) { return 1; }else { return SizeLeaf(ptr->leftchild)+SizeLeaf(ptr->rightchild); }}int SizeTwoBranch(bt_node *ptr) // 树中双分支节点的数目{ if(ptr == NULL) return 0; else if(ptr->leftchild != NULL && ptr->rightchild != NULL) { return 1; }else { return SizeLeaf(ptr->leftchild)+SizeLeaf(ptr->rightchild); }}
- 树的深度
int Max(int a,int b){ return a>b? a:b;}int Depth(bt_node *ptr){ if(ptr == NULL) { return 0; }else { return Max(Depth(ptr->leftchild),Depth(ptr->rightchild)) + 1; }}
- 寻找父亲节点
bt_node * Parent(bt_node *ptr,bt_node *child){ if(ptr == NULL || ptr->leftchild == child || ptr->rightchild == child) { return ptr; }else { bt_node *p = Parent(ptr->leftchild,child); if(NULL == p) { p = Parent(ptr->rightchild,child); } return p; }}bt_node * FindParet(bt_node *ptr,bt_node *child){ if(ptr == NULL || child == NULL || ptr == child) { return NULL; }else { return Parent(ptr,child); }}
阅读全文
0 0
- 二叉树的其他操作
- 二叉树的其他操作
- 二叉树其他操作
- 二叉查找树的其他操作
- 二叉查找树其他操作
- 二叉树的建立与输出以及其他一些相关操作(递归算法实现) C语言
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- 二叉树的操作
- C语言命令行参数解析函数
- 第二次作业
- 个人总结---微信抢票应用
- <<The C programming Language>>推荐
- 封装MVP 实现登录注册功能
- 二叉树的其他操作
- okhttp_Reclerview
- unity中使用Visual Studio
- 请求网络接口_注册登录之MVP模式
- Tweenmax ease中的各种缓动
- 服务器排障 之 nginx 499 错误的解决 【转】
- 欢迎使用CSDN-markdown编辑器
- 【20171015】C语言每日一练
- HDOJ2059(龟兔赛跑 DP)