为什么我的代码用于检查BST树是否AVL错误?

时间:2018-05-30 22:59:36

标签: c binary-search-tree

您好我写了这段代码,检查给定的BST是否是AVL,在我看来它有效,但在测试中却没有。 该结构是一个经典的BST,左右都是整数。

bool AbsoluteDifference(int a, int b)
{
return ((a-b) <= 1)) ||(b-a) <= 1)); 
} 

 void aux_avl (BST b , int &l_var, bool &isAVL)
{
if (b == NULL)
  l_var--;  
  //In consecuence of not asking if the left or right is NULL or not
  //when I make the recursive call, if it is I have to decrease the value
else if (isAVL == true)//I don't want to keep checking if I already know that it's not AVL

{

   int l_left = l_var + 1;
   aux_avl(b->left,l_left,isAVL);

   int l_right = l_var +1;
   aux_avl(b->right, l_right,isAVL); 

   isAVL = AbsoluteDifference(l_left,l_right);

   lvar = max(l_left,l_right);
}
}

bool is_AVL(BST b)
{
   if (b == NULL)
      return true;
   else
  {
     int l_var =0;
     bool isAVL = true;
     auxavl(b,l_var,isAVL);

     return isAVL;
  }
}

0 个答案:

没有答案