Problem:
Binary Search Tree I
Search trees are data structures that support dynamic set operations including insert, search, delete and so on. Thus a search tree can be used both as a dictionary and as a priority queue.
Binary search tree is one of fundamental search trees. The keys in a binary search tree are always stored in such a way as to satisfy the following binary search tree property:
Let $x$ be a node in a binary search tree. If $y$ is a node in the left subtree of $x$, then $y.key \leq x.key$. If $y$ is a node in the right subtree of $x$, then $x.key \leq y.key$.
The following figure shows an example of the binary search tree.
For example, keys of nodes which belong to the left sub-tree of the node containing 80 are less than or equal to 80, and keys of nodes which belong to the right sub-tree are more than or equal to 80. The binary search tree property allows us to print out all the keys in the tree in sorted order by an inorder tree walk.
A binary search tree should be implemented in such a way that the binary search tree property continues to hold after modifications by insertions and deletions. A binary search tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields
left
,
right
, and
p
that point to the nodes corresponding to its left child, its right child, and its parent, respectively.
To insert a new value $v$ into a binary search tree $T$, we can use the procedure insert as shown in the following pseudo code. The insert procedure is passed a node $z$ for which $z.key = v$, $z.left = NIL$, and $z.right = NIL$. The procedure modifies $T$ and some of the fields of $z$ in such a way that $z$ is inserted into an appropriate position in the tree.
1 insert(T, z)
2     y = NIL // parent of x
3     x = 'the root of T'
4     while x ≠ NIL
5         y = x // set the parent
6         if z.key < x.key
7             x = x.left // move to the left child
8         else 
9             x = x.right // move to the right child
10    z.p = y
11
12    if y == NIL // T is empty
13        'the root of T' = z
14    else if z.key < y.key
15        y.left = z // z is the left child of y
16    else 
17        y.right = z // z is the right child of y
Write a program which performs the following operations to a binary search tree $T$.
insert
$k$: Insert a node containing $k$ as key into $T$.
print
: Print the keys of the binary search tree by inorder tree walk and  preorder tree walk respectively.
You should use the above pseudo code to implement the insert operation. $T$ is empty at the initial state.
Input
In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by
insert
$k$ or
print
are given.
Output
For each
print
operation, print a list of keys obtained by inorder tree walk and preorder tree walk in a line respectively. Put a space character
before each key
.
Constraints
The number of operations $\leq 500,000$
...(truncated)

Submitted solutions:

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

struct binary_node
{
    int id;
    binary_node* parent;
    binary_node* left;
    binary_node* right;

    int key;

    void set_left(binary_node& l)
    {
        left = &l;
    }
    void set_right(binary_node& r)
    {
        right = &r;
    }
    binary_node(int i, int k)
    {
        id = i;
        key = k;
        parent = nullptr;
        left = nullptr;
        right = nullptr;
    }
    binary_node() : binary_node(-1, -2147483648) {}
};

struct binary_tree
{
    vector<binary_node> nodes;
    int node_count;

    void insert(int k)
    {
        binary_node* z = &nodes[node_count];
        z->key = k;
        node_count++;

        binary_node* y = nullptr;
        binary_node* x = this->root();
        while(x != nullptr)
        {
            y = x;
            if(z->key < x->key)
                x = x->left;
            else
                x = x->right;
        }

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

    binary_node* root()
    {
        for(int i = 0; i < node_count; i++)
            if(nodes[i].parent == nullptr)
                return &nodes[i];
        exit(-1);
    }

    void preorder_walk(binary_node* n)
    {
        cout << " " << n->key;
        if(n->left != nullptr)
            preorder_walk(n->left);
        if(n->right != nullptr)
            preorder_walk(n->right);
    }
    void preorder_walk()
    {
        preorder_walk(this->root());
        cout << endl;
    }
    void inorder_walk(binary_node* n)
    {
        if(n->left != nullptr)
            inorder_walk(n->left);
        cout << " " << n->key;
        if(n->right != nullptr)
            inorder_walk(n->right);
    }
    void inorder_walk()
    {
        inorder_walk(this->root());
        cout << endl;
    }

    binary_tree(int n)
    {
        node_count = 0;
        for(int i = 0; i < n; i++)
            nodes.push_back(binary_node(i,-2147483648));
    }
    binary_tree() : binary_tree(0) {};
};

int main()
{
    int q;
    cin >> q;

    binary_tree t = binary_tree(q);

    for(int query = 0; query < q; query++)
    {
        string s;
        cin >> s;
        if(s == "insert")
        {
            int k;
            cin >> k;
            t.insert(k);
        }
        if(s == "print")
        {
            t.inorder_walk();
            t.preorder_walk();
        }
    }
}
accuracy: 1
time_ms: 340
memory_kb: 24952
final_rank: 12

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
#define PI 3.14159265358979323846264338327950l
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

struct tree {
    // ここでは無駄なものを削除し、keyを追加した
    ll id = -1, parent=-1, key=-1, right=-1, left=-1;
};

// この関数ではvectorの最後の場所にparentとkeyとrightとleftを正しく設定する
void insert(vector<tree> &binary_tree, ll root, ll target, ll key) {
    // 右側の木へ移動する
    if (binary_tree[root].key < key) {
        if (binary_tree[root].right == -1) {
            binary_tree[root].right = target;
            binary_tree[target].parent = root;
            return;
        }
        else {
            insert(binary_tree, binary_tree[root].right, target, key);
        }
    }
    // 左側の木へ移動する
    else if (binary_tree[root].key > key) {
        if (binary_tree[root].left == -1) {
            binary_tree[root].left = target;
            binary_tree[target].parent = root;
        }
        else {
            insert(binary_tree, binary_tree[root].left, target, key);
        }
    }
}

// 木の先行順巡回
void preorder_tree_walk(vector<tree> &binary_tree, ll root) {
    cout << " " << binary_tree[root].key;
    if (binary_tree[root].left != -1) preorder_tree_walk(binary_tree, binary_tree[root].left);
    if (binary_tree[root].right != -1) preorder_tree_walk(binary_tree, binary_tree[root].right);
    return;
}
// 木の中間順巡回
void inorder_tree_walk(vector<tree> &binary_tree, ll root) {
    if (binary_tree[root].left != -1) inorder_tree_walk(binary_tree, binary_tree[root].left);
    cout << " " << binary_tree[root].key;
    if (binary_tree[root].right != -1) inorder_tree_walk(binary_tree, binary_tree[root].right);
    return;
}

// main
void solve() {
    ll m;
    cin >> m;
    ll used = -1, root = 0;
    vector<tree> binary_tree;
    while (m--) {
        string type;
        cin >> type;
        if (type == "insert") {
            ll k;
            cin >> k;
            tree temptree;
            binary_tree.push_back(temptree);
            ++used;
            binary_tree[used].id = used;
            binary_tree[used].key = k;
            if (used != 0) insert(binary_tree, root, used, k);
        }
        else if (type == "print") {
            inorder_tree_walk(binary_tree, root);
            cout << endl;
            preorder_tree_walk(binary_tree, root);
            cout << endl;
        }
    }


    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 25568
final_rank: 7

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

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

const int MAXN = 500000 + 5;
Node tree[MAXN];
int root = 0, tot = 0;

void insert_key(int k) {
    int y = 0, x = root;
    int z = ++tot;
    tree[z].key = k;
    tree[z].left = tree[z].right = tree[z].parent = 0;

    while (x != 0) {
        y = x;
        if (k < tree[x].key)
            x = tree[x].left;
        else
            x = tree[x].right;
    }

    tree[z].parent = y;
    if (y == 0)
        root = z;
    else if (k < tree[y].key)
        tree[y].left = z;
    else
        tree[y].right = z;
}

void inorder(int u) {
    if (u == 0) return;
    inorder(tree[u].left);
    cout << " " << tree[u].key;
    inorder(tree[u].right);
}

void preorder(int u) {
    if (u == 0) return;
    cout << " " << tree[u].key;
    preorder(tree[u].left);
    preorder(tree[u].right);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, x;
    string op;
    cin >> n;
    while (n--) {
        cin >> op;
        if (op == "insert") {
            cin >> x;
            insert_key(x);
        } else {
            inorder(root);
            cout << "\n";
            preorder(root);
            cout << "\n";
        }
    }
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 11184
final_rank: 1

code_snippet: #include <bits/stdc++.h>

using namespace std;

struct node
{
    int key;
    node *left, *right, *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);
    cout << " " << u->key;
    inorder(u->right);
}
void preoder(node *u)
{
    if (u == NIL)
        return;
    cout << " " << u->key;
    preoder(u->left);
    preoder(u->right);
}
int main()
{
    int n, i, x;
    string op;
    cin >> n;
    while (n--)
    {
        cin >> op;
        if (op == "insert")
        {
            cin >> x;
            insert(x);
        }
        else
        {
            inorder(root);
            cout << endl;
            preoder(root);
            cout << endl;
        }
    }

    return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 26788
final_rank: 13

code_snippet: #include <iostream>
#include <string>
#include <cstdio>

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

Node *root = nullptr;
Node *NIL = nullptr;

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

    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);
    std::printf(" %d", u->key);
    inorder(u->right);
}

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

int main() {
    int n, x;
    std::string com;

    NIL = nullptr;
    root = NIL;

    std::scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        std::cin >> com;
        if (com == "insert") {
            std::scanf("%d", &x);
            insert(x);
        } else if (com == "print") {
            inorder(root);
            std::printf("\n");
            preorder(root);
            std::printf("\n");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 26980
final_rank: 11

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

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

Node* root = nullptr;

void insert(Node* z) {
    Node* y = nullptr;
    Node* x = root;

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

    z->parent = y;

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

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

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int m;
    cin >> m;

    for (int i = 0; i < m; ++i) {
        string cmd;
        cin >> cmd;

        if (cmd == "insert") {
            int k;
            cin >> k;
            Node* z = new Node();
            z->key = k;
            z->left = z->right = z->parent = nullptr;
            insert(z);
        } 
        else if (cmd == "print") {
            inorder(root);
            cout << "\n";
            preorder(root);
            cout << "\n";
        }
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 26828
final_rank: 5

code_snippet: #include<iostream>
using namespace std;

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

Node *root;

void insert (int k) {
    Node *x = root;
    Node *y = nullptr;
    Node *z;
    z = (Node *)malloc(sizeof(Node));
    z->key = k;
    z->left = nullptr;
    z->right = nullptr;

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

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

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

int main() {
    int m, k;
    cin >> m;
    string com;
    for (int i = 0; i < m; i++) {
        cin >> com;
        if ( com == "insert" ) {
            cin >> k;
            insert(k);
        }
        else if ( com == "print" ) {
            inorder(root);
            cout << endl;
            preorder(root);
            cout << endl;
        }
    }
}
accuracy: 1
time_ms: 360
memory_kb: 26876
final_rank: 14

code_snippet: #include <cstdio>
#include <cstring>
#include <cstdlib>
#define max 50000
typedef struct Node{
    int m;
    Node *p,*l,*r;
}tree;
int n;
tree *root=NULL;
void preorder(tree *a){
    if(a==NULL) return;
    printf(" %d",a->m);
    preorder(a->l);
    preorder(a->r);
}
void inorder(tree *a){
    if(a==NULL) return;
    inorder(a->l);
    printf(" %d",a->m);
    inorder(a->r);
}
void insert(int k){
    tree *b=NULL;
    tree *c=root;
    tree *w;
    w=(tree*)malloc(sizeof(tree));//分配了内存，结束后还存在
    w->m=k;
    w->l=NULL;
    w->r=NULL;
    while(c!=NULL){
        b=c;//设置父节点
        if(w->m<c->m){
            c=c->l;
        }else c=c->r;
    }
    w->p=b;
    if(b==NULL){
        root=w;
    }else if(b->m>w->m){
        b->l=w;
    }else if(b->m<w->m){
        b->r=w;
    }
}
int main(){
    scanf("%d",&n);
    char q[20];
    int e;
    for(int i=0;i<n;i++){
        scanf("%s",q);
        if(q[0]=='i'){
            scanf("%d",&e);
            insert(e);
        }
        if(q[0]=='p'){
            inorder(root);
            printf("\n");
            preorder(root);
            printf("\n");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 26416
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#include <chrono>
#include <complex>
#include <cmath>
#include <unistd.h>
#define pc_u putchar
#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,a,b) for(it i=(it)(a);i<(it)b;i++)
#define rep2(i,a,b,c) for(it i=(it)(a);i<(it)b;i+=(it)c)
#define repp(i,a,b) for(it i=(it)(a);i<=(it)b;i++)
#define repp2(i,a,b,c) for(it i=(it)(a);i<=(it)b;i+=(it)c)
#define irep(i,a,b) for(int i=(int)(a);i<(int)b;i++)
#define irep2(i,a,b,c) for(int i=(int)(a);i<(int)b;i+=c)
#define irepp(i,a,b) for(int i=(int)(a);i<=(int)b;i++)
#define irepp2(i,a,b,c) for(int i=(int)(a);i<=(int)b;i+=c)
#define nrep(i,a,b) for(it i=(it)(a)-1;i>=(it)b;i--)
#define nrepp(i,a,b) for(it i=(it)(a);i>=(it)b;i--)
#define inrep(i,a,b) for(int i=(int)(a)-1;i>=(int)b;i--)
#define inrepp(i,a,b) for(int i=(int)(a);i>=(int)b;i--)
#define inrep2(i,a,b,c) for(int i=(int)(a)-1;i>=(int)b;i-=c)
#define inrepp2(i,a,b,c) for(int i=(int)(a);i>=(int)b;i-=c)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Min(x) *min_element(all(x))
#define Max(x) *max_element(all(x))
#define popcount(x) __builtin_popcountll(x)
#define moda 998244353LL
#define modb 1000000007LL
#define modc 968244353LL
#define dai 2502502502502502502LL
#define sho -dai
#define aoi 1e18+1e6
#define giri 1010000000
#define en 3.14159265358979
#define eps 1e-14
#define fi first
#define se second
#define elif else if
template<typename T> using pq=priority_queue<T>;
template<typename T> using pqg=priority_queue<T,vector<T>,greater<T>>;
#define uni(a) a.erase(unique(all(a)),a.end())
using it=long long;
using itn=int;
using un=unsigned long long;
using idb=double;
using db=long double;
using st=string;
using ch=char;
using bo=bool;
using P=pair<it,it>;
using ip=pair<int,int>;
using vi=vector<it>;
using ivi=vector<int>;
using ivd=vector<idb>;
using vd=vector<db>;
using vs=vector<st>;
using vc=vector<ch>;
using vb=vector<bo>;
using vp=vector<P>;
using ivp=vector<ip>;
using sp=set<P>;
using isp=set<ip>;
using ss=set<st>;
using sca=set<ch>;
using si=set<it>;
using isi=set<int>;
using svi=set<vi>;
using svb=set<vb>;
using vvi=vector<vi>;
using ivvi=vector<ivi>;
using ivvd=vector<ivd>;
using vvd=vector<vd>;
using vvs=vector<vs>;
using vvb=vector<vb>;
using vvc=vector<vc>;
using vvp=vector<vp>;
using ivvp=vector<ivp>;
using vsi=vector<si>;
using ivsi=vector<isi>;
using isvi=set<ivi>;
using vsc=vector<sca>;
using vsp=vector<sp>;
using ivsp=vector<isp>;
using vvsi=vector<vsi>;
using ivvsi=vector<ivsi>;
using vvsp=vector<vsp>;
using ivvsp=vector<ivsp>;
using svvb=set<vvb>;
using vvvi=vector<vvi>;
using ivvvi=vector<ivvi>;
using ivvvd=vector<ivvd>;
using vvvd=vector<vvd>;
using vvvb=vector<vvb>;
using ivvvp=vector<ivvp>;
using vvvp=vector<vvp>;
using vvvvi=vector<vvvi>;
using ivvvvi=vector<ivvvi>;
using vvvvd=vector<vvvd>;
using vvvvb=vector<vvvb>;
using ivvvvp=vector<ivvvp>;
using vvvvp=vector<vvvp>;
using ivvvvvi=vector<ivvvvi>;
using vvvvvi=vector<vvvvi>;
using ivvvvvvi=vector<ivvvvvi>;
using vvvvvvi=vector<vvvvvi>;
using ivvvvvvvi=vector<ivvvvvvi>;
using vvvvvvvi=vector<vvvvvvi>;
using vvvvvvvvi=vector<vvvvvvvi>;
const int dx[8]={0,1,0,-1,1,1,-1,-1};
const int dy[8]={1,0,-1,0,1,-1,1,-1};
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
st num="0123456789";
st mb="xo";
st MB="XO";
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

namespace {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  int s = (int)v.size();
  for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
  return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}

istream &operator>>(istream &is, __int128_t &x) {
  string S;
  is >> S;
  x = 0;
  int flag = 0;
  for (auto &c : S) {
    if (c == '-') {
      flag = true;
      continue;
    }
    x *= 10;
    x += c - '0';
  }
  if (flag) x = -x;
  return is;
}

istream &operator>>(istream &is, __uint128_t &x) {
  string S;
  is >> S;
  x = 0;
  for (auto &c : S) {
    x *= 10;
    x += c - '0';
  }
  return is;
}

ostream &operator<<(ostream &os, __int128_t x) {
  if (x == 0) return os << 0;
  if (x < 0) os << '-', x = -x;
  string S;
  while (x) S.push_back('0' + x % 10), x /= 10;
  reverse(begin(S), end(S));
  return os << S;
}
ostream &operator<<(ostream &os, __uint128_t x) {
  if (x == 0) return os << 0;
  string S;
  while (x) S.push_back('0' + x % 10), x /= 10;
  reverse(begin(S), end(S));
  return os << S;
}

void input() {}
template <typename T, class... U>
void input(T &t, U &...u) {
  cin >> t;
  input(u...);
}
template <typename T, class... U>
void input1(T &t, U &...u) {
  input(u...);
}
void print() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void print(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u)) cout << sep;
  print(u...);
}
template <typename T, class... U, char sep = ' '>
void print1(const T &t, const U &...u) {
  print(u...);
}

struct IoSetupNya {
  IoSetupNya() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(7);
  }
} iosetupnya;

}  //namespace Nyaan

template<typename T>
T Sum(vector<T> &a){
  T s=0;
  for(auto i:a)s+=i;
  return s;
}

template<typename T>
T Sum(vector<T> &a,int l,int r){
  T s=0;
  irep(i,l,r)s+=a[i];
  return s;
}

template <typename T>
void dec(vector<T> &t){
  for(auto &i:t)i--;
}

template <typename T>
void inc(vector<T> &t,T k=1){
  for(auto &i:t)i+=k;
}

template <typename T>
vector<T> make_vec(int n,T t){
  vector<T> g(n);
  for(auto &i:g)i=t;
  return g;
}

template <typename T>
vector<T> subvec(vector<T> a,int f,int l){
  vector<T> g(l);
  irep(i,f,f+l)g[i-f]=a[i];
  return g;
}

template<typename T>
T gcda(T a,T b){
  if(!a||!b)return max(a,b);
  while(a%b&&b%a){
    if(a>b)a%=b;
    else b%=a;
  }
  return min(a,b);
}

it lcma(it a,it b){
  return a/gcda(a,b)*b;
}

db distance(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));}

bo outc(int h,int w,int x,int y){
  return (x<0||x>=h||y<0||y>=w);
}

template<typename T>
vector<vector<T>> nep(vector<T> a){
  vector<vector<T>> e;
  sort(all(a));
  do{
    e.emplace_back(a);
  }while(next_permutation(all(a)));
  return e;
}

template<typename T>
void chmin(T &a,T b){a=min(a,b);}
template<typename T>
void chmax(T &a,T b){a=max(a,b);}

void yn(bo a){print(a?string("Yes"):string("No"));}
void YN(bo a){print(a?string("YES"):string("NO"));}

struct dsu1{
  ivi par,siz;
  dsu1(int n){
    init(n);
  }
  void init(int n){
    irep(i,0,n){
      par.emplace_back(i);
    }
    siz.resize(n,1);
  }
  int leader(int u){
    if(par[u]==u)return u;
    return par[u]=leader(par[u]);
  }
  void merge(int u,int v){
    int ru=leader(u),rv=leader(v);
    if(ru==rv)return;
    if(size(ru)<size(rv))swap(ru,rv);
    siz[ru]+=siz[rv];
    par[rv]=ru;
  }
  bool same(int u,int v){
    return leader(u)==leader(v);
  }
  int size(int u){
    return siz[leader(u)];
  }
};

struct edge{
  int f;
  int t;
  it l;
};
void input(edge &a){input(a.f,a.t,a.l);}
bo comppppp(edge &a,edge &b){return a.l<b.l;}

it minspantree(vector<edge> &e,int n){
  /*
  与えられた辺を持つグラフの最小全域木を求める
  存在する場合はその答えを　存在しないならinf
  計算量はO(MlogM+N)くらい
  */
  vector<edge> g=e;sort(all(g),comppppp);
  it ans=0;
  dsu1 uf(n);
  for(auto i:g){
    if(!uf.same(i.f,i.t)){
      uf.merge(i.f,i.t);
      ans+=i.l;
    }
  }
  if(uf.size(0)==n)return ans;
  return dai;
}

/*#include <atcoder/all>
using namespace atcoder;
using mints=modint998244353;
using mint=modint;
using minto=modint1000000007;
using vm=vector<mint>;
using vms=vector<mints>;
using vmo=vector<minto>;
using vvm=vector<vm>;
using vvms=vector<vms>;
using vvmo=vector<vmo>;
using vvvm=vector<vvm>;
using vvvms=vector<vvms>;
using vvvmo=vector<vvmo>;
using vvvvm=vector<vvvm>;
using vvvvms=vector<vvvms>;
using vvvvmo=vector<vvvmo>;
using vvvvvm=vector<vvvvm>;
using vvvvvms=vector<vvvvms>;
using vvvvvmo=vector<vvvvmo>;
using vvvvvvm=vector<vvvvvm>;
using vvvvvvms=vector<vvvvvms>;
using vvvvvvmo=vector<vvvvvmo>;
using vvvvvvvms=vector<vvvvvvms>;
using vvvvvvvmo=vector<vvvvvvmo>;*/

/*総和をもとめるセグ木
struct nod{
  it val;
  nod(it v=0):val(v){}
};

nod op(nod a,nod b){return nod(a.val+b.val);}
nod e(){return nod(0);}

struct act{
  it a;
  act(it e=0):a(e){}
};

nod mapping(act f,nod x){return nod(f.a+x.val);}
act comp(act f,act g){return act(f.a+g.a);}
act id(){return act(0);}*/
//#define endl '\n'
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

template<typename T> struct binarytree{
  private:
  struct node {
    T v;
    node *l;
    node *r;
    node *p;
    // 初め、左右の子は nullptr となっている
    node(T v) : v(v), l(nullptr), r(nullptr), p(nullptr) {}
  };
  public:
  node *root=nullptr;
  void insert(T x){
    node *root_tmp=root;
    node *elem;
    elem=(node *)malloc(sizeof(node));
    elem->v=x;
    elem->l=nullptr;
    elem->r=nullptr;
    node *prev=nullptr;
    while(root_tmp!=nullptr){
      prev=root_tmp;
      if(x<root_tmp->v)root_tmp=root_tmp->l;
      else root_tmp=root_tmp->r;
    }
    elem->p=prev;
    if(elem->p==nullptr)root=elem;
    elif(elem->p->v>elem->v)elem->p->l=elem;
    else elem->p->r=elem;
  }
  void preorder(node *u){
    if(u==nullptr)return;
    cout<<' '<<(u->v);
    preorder(u->l);
    preorder(u->r);
  }
  void inorder(node *u){
    if(u==nullptr)return;
    inorder(u->l);
    cout<<' '<<(u->v);
    inorder(u->r);
  }
};

void solve(){
  int q;input(q);
  binarytree<int> e;
  while(q--){
    st s;input(s);
    if(s=="insert"){
      int x;input(x);
      e.insert(x);
    }
    else{
      e.inorder(e.root);cout<<endl;
      e.preorder(e.root);cout<<endl;
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t=1;//input(t);
  while(t--)solve();
}
accuracy: 1
time_ms: 260
memory_kb: 26828
final_rank: 6

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
using namespace std;


struct Node {
    int v;
    Node* l;
    Node* r;
};

Node* root = NULL;

void insert(int v) {
    if (!root) {
        root = new Node;
        root -> v = v;
        root -> l = NULL;
        root -> r = NULL;
        return;
    }

    Node* par = NULL;
    Node* x = root;
    while (x) {
        par = x;
        if (v < x -> v) {
            x = x -> l;
        } else {
            x = x -> r;
        }
    }
    if (v < par->v) {
        par -> l = new Node;
        Node* p = par -> l;
        p -> v = v;
        p -> l = NULL;
        p -> r = NULL;
    } else {
        par -> r = new Node;
        Node* p = par -> r;
        p -> v = v;
        p -> l = NULL;
        p -> r = NULL;
    }
}

void preOrder(Node* root) {
    if (root == NULL) return ;
    printf(" %d", root -> v);
    preOrder(root -> l);
    preOrder(root -> r);
}

void inOrder(Node* root) {
    if (root == NULL) return ;
    inOrder(root -> l);
    printf(" %d", root -> v);
    inOrder(root -> r);
}


int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;
    int num;
    char s[10];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", s);
        if (s[0] == 'i') {
            scanf("%d", &num);
            insert(num);
        } else if (s[0] == 'p') {
            inOrder(root);
            printf("\n");
            preOrder(root);
            printf("\n");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 19196
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)


// vectorを出力する
void vecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << endl;
    }
}
void newvecprint(vector<int> vec) {
    rep(i, vec.size()) {
        cout << vec[i] << " ";
    }
    cout << endl;
}

// 最大か最小を求める
int minval(vector<int> vec) {
    return *min_element(begin(vec), end(vec));
}
int maxval(vector<int> vec) {
    return *max_element(begin(vec), end(vec));
}

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);
    cout << " " << u->key;
    inorder(u->right);
}
void preorder(node *u) {
    if (u == NIL) {
        return;
    }
    cout << " " << u->key;
    preorder(u->left);
    preorder(u->right);
}



void solve() {
    ll n;
    cin >> n;
    rep(i, n) {
        string cmd;
        cin >> cmd;
        if (cmd == "insert") {
            ll target;
            cin >> target;
            insert(target);
        }
        else if (cmd == "print") {
            inorder(root);
            cout << endl;
            preorder(root);
            cout << endl;
        }
    }

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 26880
final_rank: 8

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

struct Node {
    long long key;
    Node *left, *right, *parent;
    Node(long long k) : key(k), left(nullptr), right(nullptr), parent(nullptr) {}
};

class BST {
    Node* root;

    void insert(long long key) {
        Node* z = new Node(key);
        Node* y = nullptr; // Parent of x
        Node* x = root;   // Start at root

        // Traverse to find the insertion point
        while (x != nullptr) {
            y = x; // Set parent
            if (z->key < x->key)
                x = x->left; // Move to left child
            else
                x = x->right; // Move to right child
        }

        z->parent = y; // Set z's parent

        if (y == nullptr) // Tree is empty
            root = z;
        else if (z->key < y->key)
            y->left = z; // z is left child
        else
            y->right = z; // z is right child
    }

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

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

public:
    BST() : root(nullptr) {}

    void insertKey(long long key) {
        insert(key);
    }

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

int main() {
    BST bst;
    int m;
    cin >> m;
    while (m--) {
        string op;
        cin >> op;
        if (op == "insert") {
            long long key;
            cin >> key;
            bst.insertKey(key);
        } else if (op == "print") {
            bst.print();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 26652
final_rank: 16

code_snippet: #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
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;
}
accuracy: 1
time_ms: 310
memory_kb: 26888
final_rank: 10

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

struct Node{
  int key;
  Node* p;
  Node* left;
  Node* right;
  Node(int k) : key(k), p(nullptr), left(nullptr), right(nullptr) {}
};


Node* insert(Node* &t, Node* z) {
  Node* y = nullptr;
  Node* x = t;
  while(x != nullptr){
    y = x;
    if(z->key < x->key){
      x = x->left;
    }else{
      x = x->right;
    }
  }
  z->p = y;
  if (y == nullptr) {
    t = z;
  } else if (z->key < y->key) {
    y->left = z;
  } else {
    y->right = z;
  }
  return t;
}


void printInorder(Node* t) {
  if (!t) return;
  printInorder(t->left);
  std::cout << " " << t->key;
  printInorder(t->right);
}

void printPreorder(Node* t) {
  if (!t) return;
  std::cout << " " << t->key;
  printPreorder(t->left);
  printPreorder(t->right);
}

int main() {
  int m, k;
  std::string operate;
  std::cin >> m;

  Node* root = nullptr;

  for (int i = 0; i < m; ++i) {
    std::cin >> operate;
    if (operate == "insert") {
      std::cin >> k;
      root = insert(root, new Node(k));
    } else if (operate == "print") {
        printInorder(root);
	std::cout << std::endl;
	printPreorder(root);
	std::cout << std::endl;
    }
  }

  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 26496
final_rank: 15

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define loop(a,b) for (int a = 0;a < b;a++)
#define rep(a,b,c) for(int a = b;a < c;a++)
#define vec(a) vector<a>

struct Node{
    int key;
    Node *left,*right,*parent;
    Node(int key):key(key),left(nullptr),right(nullptr),parent(nullptr){};
};

class BinarySearchTree{
    private:
        Node* root;
        Node* insert(Node* node,int key){
            Node* z = new Node(key);
            Node* y = nullptr;
            Node* x = root;
            while(x != nullptr){
                y = x;
                if(z->key < x->key) x = x->left;
                else x = x->right;
            }
            z->parent = y;
            if(y == nullptr) root = z;
            else if(z->key < y->key) y->left = z;
            else y->right = z;
            return z;
        }
        void inorder(Node* node){
            if(node == nullptr) return;
            inorder(node->left);
            cout <<" "<< node->key;
            inorder(node->right);
        }
        void preorder(Node* node){
            if(node == nullptr) return;
            cout << " "<< node->key;
            preorder(node->left);
            preorder(node->right);
        }
        
        Node* find(Node* node,int key){
            while(node != nullptr && node->key != key){
                if(key < node->key) node = node->left;
                else node = node->right;
            }
            return node;
        }
        Node* find_min(Node* node) {
            while (node->left != nullptr) {
                node = node->left;
            }
            return node;
        }
        
        void remove(Node* node,int key){
            Node* z = find(node,key);
            if(z == nullptr) return;
            
            
            if(z->left == nullptr && z->right == nullptr) {
                if(z->parent == nullptr) {
                    root = nullptr;  
                } else if(z->parent->left == z) {
                    z->parent->left = nullptr;
                } else {
                    z->parent->right = nullptr;
                }
                delete z;
            }
            else if(z->left == nullptr || z->right == nullptr) {
                Node* child = (z->left != nullptr) ? z->left : z->right;
                
                if(z->parent == nullptr) {
                    root = child;  
                } else if(z->parent->left == z) {
                    z->parent->left = child;
                } else {
                    z->parent->right = child;
                }
                
                if(child != nullptr) {
                    child->parent = z->parent;
                }
                delete z;
            }
           
            else {
                
                Node* successor = find_min(z->right);
                
                
                z->key = successor->key;
                
                
                if(successor->parent->left == successor) {
                    successor->parent->left = successor->right;
                } else {
                    successor->parent->right = successor->right;
                }
                
                if(successor->right != nullptr) {
                    successor->right->parent = successor->parent;
                }
                delete successor;
            }
        }

    public:
        BinarySearchTree():root(nullptr){};
        void print(){
            inorder(root);
            cout << endl;
            preorder(root);
            cout << endl;
        }
        void insert(int key){
            insert(root,key);
        }
        bool find(int key){
            Node* node = find(root,key);
            if(node != nullptr) return 1;
            else return 0;
        }
        void remove(int key){
            remove(root,key);
        }
};
int main()
{
    int n;
    cin >> n;
    BinarySearchTree bst;
    loop(i,n){
        string command;
        int key;
        cin >> command;
        if(command == "insert"){
            cin >> key;
            bst.insert(key);
        } 
        else if(command == "find") {
            cin >> key;
            cout << (bst.find(key) ? "yes" : "no") << endl;
        }
        else if(command == "print") {
            bst.print();
        }
        else if(command == "delete"){
            cin >> key;
            bst.remove(key);
        }   
    }
}
accuracy: 1
time_ms: 380
memory_kb: 26764
final_rank: 19

code_snippet: // ALDS2_1_A
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

struct Node {
    int key;
    Node *parent;
    Node *left, *right;
    Node(int k) : key(k), parent(nullptr), left(nullptr), right(nullptr) {}
};

void Insert(Node*& root, Node* newNode) {
    if(root == nullptr) {
        root = newNode;
        return;
    }
    Node* cur = root;
    while(true) {
        if(newNode->key < cur->key) {
            if(cur->left) cur = cur->left;
            else {
                cur->left = newNode;
                newNode->parent = cur;
                break;
            }
        }
        else {
            if(cur->right) cur = cur->right;
            else {
                cur->right = newNode;
                newNode->parent = cur;
                break;
            }
        }
    }
}

void rightRotate(Node*& root, Node* u) {
    Node* x = u->left;
    if(x == nullptr) return; // 回転できない
    Node* B = x->right;

    // xをuの親にする
    x->parent = u->parent;
    if(u->parent == nullptr) root = x;
    else if(u == u->parent->left) u->parent->left = x;
    else u->parent->right = x;

    // Bをuの左に接続
    x->left = B;
    if(B) B->parent = u;

    // uをxの右に接続
    x->right = u;
    u->parent = x;
}

void inOrder(Node* root) {
    if(root == nullptr) return;
    if(root->left != nullptr) inOrder(root->left);
    cout << " " << root->key;
    if(root->right != nullptr) inOrder(root->right);
}

void preOrder(Node* root) {
    if(root == nullptr) return;
    cout << " " << root->key;
    if(root->left != nullptr) preOrder(root->left);
    if(root->right != nullptr) preOrder(root->right);
}

int main() {
    int n;
    cin >> n;
    Node* root = nullptr;

    for(int i=0; i<n; ++i) {
        string com;
        cin >> com;
        if(com == "insert") {
            int key;
            cin >> key;
            Node* newNode = new Node(key);
            Insert(root, newNode);
        }
        else if(com == "print") {
            inOrder(root);
            cout << endl;
            preOrder(root);
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 380
memory_kb: 26764
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> PII;
struct node{
    int p,l,r,key;
};
const int N=5e5+5;
node tree[N];
int idx=0,root=-1;
void insert(int z){
    int y=-1,x=root;
    while(x!=-1){
        y=x;
        if(tree[z].key<tree[x].key) x=tree[x].l;
        else x=tree[x].r;
    }
    tree[z].p=y;
    if(y==-1){
        root=z;
    }else if(tree[z].key<tree[y].key){
        tree[y].l=z;
    }else{
        tree[y].r=z;
    }
}
void dfs1(int u=0){
    if(u==-1) return;
    dfs1(tree[u].l);
    cout<<" "<<tree[u].key;
    dfs1(tree[u].r);
}
void dfs2(int u=0){
    if(u==-1) return;
    cout<<" "<<tree[u].key;
    dfs2(tree[u].l);
    dfs2(tree[u].r);
}
void solve(){
    int n;cin>>n;
    for(int i=0;i<n;i++){
        string s;cin>>s;
        if(s=="insert"){
            int k;cin>>k;
            int z=idx++;
            tree[z].key=k;
            tree[z].l=tree[z].r=-1;
            insert(z);
        }else{
            dfs1();
            cout<<endl;
            dfs2();
            cout<<endl;
        }
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T=1;
    // cin>>T;
    for(int t=1;t<=T;t++){
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 19088
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
  int key;
  node *left,*right,*p;
  node(){}
  node(int key):key(key){left=right=p=NULL;};
};
node *root;
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) root=new node(z);
  else if(z.key<y->key) y->left=new node(z);
  else y->right=new node(z);
}
void inorderTreeWalk(node *v){
  if(v==NULL) return;
  inorderTreeWalk(v->left);
  cout<<" "<<v->key;
  inorderTreeWalk(v->right);
}
void preorderTreeWalk(node *v){
  if(v==NULL) return;
  cout<<" "<<v->key;
  preorderTreeWalk(v->left);
  preorderTreeWalk(v->right);
}
signed main(){
  int n;
  cin>>n;
  root=NULL;
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    //cout<<(root==NULL?-1:root->key)<<endl;
    if(s=="print"){
      inorderTreeWalk(root);
      cout<<endl;
      preorderTreeWalk(root);
      cout<<endl;
      continue;
    }
    int x;
    cin>>x;
    insert(node(x));
  }
  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 26764
final_rank: 17

code_snippet: #include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
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;
}
accuracy: 1
time_ms: 310
memory_kb: 26884
final_rank: 9

code_snippet: #include<iostream>
#include<string>
using namespace std;
class Node{
    public:
        Node* right;
        Node* left;
        Node* parent;
        int key;
};
class Tree{
    private:
        Node* root;
    public:
    Tree(){
        root=0;
    }
    void insert(int key);
    void inorder(Node* u);
    void preorder(Node* u);
    void print();
};
void Tree::insert(int k){
    Node* z=new Node();
    z->right=0;
    z->left=0;
    z->parent=0;
    z->key=k;
    Node* y=0;
    Node* x=root;
    while(x!=0){
        y=x;
        if(z->key<x->key){
            x=x->left;
        }
        else{
            x=x->right;
        }
    }
    z->parent=y;
    if(y==0){
        root=z;
    }
    else if(z->key<y->key){
        y->left=z;
    }
    else{
        y->right=z;
    }
}
void Tree::inorder(Node* u){
    if(u==0){
        return;
    }
    inorder(u->left);
    cout<<' '<<u->key;
    inorder(u->right);
}
void Tree::preorder(Node* u){
    if(u==0){
        return;
    }
    cout<<' '<<u->key;
    preorder(u->left);
    preorder(u->right);
}
void Tree::print(){
    inorder(root);
    cout<<endl;
    preorder(root);
    cout<<endl;
}
int main(){
    int m;
    cin>>m;
    Tree T;
    for(int i=0;i<m;i++){
        string com;
        cin>>com;
        if(com=="insert"){
            int k;
            cin>>k;
            T.insert(k);
        }
        else if(com=="print"){
            T.print();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 26780
final_rank: 18