alexjercan/bugnet huggingface dataset:
Problem p02284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

struct Node {
  long key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(long k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = new Node;
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if (y == NIL)
    root = z;
  else {
    if (z->key < y->key)
      y->left = z;
    else
      y->right = z;
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  cout << " " << u->key;
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  cout << " " << u->key;
  preorder(u->left);
  preorder(u->right);
}

long find(Node *u, long x) {
  if (u == NIL)
    return 0;
  if (u->key == x)
    return 1;
  else if (u->key > x)
    return find(u->left, x);
  else
    return find(u->right, x);
}

int main() {
  long n, x;
  string com;
  cin >> n;
  for (long i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      cin >> x;
      insert(x);
    } else if (com == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    } else {
      cin >> x;
      if (find(root, x))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

struct Node {
  long key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(long k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = new Node;
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->parent = y;

  if (y == NIL)
    root = z;
  else {
    if (z->key < y->key)
      y->left = z;
    else
      y->right = z;
  }
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  cout << " " << u->key;
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  cout << " " << u->key;
  preorder(u->left);
  preorder(u->right);
}

long find(Node *u, long x) {
  if (u == NIL)
    return 0;
  if (u->key == x)
    return 1;
  else
    return find(u->left, x) || find(u->right, x);
}

int main() {
  long n, x;
  string com;
  cin >> n;
  for (long i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      cin >> x;
      insert(x);
    } else if (com == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    } else {
      cin >> x;
      if (find(root, x))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <memory>
#include <stdio.h>
#include <string.h>
using namespace std;

typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, *root;

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preorder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preorder(u->left);
  preorder(u->right);
}

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = new Node;
  z->key = k;
  z->left = NIL;
  z->right = NIL;
  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL)
    root = z;
  else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

bool Find(int k) {
  Node *x = root;
  while (x != NIL) {
    if (x->key == k)
      return true;
    if (x->key > k) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  return false;
}

int main() {
  int i, n, x;
  string com;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "find") {
      scanf("%d", &x);
      if (Find(x))
        printf("yes\\n");
      else
        printf("no\\n");
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <memory>
#include <stdio.h>
#include <string.h>
#define MAX 500000
using namespace std;

typedef struct Node {
  int key;
  Node *parent, *left, *right;
} Node;

Node *NIL, *root;

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preoder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preoder(u->left);
  preoder(u->right);
}

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = new Node;
  z->key = k;
  z->left = NIL;
  z->right = NIL;
  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL)
    root = z;
  else {
    if (z->key < x->key) {
      x->left = z;
    } else {
      x->right = z;
    }
  }
}

bool Find(int k) {
  Node *x = root;
  while (x != NIL) {
    if (x->key == k)
      return true;
    if (x->key > k) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  return false;
}

int main() {
  int i, n, x;
  string com;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      scanf("%d", &x);
      insert(x);
    } else if (com == "find") {
      scanf("%d", &x);
      if (Find(x))
        printf("yes\\n");
      else
        printf("no\\n");
    } else if (com == "print") {
      inorder(root);
      printf("\\n");
      preoder(root);
      printf("\\n");
    }
  }

  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

struct Node {
  int key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

bool find(int k, Node *u) {
  if (u == NIL)
    return false;
  if (u->key == k)
    return true;
  return (u->key > k) ? find(k, u->left) : find(k, u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preoder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preoder(u->left);
  preoder(u->right);
}

int main() {
  int n;
  string s;

  cin >> n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    // scanf("%s", s);
    cin >> s;
    if (s == "print") {
      inorder(root);
      printf("\\n");
      preoder(root);
      printf("\\n");
    } else if (s == "insert") {
      int x;
      scanf("%d", &x);
      insert(x);
    } else if (s == "find") {
      int k;
      scanf("%d", &k);
      printf(("%s", find(k, root) ? "yes" : "no"));
      printf("\\n");
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

struct Node {
  int key;
  Node *parent, *left, *right;
};

Node *root, *NIL;

void insert(int k) {
  Node *y = NIL;
  Node *x = root;
  Node *z;

  z = (Node *)malloc(sizeof(Node));
  z->key = k;
  z->left = NIL;
  z->right = NIL;

  while (x != NIL) {
    y = x;
    if (z->key < x->key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  z->parent = y;
  if (y == NIL) {
    root = z;
  } else {
    if (z->key < y->key) {
      y->left = z;
    } else {
      y->right = z;
    }
  }
}

bool find(int k, Node *u) {
  if (u == NIL)
    return false;
  if (u->key == k)
    return true;
  return find(k, u->left) || find(k, u->right);
}

void inorder(Node *u) {
  if (u == NIL)
    return;
  inorder(u->left);
  printf(" %d", u->key);
  inorder(u->right);
}

void preoder(Node *u) {
  if (u == NIL)
    return;
  printf(" %d", u->key);
  preoder(u->left);
  preoder(u->right);
}

int main() {
  int n;
  string s;

  cin >> n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    // scanf("%s", s);
    cin >> s;
    if (s == "print") {
      inorder(root);
      printf("\\n");
      preoder(root);
      printf("\\n");
    } else if (s == "insert") {
      int x;
      scanf("%d", &x);
      insert(x);
    } else if (s == "find") {
      int k;
      scanf("%d", &k);
      printf(("%s", find(k, root) ? "yes" : "no"));
      printf("\\n");
    }
  }

  return 0;
}
Problem p02284 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

struct node {
  int id;
  node *right, *left, *parent;
};

node *root;

bool find(int key) {
  node *cur = root;
  while (cur != NULL && cur->id != key) {
    if (cur->id > key)
      cur = cur->left;
    else
      cur = cur->right;
  }
  if (cur == NULL)
    return false;
  else
    return true;
}

void insert(int k) {
  node *y = NULL, *x = root, *z;
  z = (node *)malloc(sizeof(node));
  z->id = k;
  z->left = NULL;
  z->right = NULL;
  while (x != NULL) {
    y = x;
    if (z->id > x->id)
      x = x->right;
    else
      x = x->left;
  }
  z->parent = y;
  if (y == NULL)
    root = z;
  else {
    if (z->id > y->id)
      y->right = z;
    else
      y->left = z;
  }
}

void in(node *cur) {
  if (cur == NULL)
    return;
  in(cur->left);
  printf(" %d", cur->id);
  in(cur->right);
}
void pre(node *cur) {
  if (cur == NULL)
    return;
  printf(" %d", cur->id);
  pre(cur->left);
  pre(cur->right);
}

int main() {
  int n;
  cin >> n;
  string command;
  int v;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command[0] == 'i') {
      cin >> v;
      insert(v);
    } else if (command[0] == 'f') {
      cin >> v;
      if (find(v))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else if (command[0] == 'p') {
      in(root);
      cout << endl;
      pre(root);
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#define NIL NULL
struct tree {
  int id;
  tree *left, *right;
};
tree *root;

void insert(int key) {
  tree *x, *y, *z;
  y = NIL;
  x = root;
  z = (tree *)malloc(sizeof(tree));
  z->id = key;
  while (x != NIL) {
    y = x;
    if (z->id < x->id)
      x = x->left;
    else
      x = x->right;
  }
  if (y == NIL)
    root = z;
  else if ((z->id) < (y->id))
    y->left = z;
  else
    y->right = z;
}

void find(int key) {
  tree *x = root;
  while (x != NIL) {
    if (x->id == key) {
      printf("yes\\n");
      return;
    } else if (x->id > key) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  printf("no\\n");
  return;
}

void inorder(tree *a) {
  if (a->left != NIL)
    inorder(a->left);
  printf(" %d", a->id);
  if (a->right != NIL)
    inorder(a->right);
}
void preorder(tree *a) {
  printf(" %d", a->id);
  if (a->left != NIL)
    preorder(a->left);
  if (a->right != NIL)
    preorder(a->right);
}

int main() {
  int m;
  cin >> m;
  string com;
  int num;
  root = NIL;
  for (int i = 0; i < m; i++) {
    cin >> com;
    if (com == "insert") {
      cin >> num;
      insert(num);
    } else if (com == "find") {
      cin >> num;
      find(num);
    } else if (com == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int a[251];
int parent(int i) { return i / 2; }
int left(int i) { return i * 2; }
int right(int i) { return i * 2 + 1; }

int main() {

  int h;
  cin >> h;

  for (int i = 1; i <= h; i++) {
    scanf("%d", &a[i]);
  }

  for (int i = 1; i <= h; i++) {
    printf("node %d: key = %d, ", i, a[i]);

    if (parent(i) > 0) {
      printf("parent key = %d, ", a[parent(i)]);
    }
    if (left(i) <= h) {
      printf("left key = %d, ", a[left(i)]);
    }
    if (right(i) <= h) {
      printf("right key = %d, ", a[right(i)]);
    }

    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

struct Node {
  int key, parent, left, right;
};

struct Node a[251];

int main() {

  for (int i = 0; i <= 250; i++) {
    a[i].key = 2000000001;
  }

  int h;
  cin >> h;

  for (int i = 1; i <= h; i++) {
    scanf("%d", &a[i].key);
    a[i].parent = i / 2;
    a[i].left = i * 2;
    a[i].right = (i * 2) + 1;
  }

  for (int i = 1; i <= h; i++) {
    printf("node %d: key = %d, ", i, a[i].key);

    if (a[i].parent != 0) {
      printf("parent key = %d, ", a[a[i].parent].key);
    }
    if (a[a[i].left].key != 2000000001) {
      printf("left key = %d, ", a[a[i].left].key);
    }
    if (a[a[i].right].key != 2000000001) {
      printf("right key = %d, ", a[a[i].right].key);
    }

    cout << endl;
  }

  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
const int maxn = 2 * 300;
int n;

int a[maxn];
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }

  for (int i = 1; i <= n; i++) {
    printf("node %d: key = %d, ", i, a[i]);
    if (i != 1)
      printf("parent key = %d, ", a[i / 2]);
    if (2 * i <= n)
      printf("left key = %d, ", a[2 * i]);
    if (2 * i + 1 <= n)
      printf("right key = %d, ", a[2 * i + 1]);
    printf("\\n");
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
const int maxn = 300;
int n;
struct Node {
  int key;
  int left;
  int right;
};
Node node[maxn];
int a[maxn];
int p[maxn];
void make_tree(int i) {
  if (a[i] == -1)
    return;
  int left = 2 * i + 1;
  int right = 2 * i + 2;
  p[left] = i;
  p[right] = i;
  node[i].key = a[i];
  node[i].left = a[left];
  node[i].right = a[right];
  make_tree(left);
  make_tree(right);
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  memset(a, -1, sizeof(a));
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    p[i] = -1;
    node[i].left = -1;
    node[i].right = -1;
  }
  make_tree(0);
  for (int i = 0; i < n; i++) {
    printf("node %d: key = %d, ", i + 1, node[i].key);
    if (p[i] != -1)
      printf("parent key = %d, ", node[p[i]].key);
    if (node[i].left != -1)
      printf("left key = %d, ", node[i].left);
    if (node[i].right != -1)
      printf("right key = %d, ", node[i].right);
    printf("\\n");
  }
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[100000];

void heap(Node A[], int H) {
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[251];

void heap(Node A[], int H) {
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX 100000

using namespace std;

int h;

void print(int i, int h, int *A) {
  cout << "node " << i << ": ";
  cout << "key = " << A[i] << ", ";
  if (i > 1)
    cout << "parent key = " << A[i / 2] << ", ";
  if (2 * i <= h)
    cout << "left key = " << A[2 * i] << ", ";
  if (2 * i + 1 <= h)
    cout << "right key = " << A[2 * i + 1] << ", ";
}

int main() {
  int i, A[MAX + 1];
  cin >> h;
  for (i = 1; i <= h; i++)
    cin >> A[i];
  for (i = 1; i <= h; i++) {
    print(i, h, A);
    cout << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#define MAX 250
#define NIL -1

using namespace std;

void print(int i, int *A) {
  cout << "node " << i << ": ";
  cout << "key = " << A[i] << ", ";
  if (i != 1)
    cout << "parent key = " << A[i / 2] << ", ";
  if (A[2 * i] != NIL)
    cout << "left key = " << A[2 * i] << ", ";
  if (A[2 * i + 1] != NIL)
    cout << "right key = " << A[2 * i + 1] << ", ";
}

int main() {
  int h, i, A[MAX];
  cin >> h;
  for (i = 1; i <= 2 * h + 1; i++)
    A[i] = NIL;
  for (i = 1; i <= h; i++)
    cin >> A[i];
  for (i = 1; i <= h; i++) {
    print(i, A);
    cout << endl;
  }
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

typedef long long signed int ll;

ll a[510];
int h;

void rk(int n) {
  if (a[n * 2 + 1])
    cout << "right key = " << a[n * 2 + 1] << ", ";
}
void lk(int n) {
  if (a[n * 2])
    cout << "left key = " << a[n * 2] << ", ";
}
void pk(int n) {
  if (a[n / 2])
    cout << "parent key = " << a[n / 2] << ", ";
}

void print(int n) {
  cout << "node " << n << ": key = " << a[n] << ", ";
  pk(n);
  lk(n);
  rk(n);
  cout << endl;
}

int main() {
  cin >> h;
  for (int i = 1; i <= h; i++)
    cin >> a[i];
  for (int i = 1; i <= h; i++)
    print(i);
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>

using namespace std;

typedef long long signed int ll;

ll a[251];
int h;

void rk(int n) {
  if (a[n * 2 + 1])
    cout << "right key = " << a[n * 2 + 1] << ", ";
}
void lk(int n) {
  if (a[n * 2])
    cout << "left key = " << a[n * 2] << ", ";
}
void pk(int n) {
  if (a[n / 2])
    cout << "parent key = " << a[n / 2] << ", ";
}

void print(int n) {
  cout << "node " << n << ": key = " << a[n] << ", ";
  pk(n);
  lk(n);
  rk(n);
  cout << endl;
}

int main() {
  cin >> h;
  for (int i = 1; i <= h; i++)
    cin >> a[i];
  for (int i = 1; i <= h; i++)
    print(i);
  return 0;
}
Problem p02287 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[100000];

void heap(Node A[], int H) {
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct Node {
  int key;
  int id, parent, left, right;
};

Node A[300];

void heap(Node A[], int H) {
  // for(int i=1;i<=299;i++){
  // A[i].parent=0;
  // A[i].left=0;
  // A[i].right=0;
  // }
  for (int i = 1; i <= H; i++) {
    if (i == 1) {
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    } else {
      A[i].parent = A[i / 2].key;
      A[i].left = A[2 * i].key;
      A[i].right = A[2 * i + 1].key;
    }
  }
}

int main() {
  int H = 0;
  cin >> H;
  for (int i = 1; i <= H; i++) {
    cin >> A[i].key;
    A[i].id = i;
  }
  heap(A, H);
  for (int i = 1; i <= H; i++) {
    cout << "node " << A[i].id << ": key = " << A[i].key << ", ";
    if (i == 1) {
      cout << "left key = " << A[i].left << ", right key = " << A[i].right
           << ", " << endl;
    } else {
      if (A[i].left == 0 && A[i].right != 0) {
        cout << "parent key = " << A[i].parent << ", right key = " << A[i].right
             << ", " << endl;
      } else if (A[i].left != 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", " << endl;
      } else if (A[i].left == 0 && A[i].right == 0) {
        cout << "parent key = " << A[i].parent << ", " << endl;
      } else {
        cout << "parent key = " << A[i].parent << ", left key = " << A[i].left
             << ", right key = " << A[i].right << ", " << endl;
      }
    }
  }
  return 0;
}