alexjercan/bugnet huggingface dataset:
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;
    }
  }

  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;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct d {
  ll n, r, l;
} d;
const ll INF = 100000000000000000;
d T[1000001];
int N, gp = 1;

void pre(int n) {
  if (n == -1)
    return;
  printf(" %d", T[n].n);
  if (T[n].l != -1)
    pre(T[n].l);
  if (T[n].r != -1)
    pre(T[n].r);
  return;
}

void ino(int n) {
  if (n == -1)
    return;
  if (T[n].l != -1)
    ino(T[n].l);
  printf(" %d", T[n].n);
  if (T[n].r != -1)
    ino(T[n].r);
  return;
}

void ins(int p, int np = 0) {
  if (T[np].n == INF)
    T[np].n = p;
  else {
    if (T[np].n > p) {
      if (T[np].l == -1)
        T[np].l = gp++;
      ins(p, T[np].l);
    } else {
      if (T[np].r == -1)
        T[np].r = gp++;
      ins(p, T[np].r);
    }
  }
}

void p() {
  ino(0);
  puts("");
  pre(0);
  puts("");
}

int main() {
  for (int i = 0; i < 1000001; i++) {
    T[i].n = INF;
    T[i].r = T[i].l = -1;
  }
  scanf("%d\\n", &N);
  for (int i = 0; i < N; i++) {
    char s[20];
    int a;
    scanf("%s%d\\n", s, &a);
    if (s[0] == 'i')
      ins(a);
    else
      p();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 100000000000000000;
ll T[9000001], N;

void pre(ll n) {
  if (T[n] == INF)
    return;
  printf(" %lld", T[n]);
  pre(2 * n + 1);
  pre(2 * n + 2);
  return;
}

void ino(ll n) {
  if (T[n] == INF)
    return;
  ino(2 * n + 1);
  printf(" %lld", T[n]);
  ino(2 * n + 2);
  return;
}

void ins(int p, int np = 0) {
  if (T[np] == INF)
    T[np] = p;
  else {
    if (T[np] > p)
      ins(p, 2 * np + 1);
    else
      ins(p, 2 * np + 2);
  }
}

void p() {
  ino(0);
  puts("");
  pre(0);
  puts("");
}

int main() {
  for (int i = 0; i < 5000001; i++)
    T[i] = INF;
  scanf("%d\\n", &N);
  for (int i = 0; i < N; i++) {
    char s[20];
    int a;
    scanf("%s%d\\n", s, &a);
    if (s[0] == 'i')
      ins(a);
    else
      p();
  }
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
struct Node {
  int value;
  Node *left;
  Node *right;
};

struct Node *NIL;

class BinarySearchTree {
private:
  Node *root;

public:
  BinarySearchTree() { root = NIL; }
  void insert(int value) {
    Node *node = (Node *)malloc(sizeof(Node));
    node->value = value;
    node->right = NIL;
    node->left = NIL;
    Node *x = root;
    Node *y = NIL;
    while (x != NIL) {
      y = x;
      if (node->value < x->value) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NIL)
      root = node;
    else if (node->value < y->value)
      y->left = node;
    else
      y->right = node;
  }
  void print() {
    inorderWalk(root);
    cout << endl;
    preorderWalk(root);
    cout << endl;
  }
  void inorderWalk(Node *node) {
    if (node == NIL)
      return;
    inorderWalk(node->left);
    cout << ' ' << node->value;
    inorderWalk(node->right);
  }
  void preorderWalk(Node *node) {
    if (node == NIL)
      return;
    cout << ' ' << node->value;
    preorderWalk(node->left);
    preorderWalk(node->right);
  }
};

int main() {
  int n;
  cin >> n;
  string com;
  BinarySearchTree T;
  for (int i = 0; i < n; i++) {
    cin >> com;
    if (com == "insert") {
      int num;
      cin >> num;
      T.insert(num);
    } else if (com == "print") {
      T.print();
    }
  }
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#define NIL -1

using namespace std;

int x = NIL, root_id = NIL;
struct Node {
  int parent = -1, left = -1, right = -1;
};

Node T[500009];

void inorder(int id) {
  if (id == -1)
    return;
  inorder(T[id].left);
  cout << " " << id;
  inorder(T[id].right);
}

void preorder(int id) {
  if (id == -1)
    return;
  cout << " " << id;
  preorder(T[id].left);
  preorder(T[id].right);
}

void print(int root_id) {
  inorder(root_id);
  cout << endl;
  preorder(root_id);
  cout << endl;
}

void insert(int z) {
  int y = NIL;
  x = root_id;
  while (x != NIL) {
    y = x;
    if (z < x)
      x = T[x].left;
    else
      x = T[x].right;
  }
  T[z].parent = y;

  if (y == NIL)
    root_id = z;
  else if (z < y)
    T[y].left = z;
  else
    T[y].right = z;
}

int main() {
  int n, ins_num;
  string ope;
  cin >> n;
  for (int i = 0; i <= n - 1; i++) {
    cin >> ope;
    if (ope == "print")
      print(root_id);
    else if (ope == "insert") {
      cin >> ins_num;
      insert(ins_num);
    }
  }
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

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

struct node {
  int lc;
  int rc;
  int key;
  void init(int a) {
    lc = rc = -1;
    key = a;
  }
};

#define bstN 600000
struct bst {

  int size;
  node t[bstN];

  void init() { size = 0; }

  void insert(int v) {
    int i = 0;
    while (i < size) {
      int &child = (v < t[i].key ? t[i].lc : t[i].rc);
      if (child == -1)
        child = size;
      i = child;
    }
    t[size++].init(v);
  }

  void preord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    preord(t[pos].lc, vec);
    vec.push_back(t[pos].key);
    preord(t[pos].rc, vec);
  }

  void inord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    vec.push_back(t[pos].key);
    inord(t[pos].lc, vec);
    inord(t[pos].rc, vec);
  }
};

bst T;

int main() {
  T.init();
  int n, a;
  char str[100];
  scanf("%d", &n);

  while (n--) {
    scanf("%s", str);
    if (str[0] == 'i') {
      scanf("%d", &a);
      T.insert(a);
    } else {
      vector<int> ans, ans2;

      T.preord(0, ans);
      for (int i : ans)
        cout << ' ' << i;
      cout << endl;

      T.inord(0, ans2);
      for (int i : ans2)
        cout << ' ' << i;
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

struct node {
  int lc;
  int rc;
  int key;
  void init(int a) {
    lc = rc = -1;
    key = a;
  }
};

#define bstN 200000
struct bst {

  int size;
  node t[bstN];

  void init() { size = 0; }

  void insert(int v) {
    int i = 0;
    while (i < size) {
      int &child = (v < t[i].key ? t[i].lc : t[i].rc);
      if (child == -1)
        child = size;
      i = child;
    }
    t[size++].init(v);
  }

  void preord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    preord(t[pos].lc, vec);
    vec.push_back(t[pos].key);
    preord(t[pos].rc, vec);
  }

  void inord(int pos, vector<int> &vec) {
    if (pos == -1 || pos == size)
      return;
    vec.push_back(t[pos].key);
    inord(t[pos].lc, vec);
    inord(t[pos].rc, vec);
  }
};

bst T;

int main() {
  T.init();
  int n, a;
  char str[100];
  scanf("%d", &n);

  while (n--) {
    scanf("%s", str);
    if (str[0] == 'i') {
      scanf("%d", &a);
      T.insert(a);
    } else {
      vector<int> ans, ans2;

      T.preord(0, ans);
      for (int i : ans)
        cout << ' ' << i;
      cout << endl;

      T.inord(0, ans2);
      for (int i : ans2)
        cout << ' ' << i;
      cout << endl;
    }
  }

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

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

#define MAX_N 500000
#define INF 2139062143

int x[MAX_N][3], cnt; // num,left,right
int n, a;
string S;

void inorder(int u) {
  if (u == -1) {
    return;
  }
  inorder(x[u][1]);
  cout << ' ' << x[u][0];
  inorder(x[u][2]);
}
void preorder(int u) {
  if (u == -1) {
    return;
  }
  cout << ' ' << x[u][0];
  preorder(x[u][1]);
  preorder(x[u][2]);
}

int main() {
  int root = -INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S;
    if (S == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
    if (S == "insert") {
      cin >> a;
      if (root == -INF) {
        root = cnt;
      } else {
        int v = root;
        while (true) {
          if (a < x[v][0]) {
            if (x[v][1] == -1) {
              x[v][1] = cnt;
              goto E;
            }
            v = x[v][1];
          } else {
            if (x[v][2] == -1) {
              x[v][2] = cnt;
              goto E;
            }
            v = x[v][2];
          }
        }
      E:;
      }
      x[cnt][0] = a;
      x[cnt][1] = -1;
      x[cnt][2] = -1;
      cnt++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define MAX_N 500000
#define INF 2139062143

int x[MAX_N][3], cnt; // num,left,right
int n, a;
string S;

void inorder(int u) {
  if (u == -1) {
    return;
  }
  inorder(x[u][1]);
  cout << ' ' << u;
  inorder(x[u][2]);
}
void preorder(int u) {
  if (u == -1) {
    return;
  }
  cout << ' ' << u;
  preorder(x[u][1]);
  preorder(x[u][2]);
}

int main() {
  int root = -INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S;
    if (S == "print") {
      inorder(root);
      cout << endl;
      preorder(root);
    }
    if (S == "insert") {
      cin >> a;
      if (root == -INF) {
        root = cnt;
      } else {
        int v = root;
        while (true) {
          if (a < x[v][0]) {
            if (x[v][1] == -1) {
              x[v][1] = cnt;
              goto E;
            }
            v = x[v][1];
          } else {
            if (x[v][2] == -1) {
              x[v][2] = cnt;
            }
            v = x[v][2];
          }
        }
      E:;
      }
      x[cnt][0] = a;
      x[cnt][1] = -1;
      x[cnt][2] = -1;
      cnt++;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#pragma warning(disable : 4996)
struct Node {
  int key;
  Node *p, *l, *r;
};
Node *root, *NIL;
void insert(int key_) {
  Node *x = root, *y = NIL, *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = key_, z->l = NIL, z->r = NIL;
  while (x != NIL) {
    y = x;
    x = (z->key < x->key) ? x->l : x->r;
  }
  z->p = y;
  if (y == NIL)
    root = z;
  else if (z->key < y->key)
    y->l = z;
  else
    y->r = z;
}
void walk1(Node *x) {
  if (x == NIL)
    return;
  walk1(x->l);
  printf(" %d", x->key);
  walk1(x->r);
}
void walk2(Node *x) {
  if (x == NIL)
    return;
  printf(" %d", x->key);
  walk2(x->l);
  walk2(x->r);
}
int n, v;
char c[8];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", &c);
    if (c[0] == 'i')
      scanf("%d", &v), insert(v);
    else
      walk1(root), printf("\\n"), walk2(root), printf("\\n");
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#pragma warning(disable : 4996)
struct Node {
  int key;
  Node *p, *l, *r;
};
Node *root, *NIL;
void insert(int key_) {
  Node *x = root, *y = NIL, *z;
  z = (Node *)malloc(sizeof(Node));
  z->key = key_, z->l = NIL, z->r = NIL;
  while (x != NIL) {
    y = x;
    x = (z->key < x->key) ? x->l : x->r;
  }
  z->p = y;
  if (y == NIL)
    root = z;
  else if (z->key < y->key)
    y->l = z;
  else
    y->r = z;
}
void walk1(Node *x) {
  walk1(x->l);
  printf(" %d", x->key);
  walk1(x->r);
}
void walk2(Node *x) {
  printf(" %d", x->key);
  walk2(x->l);
  walk2(x->r);
}
int n, v;
char c[8];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", &c);
    if (c[0] == 'i')
      scanf("%d", &v), insert(v);
    else
      walk1(root), printf("\\n"), walk2(root), printf("\\n");
  }
}
Problem p02283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  Node *l, *r;
  int key;
};

void insert(Node **root, int key) {
  Node **cur = root, *node = new Node;
  node->l = NULL;
  node->r = NULL;
  node->key = key;
  while (NULL != *cur) {
    if (node->key < (*cur)->key)
      cur = &((*cur)->l);
    else
      cur = &((*cur)->r);
  }
  *cur = node;
  return;
}

void inorder(Node *cur) {
  if (cur == NULL)
    return;
  inorder(cur->l);
  cout << " " << cur->key;
  inorder(cur->r);
}

void preorder(Node *cur) {
  if (cur == NULL)
    return;
  cout << " " << cur->key;
  preorder(cur->l);
  preorder(cur->r);
}

int main() {
  Node *root = NULL;
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(&root, key);
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

class Node {
public:
  Node *l, *r;
  int key;
};

void insert(Node **root, int key) {
  Node **cur = root, *node = new Node;
  node->l = NULL;
  node->r = NULL;
  node->key = key;
  while (NULL != *cur) {
    if (node->key < (*cur)->key)
      cur = &((*cur)->l);
    else
      cur = &((*cur)->r);
  }
  *cur = node;
  return;
}

void inorder(Node *cur) {
  if (cur == NULL)
    return;
  inorder(cur->l);
  cout << " " << cur->key;
  inorder(cur->r);
}

void preorder(Node *cur) {
  if (cur == NULL)
    return;
  cout << " " << cur->key;
  preorder(cur->l);
  preorder(cur->r);
  free(cur);
}

int main() {
  Node *root = NULL;
  int n, key;
  string command;
  cin >> n;
  while (n--) {
    cin >> command;
    if ("insert" == command) {
      cin >> key;
      insert(&root, key);
    }
    if ("print" == command) {
      inorder(root);
      cout << endl;
      preorder(root);
      cout << endl;
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

struct Node {
  int key;
  Node *left;
  Node *right;
  Node() { left = right = NULL; }
};

class Tree {
  Node *root = NULL;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
    } else {
      y->right = n;
    }
  }

  void print() {
    print_inorder(root);
    printf("\\n");
    print_preorder(root);
    printf("\\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

struct Node {
  int key;
  Node *left;
  Node *right;
  Node() { left = right = NULL; }
};

class Tree {
  Node *root;

public:
  void insert(Node *n) {
    Node *y = NULL;
    Node *x = root;
    while (x != NULL) {
      y = x;
      if (n->key < x->key) {
        x = x->left;
      } else {
        x = x->right;
      }
    }
    if (y == NULL) {
      root = n;
    } else if (n->key < y->key) {
      y->left = n;
    } else {
      y->right = n;
    }
  }

  void print() {
    print_inorder(root);
    printf("\\n");
    print_preorder(root);
    printf("\\n");
  }

  void print_preorder(Node *n) {
    if (n == NULL)
      return;
    printf(" %d", n->key);
    print_preorder(n->left);
    print_preorder(n->right);
  }
  void print_inorder(Node *n) {
    if (n == NULL)
      return;
    print_inorder(n->left);
    printf(" %d", n->key);
    print_inorder(n->right);
  }
};

int main() {
  int n;
  cin >> n;
  Tree t;
  for (int i = 0; i < n; i++) {
    string ope;
    cin >> ope;
    if (ope == "insert") {
      int k;
      cin >> k;
      Node *node = new Node();
      node->key = k;
      t.insert(node);
    } else if (ope == "print") {
      t.print();
    }
  }
  return 0;
}
Problem p02283 (C++) — buggy submission, verdict: Runtime Error

// binary search tree 1
// input command "insert" or "print", print is output in-order tree walk and
// pre-order tree walk
#include <iostream>
#include <stdlib.h>
#include <string>
typedef struct node {
  struct node *left, *right, *parent;
  int key;
} node;
node *T;
void Insert(int);
void Inorder(node *);
void Preorder(node *);
void Free(node *);
int main() {
  int n, i, key;
  std::string func, ins = "insert", pri = "print";
  T = new node;
  ;
  T->parent = T->left = T->right = NULL, T->key = -1;
  std::cin >> n;
  for (i = 0; i < n; i++) {
    std::cin >> func;
    if (func == ins) {
      std::cin >> key;
      Insert(key);
    } else if (func == pri) {
      Inorder(T);
      std::cout << "\\n";
      Preorder(T);
      std::cout << "\\n";
    }
  }
  Free(T);
  return 0;
}
void Inorder(node *X) {
  if (X != NULL) {
    Inorder(X->left);
    std::cout << " " << X->key;
    Inorder(X->right);
  }
}
void Preorder(node *X) {
  if (X != NULL) {
    std::cout << " " << X->key;
    Preorder(X->left);
    Preorder(X->right);
  }
}
void Free(node *X) {
  if (X != NULL) {
    Free(X->left);
    Free(X->right);
    delete X;
  }
}
void Insert(int key) {
  node *x, *y, *S;
  if (T->key == -1) {
    T->key = key;
    return;
  }
  S = new node;
  S->key = key;
  S->parent = S->left = S->right = NULL;
  x = T;
  while (x != NULL) {
    y = x;
    if (key < x->key)
      x = x->left;
    else
      x = x->right;
  }
  S->parent = y;
  if (S->key < y->key)
    y->left = S;
  else
    y->right = S;
}

Error: -11

Fixed submission:

// binary search tree 1
// input command "insert" or "print", print is output in-order tree walk and
// pre-order tree walk
#include <iostream>
#include <stdlib.h>
#include <string>
typedef struct node {
  struct node *left, *right, *parent;
  int key;
} node;
node *T;
void Insert(int);
void Inorder(node *);
void Preorder(node *);
void Free(node *);
int main() {
  int n, i, key;
  std::string func, ins = "insert", pri = "print";
  T = new node;
  ;
  T->parent = T->left = T->right = NULL, T->key = -1;
  std::cin >> n;
  for (i = 0; i < n; i++) {
    std::cin >> func;
    if (func == ins) {
      std::cin >> key;
      Insert(key);
    } else if (func == pri) {
      Inorder(T);
      std::cout << "\\n";
      Preorder(T);
      std::cout << "\\n";
    }
  }
  Free(T);
  return 0;
}
void Inorder(node *X) {
  if (X != NULL) {
    Inorder(X->left);
    std::cout << " " << X->key;
    Inorder(X->right);
  }
}
void Preorder(node *X) {
  if (X != NULL) {
    std::cout << " " << X->key;
    Preorder(X->left);
    Preorder(X->right);
  }
}
void Free(node *X) {
  if (X != NULL) {
    Free(X->left);
    Free(X->right);
    delete X;
  }
}
void Insert(int key) {
  node *x, *y, *S;
  if (T->key == -1) {
    T->key = key;
    return;
  }
  S = new node;
  S->key = key, S->parent = S->left = S->right = NULL;
  x = T;
  while (x != NULL)
    y = x = (key < x->key ? x->left : x->right);
  S->parent = y;
  (S->key < y->key ? y->left : y->right) = S;
}