首页 树深度的递归算法

树深度的递归算法

举报
开通vip

树深度的递归算法树深度的递归算法 //求树深度的递归算法 int Depth(Bitree T) { if(!T) return 0; else{ m=Depth(T->lchild); n=Depth(T->rchild); return (m>n?m:n)+1; } }//Depth //求二叉树中以值为x的结点为根的子树深度 int Get_Sub_Depth(Bitree T, int x) { if(T){ if(T->data==x) return Depth(T); //找到了值为x的...

树深度的递归算法
树深度的递归算法 //求树深度的递归算法 int Depth(Bitree T) { if(!T) return 0; else{ m=Depth(T->lchild); n=Depth(T->rchild); return (m>n?m:n)+1; } }//Depth //求二叉树中以值为x的结点为根的子树深度 int Get_Sub_Depth(Bitree T, int x) { if(T){ if(T->data==x) return Depth(T); //找到了值为x的结点,求x为根的子树的其深度 else{ if(T->lchild) m=Get_Sub_Depth(T->lchild,x); if(T->rchild) n=Get_Sub_Depth(T->rchild,x); //在左右子树中继续寻找 } return m>n?m:n; } return 0; }//Get_Sub_Depth //交换所有结点的左右子树 void Bitree_Revolute(Bitree T) { if(T){ T->lchild <=> T->rchild; //交换左右子树 if(T->lchild) Bitree_Revolute(T->lchild); if(T->rchild) Bitree_Revolute(T->rchild); } }//Bitree_Revolute //根据顺序存储结构建立二叉链表 Status CreateBitree_SqList(Bitree &T,SqList sa) { Bitree ptr[sa.last+1]; //该数组储存与sa中各结点对应的树指针 if(!sa.last){ T=NULL; //空树 Return OK; } ptr[1]=(BTNode*)malloc(sizeof(BTNode)); ptr[1]->data=sa.elem[1]; //建立树根 T=ptr[1]; T->lchild=T->rchild=NULL; for(i=2;i<=sa.last;i++){ if(!sa.elem[i]) return ERROR; //顺序错误 ptr[i]=(BTNode*)malloc(sizeof(BTNode)); ptr[i]->data=sa.elem[i]; ptr[i]->lchild= ptr[i]->rchild=NULL; j=i/2; //找到结点i的双亲j if(i==j*2) ptr[j]->lchild=ptr[i]; //i是j的左孩子 else if(i==2*j+1)ptr[j]->rchild=ptr[i]; //i是j的右孩子 } return OK; }//CreateBitree_SqList
本文档为【树深度的递归算法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_083599
暂无简介~
格式:doc
大小:13KB
软件:Word
页数:0
分类:生产制造
上传时间:2017-11-11
浏览量:10