0

I submitted a problem about AVL to DOMJUDGE, the result is correct and runtime 0 sec but its RUN-ERROR.

This is my program looks like, could you help me find the problem? It's about sum the parent of the value with sibling of the value parent, for example if the value is 17 and 17 parent is 29 and 29 has 15 as it siblings so it will sum 29+15.

int height(AVLNode *root)
{
      if (root == NULL)
            return 0;
      else
      {
            int lheight = height(root->left);
            int rheight = height(root->right);
            if (lheight > rheight)
                  return (lheight + 1);
            else
                  return (rheight + 1);
      }
}

bool CurrentLevel(AVLNode *root, int level, int val)
{
      if (root == NULL)
            return false;

      if (level == 1)
      {
            if (root->data == val)
            {
                  return true;
            }
      }

      else if (level > 1)
      {
            bool left = CurrentLevel(root->left, level - 1, val);
            bool right = CurrentLevel(root->right, level - 1, val);
            return left || right;
      }
      return false;
}

AVLNode *findGrandparent(AVLNode *root, int val)
{
      if (root == NULL || (root->left == NULL && root->right == NULL))
      {
            return NULL;
      }
      else if ((root->left != NULL && (root->left->left != NULL || root->left->right != NULL) && (root->left->left->data == val || root->left->right->data == val)) || (root->right != NULL && (root->right->left != NULL || root->right->right != NULL) && (root->right->left->data == val || root->right->right->data == val)))
      {
            return root;
      }
      else
      {
            AVLNode *left = findGrandparent(root->left, val);
            if (left != NULL)
            {
                  return left;
            }
            else
            {
                  AVLNode *right = findGrandparent(root->right, val);
                  if (right != NULL)
                  {
                        return right;
                  }
                  else
                  {
                        return NULL;
                  }
            }
      }
}

int count_parent(AVLNode *root, int level, int val)
{
      if (root == NULL || level < 2)
      {
            return 0;
      }

      if (level == 2)
      {
            if (root->left != NULL && root->left->data == val)
            {
                  return root->data;
            }
            else if (root->right != NULL && root->right->data == val)
            {
                  return root->data;
            }
      }

      if (level >= 3)
      {
            AVLNode *grandparent = findGrandparent(root, val);
            if (grandparent != NULL)
            {
                  int left_child = (grandparent->left != NULL) ? grandparent->left->data : 0;
                  int right_child = (grandparent->right != NULL) ? grandparent->right->data : 0;
                  return left_child + right_child;
            }
      }

      int left_sum = count_parent(root->left, level - 1, val);
      int right_sum = count_parent(root->right, level - 1, val);
      return left_sum + right_sum;
}


int LOT(AVL *root, int value)
{
      int h = height(root->_root);
      int i;
      for (i = 1; i <= h; i++)
      {
            if (CurrentLevel(root->_root, i, value))
            {
                 return count_parent(root->_root, i, value);
            }
      }

      return 0;
}

int main()
{
      AVL avlku;
      avl_init(&avlku);

      int n,m,val,srch,x;

      scanf("%d", &n);
      scanf("%d", &m);

      int i;
      for (i = 0; i < n; i++)
      {
            scanf("%d", &val);
            avl_insert(&avlku, val);
      }

      for (i = 0; i < m; i++)
      {
            scanf("%d", &srch);
            x = LOT(&avlku, srch);
            printf("%d\n", x);
      }

      return 0;

}
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
  • 1
    Please choose a language, either C or C++, not both. – PaulMcKenzie May 07 '23 at 02:17
  • im sorry, it should be c. do you see any problem that makes my program run-error? – Fauzan Putra S May 07 '23 at 02:36
  • 1
    Get rid of the `scanf` calls and actually populate the variables with hard-coded values that duplicate the error. This way others can simply take your code, compile it, run it, and see the error for themselves. For example `n = 10; m = 2;` or whatever the data is. Also what [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) have you done? – PaulMcKenzie May 07 '23 at 02:38

0 Answers0