
数据结构 AVL树的高度怎么算?
书中给出的一段代码T->Height=Max(Height(T->left),Height(T->Right))+1;但是并没有给出Max(...)的代码...
书中给出的一段代码
T->Height = Max( Height( T->left ), Height( T->Right ) ) + 1;
但是并没有给出 Max(...)的代码 展开
T->Height = Max( Height( T->left ), Height( T->Right ) ) + 1;
但是并没有给出 Max(...)的代码 展开
2个回答
展开全部
在C++中有Max()函数是库函数,包含头文件就可以了,在C语言中,需要自己定义,Max()函数就是返回两个整数中的较大的那个数,具体实现如下:
int Max(int a, int b)
{
return a>b?a:b;//a大于b为真的话就返回a,a大于b为假就返回b
}
这就是你要的Max函数实现。希望对你有所帮助,望采纳!
int Max(int a, int b)
{
return a>b?a:b;//a大于b为真的话就返回a,a大于b为假就返回b
}
这就是你要的Max函数实现。希望对你有所帮助,望采纳!

2024-11-30 广告
RNA-seq数据分析是转录组研究的核心,包括数据预处理、序列比对、定量分析、差异表达分析、功能注释和可视化等步骤。数据预处理主要是质量控制和去除低质量序列。序列比对使用HISAT2、STAR等工具将reads比对到参考基因组。定量分析评估...
点击进入详情页
本回答由迈杰提供
展开全部
int ch(bitnode *T)//计算高度的函数
{
if(T==NULL) return 0;
int temp_l=0,temp_r=0,count = 0;
temp_l = ch(T->lchild) + 1;
temp_r = ch(T->rchild) + 1;
if(temp_l>temp_r) return temp_l;
else return temp_r;
}
完整代码:
#include<iostream>
using namespace std;
struct bitnode{
char data;
struct bitnode *lchild, *rchild;
};
bitnode*T;
void CreateBiTree(bitnode* &T){
char ch;
if((ch=getchar())=='#')T=NULL;
else
{
T=new bitnode;
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int ch(bitnode *T)
{
if(T==NULL) return 0;
int temp_l=0,temp_r=0,count = 0;
temp_l = ch(T->lchild) + 1;
temp_r = ch(T->rchild) + 1;
if(temp_l>temp_r) return temp_l;
else return temp_r;
}
int main()
{
cout<<"创建一颗树,其中A->Z字符代表树的数据,用“#”表示空树:"<<endl;
CreateBiTree(T);
cout<<"计算高度"<<endl;
cout<<ch(T)<<endl;
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询