判断是不是AVL平衡树
如果为空,则是
如果不为空,左右子树都是AVL且左右相差不大于1,则是
注意:主函数,传递参数时高度设置变量
cout<<"T是否AVL平衡树:"<<endl;
int height=0;
if(IsAvl(T,height))
cout << "是AVL平衡树";
else
cout << "不是AVL平衡树";
cout<<endl;
/**************************判断是不是AVL平衡树****************************/
bool IsAvl(BiTreeNode* &T,int &height)//还要判断左右子树高度是否差1
{
if(T == NULL)
{
height = 0;
return true;
}
int heightLeft;
bool resultLeft = IsAvl(T->lchild,heightLeft);
int heightRight;
bool resultRight = IsAvl(T->rchild,heightRight);
if(resultLeft && resultRight && abs(heightLeft - heightRight)<=1)//如果左右子树都为平衡树且高度差不大于1
{
height = max(heightLeft, heightRight)+1;
return true;
}
else
{
height = max(heightLeft, heightRight) + 1;
return false;
}
}
683

被折叠的 条评论
为什么被折叠?



