二叉树的基本操作
来源:互联网 发布:c语言matlab混编 编辑:程序博客网 时间:2024/05/17 01:47
/*BiTree.h 二叉树的其他基本操作*/
//二叉树的深度
int Depth_BiTree(BiTree T)
{
if(T)
{
int i=Depth_BiTree(T->lchild);
int j=Depth_BiTree(T->rchild);
if(i>j)
return i+1;
return j+1;
}
return 0;
}//Depth_BiTree
// 调用之前 level (层)的初值为 1。
// dval (深度)的初值为 0。
void Depth(BiTree T , int level, int &dval){
if ( T ) {
if (level>dval) dval = level;
Depth( T->lchild, level+1, dval );
Depth( T->rchild, level+1, dval );
}
}
//二叉树中所有结点个数
int NumOfNode(BiTree T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
int i=NumOfNode(T->lchild);
i+=NumOfNode(T->rchild);
return i+1;
}
return 0;
}//NumOfNode
//二叉树的叶子结点总数
int NumOfLeaf(BiTree T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
int i=NumOfLeaf(T->lchild);
i+=NumOfLeaf(T->rchild);
return i;
}
return 0;
}//NumOfLeaf
void Print(int i)
{
for(int j=0;j<i;j++)
{
cout<<"_";
}
}
//凹入法显示二叉树
void PrintTree(BiTree T,int i)
{
if(T)
{
Print(i);
cout<<T->data<<endl;//输出根结点
if(T->lchild==NULL&&T->rchild==NULL)
{
return;
}else if(T->lchild==NULL)
{
Print(i+2);
cout<<"*"<<endl;
}else{
PrintTree(T->lchild,i+2);//遍历左子树
}
if(T->rchild==NULL)
{
Print(i+2);
cout<<"*"<<endl;
}else
{
PrintTree(T->rchild,i+2);//遍历右子树
}
}
}//PrintTree
//广义表输出二叉树
void PrintBiTree(BiTree T)
{
if(T)
{
cout<<T->data;//访问根结点
if(T->lchild==NULL&&T->rchild==NULL)
return;
//if(T->lchild!=NULL)
cout<<"(";
PrintBiTree(T->lchild);//遍历左子树
cout<<",";
PrintBiTree(T->rchild);//遍历右子树
cout<<")";
}
else
cout<<" ";
}//PrintBiTree
//二叉树的深度
int Depth_BiTree(BiTree T)
{
if(T)
{
int i=Depth_BiTree(T->lchild);
int j=Depth_BiTree(T->rchild);
if(i>j)
return i+1;
return j+1;
}
return 0;
}//Depth_BiTree
// 调用之前 level (层)的初值为 1。
// dval (深度)的初值为 0。
void Depth(BiTree T , int level, int &dval){
if ( T ) {
if (level>dval) dval = level;
Depth( T->lchild, level+1, dval );
Depth( T->rchild, level+1, dval );
}
}
//二叉树中所有结点个数
int NumOfNode(BiTree T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
int i=NumOfNode(T->lchild);
i+=NumOfNode(T->rchild);
return i+1;
}
return 0;
}//NumOfNode
//二叉树的叶子结点总数
int NumOfLeaf(BiTree T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
return 1;
int i=NumOfLeaf(T->lchild);
i+=NumOfLeaf(T->rchild);
return i;
}
return 0;
}//NumOfLeaf
void Print(int i)
{
for(int j=0;j<i;j++)
{
cout<<"_";
}
}
//凹入法显示二叉树
void PrintTree(BiTree T,int i)
{
if(T)
{
Print(i);
cout<<T->data<<endl;//输出根结点
if(T->lchild==NULL&&T->rchild==NULL)
{
return;
}else if(T->lchild==NULL)
{
Print(i+2);
cout<<"*"<<endl;
}else{
PrintTree(T->lchild,i+2);//遍历左子树
}
if(T->rchild==NULL)
{
Print(i+2);
cout<<"*"<<endl;
}else
{
PrintTree(T->rchild,i+2);//遍历右子树
}
}
}//PrintTree
//广义表输出二叉树
void PrintBiTree(BiTree T)
{
if(T)
{
cout<<T->data;//访问根结点
if(T->lchild==NULL&&T->rchild==NULL)
return;
//if(T->lchild!=NULL)
cout<<"(";
PrintBiTree(T->lchild);//遍历左子树
cout<<",";
PrintBiTree(T->rchild);//遍历右子树
cout<<")";
}
else
cout<<" ";
}//PrintBiTree
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 我
- 我
- [转]职场少走弯路的十条忠告
- 当代计算机专业学生
- 遍历二叉树
- 二叉树的基本操作
- load dll 简单方式
- 管窥Visual C++ 2008 Beta 2(Orcas)
- 汇编端口作用详细列表-1
- 二叉树存储结构
- Word2007页码设置
- Word2007页码设置
- 找工作vs找女朋友
- 操作二叉树使用的队列