alexjercan/bugnet huggingface dataset:
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

//
// Created by 李青坪 on 2018/10/23.
//

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int value;
  Node *parent, *left, *right;
};
Node *head, *NIL;

void insertNode(int value) {
  Node *x = head;
  Node *y = NIL; // 表示父节点
  while (x != NIL) {
    y = x;
    if (value < x->value) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  Node *z = new (Node);
  z->value = value;
  z->left = NIL;
  z->right = NIL;
  if (y != NIL) {
    if (z->value < y->value) {
      y->left = z;
    } else {
      y->right = z;
    }
    z->parent = y;
  } else {
    head = z;
  }
}

void inOrder(Node *x) {
  if (x != NIL) {
    inOrder(x->left);
    printf(" %d", x->value);
    inOrder(x->right);
  }
}

void preOrder(Node *x) {
  if (x != NIL) {
    printf(" %d", x->value);
    preOrder(x->left);
    preOrder(x->right);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  head = NIL;
  char command[10];
  int value;
  for (int i = 0; i < n; ++i) {
    scanf("%s", command);
    if (command[0] == 'i') {
      scanf("%d", &value);
      insertNode(value);
    } else {
      inOrder(head);
      printf("\\n");
      preOrder(head);
      printf("\\n");
    }
  }
  return 0;
}
/*
8
insert 30
insert 88
insert 12
insert 1
insert 20
insert 17
insert 25
print
 */


Error: WA

Fixed submission:

//
// Created by 李青坪 on 2018/10/23.
//

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

struct Node {
  int value;
  Node *parent, *left, *right;
};
Node *head;

void insertNode(int value) {
  Node *x = head;
  Node *y; // 表示父节点
  while (x != NULL) {
    y = x;
    if (value < x->value) {
      x = x->left;
    } else {
      x = x->right;
    }
  }
  Node *z = new (Node);
  z->value = value;
  z->left = NULL;
  z->right = NULL;
  if (y != NULL) {
    if (z->value < y->value) {
      y->left = z;
    } else {
      y->right = z;
    }
    z->parent = y;
  } else {
    head = z;
  }
}

void inOrder(Node *x) {
  if (x != NULL) {
    inOrder(x->left);
    printf(" %d", x->value);
    inOrder(x->right);
  }
}

void preOrder(Node *x) {
  if (x != NULL) {
    printf(" %d", x->value);
    preOrder(x->left);
    preOrder(x->right);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  head = NULL;
  char command[10];
  int value;
  for (int i = 0; i < n; ++i) {
    scanf("%s", command);
    if (command[0] == 'i') {
      scanf("%d", &value);
      insertNode(value);
    } else {
      inOrder(head);
      printf("\\n");
      preOrder(head);
      printf("\\n");
    }
  }
  return 0;
}
/*
8
insert 30
insert 88
insert 12
insert 1
insert 20
insert 17
insert 25
print
 */
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

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

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

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;
    }
  }
}

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);
}

int main() {
  int n, i, 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 == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }

  return 0;
}


Error: -11

Fixed submission:

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

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

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;
    }
  }

  x->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);
  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);
}

int main() {
  int n, i, 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 == "print") {
      inorder(root);
      printf("\\n");
      preorder(root);
      printf("\\n");
    }
  }

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

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

class node {
public:
  node *left;
  node *right;
  node *p;
  int key;
  node() {
    left = NULL;
    right = NULL;
    p = NULL;
  }
  node(int k) {
    left = NULL;
    right = NULL;
    p = NULL;
    key = k;
  }
};

node *root = NULL;
int a[500002];
int n = 0;

void insert(node *);
void print();
void preorder(node *);
void midorder(node *);

void insert(node *z) {
  node *y = NULL;
  node *x = root;
  while (x != NULL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->p = y;
  if (y == NULL) {
    // delete root;
    root = z;
    // cout<<"root:"<<root->key<<endl;
  } else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

void print() {
  midorder(root);
  cout << endl;
  preorder(root);
  cout << endl;
}

void midorder(node *i) {
  if (i == NULL)
    return;
  midorder(i->left);
  // if(i!=root)cout<<" "<<i->key;
  cout << " " << i->key;
  midorder(i->right);
}

void preorder(node *i) {
  if (i == NULL)
    return;
  // if(i!=root)cout<<" "<<i->key;
  cout << " " << i->key;
  preorder(i->left);
  preorder(i->right);
}

int main(void) {
  int q, k;
  string s;
  cin >> q;
  while (q--) {
    cin >> s;
    if (s[0] == 'i') {
      cin >> k;
      insert(new node(k));
    } else if (s[0] == 'p') {
      print();
    }
  }
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

class node {
public:
  node *left;
  node *right;
  node *p;
  int key;
  node() {
    left = NULL;
    right = NULL;
    p = NULL;
  }
  node(int k) {
    left = NULL;
    right = NULL;
    p = NULL;
    key = k;
  }
};

node *root;
int a[500002];
int n = 0;

int insert(node *);
void print();
void preorder(node *);
void midorder(node *);

int insert(node *z) {
  node *y;
  node *x = root;
  while (x != NULL) {
    y = x;
    if (z->key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  z->p = y;
  if (x == NULL) {
    delete root;
    root = z;
  } else if (z->key < y->key)
    y->left = z;
  else
    y->right = z;
}

void print() {
  midorder(root);
  cout << endl;
  preorder(root);
  cout << endl;
}

void midorder(node *i) {
  if (i == NULL)
    return;
  midorder(i->left);
  if (i != root)
    cout << " " << i->key;
  midorder(i->right);
}

void preorder(node *i) {
  if (i == NULL)
    return;
  if (i != root)
    cout << " " << i->key;
  preorder(i->left);
  preorder(i->right);
}

int main(void) {
  int q, k;
  string s;
  cin >> q;
  node r;
  root = &r;
  while (q--) {
    cin >> s;
    if (s[0] == 'i') {
      cin >> k;
      insert(new node(k));
    } else if (s[0] == 'p') {
      print();
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

Node *NIL, *root;

void printPreOrder(Node *u);
void printInOrder(Node *u);
void insertTree(int k);

int main() {
  int i, j, n, k;
  char com[10];
  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> k;
      insertTree(k);
    } else {
      printInOrder(root);
      cout << endl;
      printPreOrder(root);
      cout << endl;
    }
  }
  return 0;
}

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

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

void insertTree(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;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX 500
#define NIL -1
using namespace std;

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

void printPreOrder(Node node[], int parent);
void printInOrder(Node node[], int parent);
void insertTree(Node node[], int z, int &root);

int main() {
  Node node[MAX];
  int z, i, j, n, id, root, left;
  char com[10];
  cin >> n;
  for (i = 0; i < MAX; i++) {
    node[i].parent = NIL;
    node[i].left = NIL;
    node[i].right = NIL;
  }

  root = NIL;
  for (i = 0; i < n; i++) {
    cin >> com;
    if (com[0] == 'i') {
      cin >> z;
      insertTree(node, z, root);
    } else {
      printInOrder(node, root);
      cout << endl;
      printPreOrder(node, root);
      cout << endl;
    }
  }
  return 0;
}

void printPreOrder(Node node[], int parent) {
  cout << " " << parent;
  if (node[parent].left != NIL)
    printPreOrder(node, node[parent].left);
  if (node[parent].right != NIL)
    printPreOrder(node, node[parent].right);
}

void printInOrder(Node node[], int parent) {
  if (node[parent].left != NIL)
    printInOrder(node, node[parent].left);
  cout << " " << parent;
  if (node[parent].right != NIL)
    printInOrder(node, node[parent].right);
}

void insertTree(Node node[], int z, int &root) {
  int x, y;
  y = NIL;
  x = root;
  while (x != NIL) {
    y = x;
    if (z < x)
      x = node[x].left;
    else
      x = node[x].right;
  }
  node[z].parent = y;
  if (y == NIL)
    root = z;
  else if (z < y)
    node[y].left = z;
  else
    node[y].right = z;

  if (node[root].parent != NIL)
    root = node[root].parent;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

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

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

node *root;

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] == '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 {
public:
  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 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[0] == 'i') {
      cin >> num;
      insert(num);
    } else if (root != NIL) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
  int key;
  struct node *left, *right;
};

struct node *newNode(int item) {
  struct node *temp = (struct node *)malloc(sizeof(struct node));
  temp->key = item;
  temp->left = temp->right = NULL;
  return temp;
}

void Preorder(struct node *root) {
  static int hit = 0;

  if (root == NULL)
    return;

  printf(" ");
  printf("%d", root->key);
  hit = 1;
  Preorder(root->left);

  Preorder(root->right);
}

void inorder(struct node *root) {
  static int h = 0;

  if (root != NULL) {

    inorder(root->left);

    printf(" ");
    printf("%d", root->key);
    h = 1;
    inorder(root->right);
  }
}

struct node *insert(struct node *node, int key) {

  if (node == NULL)
    return newNode(key);

  if (key < node->key)
    node->left = insert(node->left, key);
  else if (key > node->key)
    node->right = insert(node->right, key);

  return node;
}

int main() {
  int z, n, i;
  char str[7];

  struct node *root = NULL;
  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s", str);
    if (strcmp(str, "insert") == 0)
      scanf("%d", &z);
    if (i == 0)
      root = insert(root, z);
    else
      insert(root, z);

    if (strcmp(str, "print") == 0) {

      inorder(root);
      printf("\\n");
      Preorder(root);
      printf("\\n");
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
  int key;
  struct node *left, *right;
};

struct node *newNode(int item) {
  struct node *temp = (struct node *)malloc(sizeof(struct node));
  temp->key = item;
  temp->left = temp->right = NULL;
  return temp;
}

void Preorder(struct node *root) {
  static int hit = 0;

  if (root == NULL)
    return;

  printf(" ");
  printf("%d", root->key);
  hit = 1;
  Preorder(root->left);

  Preorder(root->right);
}

void inorder(struct node *root) {
  static int h = 0;

  if (root != NULL) {

    inorder(root->left);

    printf(" ");
    printf("%d", root->key);
    h = 1;
    inorder(root->right);
  }
}

struct node *insert(struct node *node, int key) {

  if (node == NULL)
    return newNode(key);

  if (key < node->key)
    node->left = insert(node->left, key);
  else if (key > node->key)
    node->right = insert(node->right, key);

  return node;
}

int main() {
  int z, n, i;
  char str1[7], str2[6];

  struct node *root = NULL;
  scanf("%d", &n);
  if (n > 10)
    exit(8);
  for (i = 0; i < n - 1; i++) {
    scanf("%s", str1);
    if (strcmp(str1, "insert") == 0)
      scanf("%d", &z);
    if (i == 0)
      root = insert(root, z);
    else
      insert(root, z);
  }
  scanf("%s", str2);
  if (strcmp(str2, "print") == 0) {

    inorder(root);
    printf("\\n");
    Preorder(root);
    printf("\\n");
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
using namespace std;
typedef long long int ll;

struct node {
  int key;
  node *left, *right;
};

node *NIL = (node *)malloc(sizeof(node));
node *root;
int k;

void insert(node **focus) {
  if (*focus == NIL) {
    node *buf = (node *)malloc(sizeof(node));
    buf->key = k;
    buf->left = NIL;
    buf->right = NIL;
    *focus = buf;
  } else if (k < (*focus)->key)
    insert(&((*focus)->left));
  else
    insert(&((*focus)->right));
}
void print_in(node *focus) {
  if (focus == NIL)
    return;
  print_in(focus->left);
  printf(" %d", focus->key);
  print_in(focus->right);
}

void print_pre(node *focus) {
  if (focus == NIL)
    return;
  printf(" %d", focus->key);
  print_pre(focus->left);
  print_pre(focus->right);
}

void print() {
  print_in(root);
  printf("\\n");
  print_pre(root);
  printf("\\n");
}

int main() {
  int m;
  root = NIL;
  cin >> m;
  string query;
  Loop(i, m) {
    cin >> query;
    if (query == "insert") {
      scanf("%d", &k);
      insert(&root);
    } else
      print();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i < (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
using namespace std;
typedef long long int ll;

struct node {
  int left, right;
};

map<int, node> T;
int root;

void insert(int k, int focus) {
  if (k < focus) {
    if (T[focus].left == -1)
      T[focus].left = k;
    else
      insert(k, T[focus].left);
  } else {
    if (T[focus].right == -1)
      T[focus].right = k;
    else
      insert(k, T[focus].right);
  }
}

void print_in(int focus) {
  if (focus != -1) {
    print_in(T[focus].left);
    printf(" %d", focus);
    print_in(T[focus].right);
  }
}

void print_pre(int focus) {
  if (focus != -1) {
    printf(" %d", focus);
    print_pre(T[focus].left);
    print_pre(T[focus].right);
  }
}

void print() {
  print_in(root);
  printf("\\n");
  print_pre(root);
  printf("\\n");
}

int main() {
  int m;
  cin >> m;
  char query[10];
  int k;
  scanf("%s %d", query, &k);
  root = k;
  T[k] = {
      -1,
      -1,
  };
  Loop1(i, m) {
    scanf("%s", query);
    if (query[0] == 'i') {
      scanf("%d", &k);
      T[k] = {-1, -1};
      insert(k, root);
    } else
      print();
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

/*
 * ALDS1_8_A.cpp
 *
 *  Created on: May 2, 2018
 *      Author: 13743
 */

#include <cstdio>
#include <cstdlib>

struct Node {
  int key;
  Node *pa, *lc, *rc;
};

Node *root = NULL;

void insert(int k) {
  Node *y = NULL;
  Node *x = root;
  Node *z = new Node;
  while (x != NULL) {
    y = x;
    if (k < y->key)
      x = y->lc;
    else
      x = y->rc;
  }
  z->pa = y;
  z->lc = NULL;
  z->rc = NULL;
  z->key = k;
  if (y == NULL) {
    root = z;
  } else if (k < y->key) {
    y->lc = z;
  } else {
    y->rc = z;
  }
}

void preorder(Node *u) {
  if (u == NULL)
    return;
  printf(" %d", u->key);
  preorder(u->lc);
  preorder(u->rc);
}

void inorder(Node *u) {
  if (u == NULL)
    return;
  inorder(u->lc);
  printf(" %d", u->key);
  inorder(u->rc);
}

void print() {
  inorder(root);
  printf("\\n");
  preorder(root);
  printf("\\n");
}

int main() {
  int m;
  scanf("%d", &m);
  char s[100];
  int ke;
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &ke);
      insert(ke);
    } else {
      print();
    }
  }
}


Error: -11

Fixed submission:

/*
 * ALDS1_8_A.cpp
 *
 *  Created on: May 2, 2018
 *      Author: 13743
 */

#include <cstdio>
#include <cstdlib>

struct Node {
  int key;
  Node *pa, *lc, *rc;
};

Node *root = NULL;

void insert(int k) {
  Node *y = NULL;
  Node *x = root;
  Node *z = new Node;
  while (x != NULL) {
    y = x;
    if (k < y->key)
      x = y->lc;
    else
      x = y->rc;
  }
  z->pa = y;
  z->lc = NULL;
  z->rc = NULL;
  z->key = k;
  if (y == NULL) {
    root = z;
  } else if (k < y->key) {
    y->lc = z;
  } else {
    y->rc = z;
  }
}

void preorder(Node *u) {
  if (u == NULL)
    return;
  printf(" %d", u->key);
  preorder(u->lc);
  preorder(u->rc);
}

void inorder(Node *u) {
  if (u == NULL)
    return;
  inorder(u->lc);
  printf(" %d", u->key);
  inorder(u->rc);
}

void print() {
  inorder(root);
  printf("\\n");
  preorder(root);
  printf("\\n");
}

int main() {
  int m;
  scanf("%d", &m);
  char *s;
  int ke;
  for (int i = 0; i < m; i++) {
    scanf("%s", s);
    if (s[0] == 'i') {
      scanf("%d", &ke);
      insert(ke);
    } else {
      print();
    }
  }
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

#define N 500000
int key[N];
int rt[N];
int lt[N];
int parent[N];
int np = 0;
int root = -1;

void insert(int k) {
  int y = -1;
  int x = root;
  while (x != -1) {
    y = x;
    if (k < key[x]) {
      x = lt[x];
    } else {
      x = rt[x];
    }
  }
  if (y == -1) {
    root = np;
  } else {
    if (k < key[y]) {
      lt[y] = np;
    } else {
      rt[y] = np;
    }
  }
  key[np] = k;
  parent[np] = y;
  np++;
}

void saiki1(int x) {
  if (lt[x] != -1)
    saiki1(lt[x]);
  cout << " " << key[x];
  if (rt[x] != -1)
    saiki1(rt[x]);
}

void saiki2(int x) {
  cout << " " << key[x];
  if (lt[x] != -1)
    saiki2(lt[x]);
  if (rt[x] != -1)
    saiki2(rt[x]);
}
void print() {
  saiki1(root);
  cout << endl;
  saiki2(root);
  cout << endl;
}

int main() {

  for (int i = 0; i < N; i++) {
    key[i] = -1;
    parent[i] = -1;
    lt[i] = -1;
    rt[i] = -1;
  }

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    //		cout << i << endl;
    string s;
    cin >> s;
    if (s == "print") {
      print();
    } else if (s == "insert") {
      int k;
      cin >> k;
      insert(k);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

#define N 100000
int key[N];
int rt[N];
int lt[N];
int parent[N];
int np = 0;
int root = -1;

void insert(int k) {
  int y = -1;
  int x = root;
  while (x != -1) {
    y = x;
    if (k < key[x]) {
      x = lt[x];
    } else {
      x = rt[x];
    }
  }
  if (y == -1) {
    root = np;
  } else {
    if (k < key[y]) {
      lt[y] = np;
    } else {
      rt[y] = np;
    }
  }
  key[np] = k;
  parent[np] = y;
  np++;
}

void saiki1(int x) {
  if (lt[x] != -1)
    saiki1(lt[x]);
  cout << " " << key[x];
  if (rt[x] != -1)
    saiki1(rt[x]);
}

void saiki2(int x) {
  cout << " " << key[x];
  if (lt[x] != -1)
    saiki2(lt[x]);
  if (rt[x] != -1)
    saiki2(rt[x]);
}
void print() {
  saiki1(root);
  cout << endl;
  saiki2(root);
  cout << endl;
}

int main() {

  for (int i = 0; i < N; i++) {
    key[i] = -1;
    parent[i] = -1;
    lt[i] = -1;
    rt[i] = -1;
  }

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    //		cout << i << endl;
    string s;
    cin >> s;
    if (s == "print") {
      print();
    } else if (s == "insert") {
      int k;
      cin >> k;
      insert(k);
    }
  }

  return 0;
}