树的基本运用二
2015-06-25 10:39
323 查看
树的层次遍历:也即是从跟节点开始,每一层都从左到右输出。可利用队列的先进先出来
求树的深度:
求树的节点个数:利用递归即可实现
void Level_Traverse(Bintree root) { queue<Bintree>q; Bintree temp; temp=root; if(temp==NULL) return; q.push (temp); cout<<temp->data; while(!q.empty ()) { temp=q.front (); q.pop (); cout<<temp->data<<endl; if(temp->lchild) { cout<<temp->lchild<<endl; q.push (temp->lchild); } if(temp->rchild) { cout<<temp->rchild<<endl; q.push (temp->rchild); } } }
求树的深度:
int Depth_Bintree(Bintree *root) { if(!root) return 0; int len1,len2; len1=Depth_Bintree(root->lchild); len2=Depth_Bintree(root->rchild); return (len1>len2?len1:len2)+1; }
求树的节点个数:利用递归即可实现
int Count_Bintree(Bintree root) { if(root) { return ((Count_Bintree(root->lchild)+Count_Bintree(root->rchild))+1); } }
相关文章推荐
- opencv各种滤波分析bilateralFilter、Laplacian、bitwise_and和GaussianBlur
- 10.11 Beta 2 上不能运行xcode6.3.2了
- orika的问题
- mysql 索引
- gzip和gunzip 解压参数
- java 动态代理
- jquery实现浮动的侧栏实例
- jQuery Mobile知识点总结
- 使用Java VisualVM监控远程JVM
- 泛型的使用
- maven入门教程
- java中File目录管理(四)目录管理小工具的设计
- 【独立开发者er Cocos2d-x实战 001】csb文件导出和加载
- 【Win10 应用开发】人脸识别
- 吐血配置Apache虚拟主机
- 在js里的路径问题
- hadoop RPC(一)
- yii运行机制
- UICollectionView
- 金典 SQL笔记(1)