数据结构二叉树遍历有关算法

数据结构二叉树遍历有关算法

ID:16275631

大小:28.50 KB

页数:3页

时间:2018-08-08

数据结构二叉树遍历有关算法_第1页
数据结构二叉树遍历有关算法_第2页
数据结构二叉树遍历有关算法_第3页
资源描述:

《数据结构二叉树遍历有关算法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、//------------------二叉树结点的数据结构---------------typedefstructBiTNode//树节点定义{intvalue;BiTNode*lchild,*rchild;}BiTNode,*BiTree;//-------------------二叉树的遍历------------------voidPreorder(BiTreeroot)//先序遍历以T树根指针的二叉树(递归算法){if(root){cout<value;Preorder(root->lchild);Preorder(root->rchild);}}//--------

2、-----------先序遍历求二叉树的深度------------------intdepth=0,h=0;voidBiTreeDepthPre(BiTreeroot,inth,int&depth){if(root){if(h>depth)depth=h;BiTreeDepthPre(root->lchild,h+1,depth);BiTreeDepthPre(root->rchild,h+1,depth);}}//-------------------后序遍历求二叉树的深度------------------intBiTreeDepthPost(BiTreeroot){inthl=0,h

3、r=0;if(!root)return0;else{hl=BiTreeDepthPost(root->lchild);hr=BiTreeDepthPost(root->rchild);if(hl>=hr)returnhl+1;elsereturnhr+1;}}//-------------------二叉树的叶子节点数------------------intLeavesCount=0,noLeavesCount=0;voidCountLeaves(BiTreeroot)//计算叶子、非叶子数{if(root){if(!root->lchild&&!root->rchild)LeavesCo

4、unt++;elsenoLeavesCount++;CountLeaves(root->lchild);CountLeaves(root->rchild);}}//-------------------二叉树的节点最大值------------------intmaxvalue=-10000;voidFind_max_value(BiTreeroot){if(root){if(root->value>maxvalue)maxvalue=root->value;Find_max_value(root->lchild);Find_max_value(root->rchild);}}//-----

5、--------------交换二叉树的左右子树------------------voidchange(BiTree&root)//交换以T树根的二叉树的左右子树(递归算法){if(root){change(root->lchild);change(root->rchild);BiTNode*temp;temp=newBiTNode;temp->lchild=root->lchild;root->lchild=root->rchild;root->rchild=temp->lchild;deletetemp;}}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。