Problem:
Binary Search Tree II
Write a program which performs the following operations to a binary search tree $T$ by adding the find operation to A: Binary Search Tree I.
insert
$k$: Insert a node containing $k$ as key into $T$.
find
$k$: Report whether $T$ has a node containing $k$.
print
: Print the keys of the binary search tree by inorder tree walk and  preorder tree walk respectively.
Input
In the first line, the number of operations $m$ is given. In the following $m$ lines, operations represented by
insert
$k$,
find
$k$ or
print
are given.
Output
For each
find
$k$ operation, print "
yes
" if $T$ has a node containing $k$, "
no
" if not.
In addition, 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$
The number of print operations $\leq 10$.
$-2,000,000,000 \leq key \leq 2,000,000,000$
The height of the binary tree does not exceed 100 if you employ the above pseudo code.
The keys in the binary search tree are all different.
Sample Input 1
10
insert 30
insert 88
insert 12
insert 1
insert 20
find 12
insert 17
insert 25
find 16
print
Sample Output 1
yes
no
 1 12 17 20 25 30 88
 30 12 1 20 17 25 88
Reference
Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

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

    bool find(int k)
    {
        if(node_count == 0)
            return false;
        else
        {
            binary_node* x = root();
            while(x != nullptr)
            {
                if(k == x->key)
                    return true;

                if(k < x->key)
                    x = x->left;
                else
                    x = x->right;
            }
            return false;
        }
    }

    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 == "find")
        {
            int k;
            cin >> k;
            if(t.find(k))
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        if(s == "print")
        {
            t.inorder_walk();
            t.preorder_walk();
        }
    }
}
accuracy: 1
time_ms: 390
memory_kb: 25464
final_rank: 20

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

    bool find(int k)
    {
        if(node_count == 0)
            return false;
        else
        {
            binary_node* y = nullptr;
            binary_node* x = root();
            while(x != nullptr)
            {
                y = x;
                if(k == y->key)
                    return true;

                if(k < y->key)
                    x = y->left;
                else
                    x = y->right;
            }
            return false;
        }
    }

    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 == "find")
        {
            int k;
            cin >> k;
            if(t.find(k))
                cout << "yes" << endl;
            else
                cout << "no" << endl;
        }
        if(s == "print")
        {
            t.inorder_walk();
            t.preorder_walk();
        }
    }
}
accuracy: 1
time_ms: 390
memory_kb: 24060
final_rank: 17

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

// この関数では引数の値をrootからたどって発見する
void find(vector<tree> &binary_tree, ll root, ll target) {
    if (binary_tree[root].key == target) {
        cout << "yes" << endl;
        return;
    }
    if (binary_tree[root].key < target) {
        if (binary_tree[root].right == -1) {
            cout << "no" << endl;
            return;
        }
        else find(binary_tree, binary_tree[root].right, target);
    }
    else if (binary_tree[root].key > target) {
        if (binary_tree[root].left == -1) {
            cout << "no" << endl;
            return;
        }
        else find(binary_tree, binary_tree[root].left, target);
    }
}

// 木の先行順巡回
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;
        }
        /* ここから追加分 */
        else if (type == "find") {
            ll k;
            cin >> k;
            find(binary_tree, root, k);
        }
    }


    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 24808
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);
}
int find(int u,int k){
    while (u!=0&&k!=tree[u].key)
    {
        if(k<tree[u].key)u=tree[u].left;
        else u=tree[u].right;
    }
    return u;
    
}

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 if(op=="find"){
            int x;
            cin>>x;
            int u=find(root,x);
            if(u!=0)cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        }
        else {
            inorder(root);
            cout << "\n";
            preorder(root);
            cout << "\n";
        }
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 9392
final_rank: 1

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

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

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

Node* find(Node* u, int k) {
    while (u != NIL && k != u->key) {
        u = (k < u->key) ? u->left : u->right;
    }
    return u;
}

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

    while (x != NIL) {
        y = x;
        x = (z->key < x->key) ? x->left : 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;
    string com;

    std::scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        std::cin >> com;
        if (com == "find") {
            std::scanf("%d", &x);
            Node* t = find(root, x);
            std::printf(t != NIL ? "yes\n" : "no\n");
        } else 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: 350
memory_kb: 22228
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> preorder;
vector<int> inorder;
vector<int> postorder;

class Node {
public:
	int value;
	Node* left;
	Node* right;

	Node(int value) {
		this->value = value;
		this->left = NULL;
		this->right = NULL;
	}
};

class BST {
public:
	Node* root;
	BST() {
		this->root = NULL;
	}

	void Insert(int num);
	bool Search(int num);
};

void BST::Insert(int num) {
	if (root == NULL) {
		root = new Node(num);
		return;
	}

	Node* curr = root;
	while (curr != NULL) {
		if (num < curr->value) {
			if (curr->left == NULL) {
				curr->left = new Node(num);
				return;
			}
			curr = curr->left; // 继续和左子树进行比较
		}
		else {
			if (curr->right == NULL) {
				curr->right = new Node(num);
				return;
			}
			curr = curr->right; // 继续和右子树进行比较
		}
	}
}

bool BST::Search(int num) {
	Node* curr = root;
	while (curr != NULL) {
		if (num < curr->value) {
			curr = curr->left;
		}
		else if (num > curr->value) {
			curr = curr->right;
		}
		else {
			return true;
		}
	}
	return false;
}

// 前序遍历
void PreorderTraversal(Node* node) {
	if (node == NULL) {
		return;
	}
	preorder.push_back(node->value);
	PreorderTraversal(node->left);
	PreorderTraversal(node->right);
}

// 中序遍历
void InorderTraversal(Node* node) {
	if (node == NULL) {
		return;
	}
	InorderTraversal(node->left);
	inorder.push_back(node->value);
	InorderTraversal(node->right);
}

// 后序遍历
void PostorderTraversal(Node* node) {
	if (node == NULL) {
		return;
	}
	PostorderTraversal(node->left);
	PostorderTraversal(node->right);
	postorder.push_back(node->value);
}

void Print(Node *node) {
	inorder.clear();
	preorder.clear();
	postorder.clear();

	InorderTraversal(node);
	for (int i = 0; i < inorder.size(); i++) {
                cout << " ";
		cout << inorder[i];
	}
	cout << endl;

	PreorderTraversal(node);
	for (int i = 0; i < preorder.size(); i++) {
		cout << " ";
		cout << preorder[i];
	}
	cout << endl;

	//PostorderTraversal(node);
	//for (int i = 0; i < postorder.size(); i++) {
	//	if (i)
	//		cout << " ";
	//	cout << postorder[i];
	//}
	//cout << endl;
}

int main() {
	int n;
	cin >> n;
	BST* tree = new BST();

	for (int i = 0; i < n; i++) {
		string inst;
		cin >> inst;
		if(inst == "insert")
		{
			int num;
			cin >> num;
			tree->Insert(num);
		}
		else if (inst == "find")
		{
			int num;
			cin >> num;
			if (tree->Search(num))
				cout << "yes" << endl;
			else
				cout << "no" << endl;
		}
		else if (inst == "print")
		{
			Print(tree->root);
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 20200
final_rank: 12

code_snippet: #include<iostream>
using namespace std;

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

Node *root;

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

    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 find(int k) {
    Node *x = root;
    int flag = 0;
    while ( x != nullptr ) {
        if ( k < x->key ) x = x->left;
        else if ( k > x->key ) x = x->right;
        else if ( k == x->key ) {
            flag += 1;
            break;
        }
    }
    if ( flag ) cout << "yes" << endl;
    else cout << "no" << endl;
}

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, x;
    cin >> m;
    string com;
    for (int i = 0; i < m; i++) {
        cin >> com;
        if ( com == "insert" ) {
            cin >> x;
            insert(x);
        }
        else if ( com == "print" ) {
            inorder(root);
            cout << endl;
            preorder(root);
            cout << endl;
        }
        else if ( com == "find" ) {
            cin >> x;
            find(x);
        }
    }
}
accuracy: 1
time_ms: 420
memory_kb: 22196
final_rank: 19

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;
    }
}
tree* find(int a){
    tree*q=root;
    while(q!=NULL&&q->m!=a){
        if(q->m<a){
            q=q->r;
        }else q=q->l;
    }
    return q;
}
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");
        }
        if(q[0]=='f'){
            scanf("%d",&e);
            if(find(e)!=NULL){
                printf("yes\n");
            }else printf("no\n");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 21720
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;
  }
  bo find(T x,node *u){
    if(u==nullptr)return 0;
    if(x==u->v)return 1;
    if(x<u->v)return find(x,u->l);
    return find(x,u->r);
  }
  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);
    }
    elif(s=="find"){
      int x;input(x);
      cout<<(e.find(x,e.root)?"yes":"no")<<endl;
    }
    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: 290
memory_kb: 22212
final_rank: 5

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;

bool find(int v) {
    Node* x = root;
    while (x && x->v != v) {
        if (v < x->v) {
            x = x -> l;
        } else if (v > x-> v) {
            x = x -> r;
        } 
    }
    if (x && x->v == v) return true;
    return false;
}

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");
        } else if (s[0] == 'f') {
            scanf("%d", &num);
            if (find(num)) printf("yes\n");
            else printf("no\n");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 15916
final_rank: 2

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 find(int k) {
    node *x = root;
    while (x != NIL && k != x->key) {
        if (k < x->key) {
            x = x->left;
        }
        else {
            x = x->right;
        }
    }
    if (x == NIL) {
        cout << "no" << endl;
    }
    else cout << "yes" << endl;
}

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;
        }
        else if (cmd == "find") {
            ll target;
            cin >> target;
            find(target);
        }
    }

    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 22272
final_rank: 6

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

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

Node *NIL, *root;

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

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

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

    z->parent = y;

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

Node* find(Node *x, int k) {
    while (x != NIL && k != x->key) {
        if (k < x->key) x = x->left;
        else x = x->right;
    }
    return x;
}

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");
        } else if (com == "find") {
            scanf("%d", &x);
            Node* t = find(root,x);
            if (t!=NIL) printf("yes\n");
            else printf("no\n");
        }
    }

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 22216
final_rank: 9

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: 400
memory_kb: 21928
final_rank: 13

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 k);
    void inorder(Node* u);
    void preorder(Node* u);
    void print();
    bool find(int x);
};


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


bool Tree::find(int x){
    Node* a = root;
    while(a != 0){
        if(x < a->key){
            a = a->left;
        }
        else if(x > a->key){
            a = a->right;
        }
        else{
            return true;
        }
    }
    return false;
}    


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 == "find"){
            int k;
            cin >> k;
            if(T.find(k) == true)
                cout << "yes\n";
            else cout << "no\n";
        }
        else if(com=="print"){
            T.print();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 21808
final_rank: 16

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 k);
    node* find(int k);
    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; // xの親
    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; // 右の子にする
    }
}
node* tree::find(int k)
{
    node* x = root; // 根
    while(x!=0){
        if(k == x->key){
            return x; // 見つかった
        }
        else if(k < x->key){
            x = x->left; // 左の子へ移動
        }
        else{
            x = x->right; // 右の子へ移動
        }
    }
return 0; // 見つからなかった
}
// 中間順
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=="find"){
            int k;
            cin >> k;
            if(T.find(k)!=0){
                cout << "yes" << endl;
            }
            else{
                cout << "no" << endl;
            }
        }
        else if(com=="print"){
            T.print();
        }
    }
return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 22080
final_rank: 18

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 k);
        node* find(int k);
        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; // xの親
    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; // 右の子にする
    }
}
node* tree::find(int k){
    node* x = root; // 根
    while(x!=0){
        if(k == x->key){
            return x; // 見つかった
        }
        else if(k < x->key){
            x = x->left; // 左の子へ移動
        }
        else{
            x = x->right; // 右の子へ移動
        }
    }
    return 0; // 見つからなかった
}
// 中間順
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=="find"){
            int k;
            cin >> k;
            if(T.find(k)!=0){
                cout << "yes" << endl;
            }
            else{
                cout << "no" << endl;
            }
        }
        else if(com=="print"){
            T.print();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 21956
final_rank: 15

code_snippet: // 2025/07/17 Tazoe

#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 k);
	node* find(int k);
	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;		// xの親
	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;			// 右の子にする
	}
}

node* tree::find(int k)
{
	node* x = root;		// 根
	
	while(x!=0){
		if(k == x->key){
			return x;			// 見つかった
		}
		else if(k < x->key){
			x = x->left;		// 左の子へ移動
		}
		else{
			x = x->right;		// 右の子へ移動
		}
	}
	
	return 0;			// 見つからなかった
}

// 中間順
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=="find"){
			int k;
			cin >> k;
			
			if(T.find(k)!=0){
				cout << "yes" << endl;
			}
			else{
				cout << "no" << endl;
			}
		}
		else if(com=="print"){
			T.print();
		}
	}
	
	return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 21952
final_rank: 14

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);
}
bool find(int k,int u){
    if(u==-1) return false;
    if(k==tree[u].key) return true;
    else if(k<tree[u].key) return find(k,tree[u].l);
    else return find(k,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 if(s=="print"){
            dfs1();
            cout<<endl;
            dfs2();
            cout<<endl;
        }else if(s=="find"){
            int k;cin>>k;
            if(find(k,root)) cout<<"yes"<<endl;
            else cout<<"no"<<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: 180
memory_kb: 17476
final_rank: 3

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>

using namespace std;

// Structure to represent a node in the binary search tree
struct Node {
    int key;
    Node *right, *left, *parent;
};

Node *root, *NIL; // Global pointers for the root of the tree and a sentinel NIL node

// Function to find a node with a given key
Node *find(Node *u, int k) {
    while (u != NIL && k != u->key) {
        if (k < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }
    return u;
}

// Function to insert a new key into the binary search tree
void insert(int k) {
    Node *y = NIL; // y will be the parent of the new node z
    Node *x = root; // x traverses the tree to find the insertion point
    Node *z;

    // Allocate memory for the new node and initialize its fields
    z = (Node *)malloc(sizeof(Node));
    z->key = k;
    z->left = NIL;
    z->right = NIL;

    // Traverse the tree to find the correct position for the new node
    while (x != NIL) {
        y = x; // Keep track of the current node's parent
        if (z->key < x->key) {
            x = x->left;
        } else {
            x = x->right;
        }
    }

    z->parent = y; // Set the parent of the new node

    // If the tree is empty, the new node becomes the root
    if (y == NIL) {
        root = z; // Correction: 'root z;' was a syntax error. Should be 'root = z;'
    } else {
        // Otherwise, attach the new node to its parent
        if (z->key < y->key) {
            y->left = z;
        } else {
            y->right = z;
        }
    }
}

// Function for in-order tree traversal (left, root, right)
void inorder(Node *u) {
    if (u == NIL) return; // Base case: if node is NIL, return
    inorder(u->left); // Recursively visit left subtree
    printf(" %d", u->key); // Print current node's key
    inorder(u->right); // Recursively visit right subtree
}

// Function for pre-order tree traversal (root, left, right)
void preorder(Node *u) {
    if (u == NIL) return; // Base case: if node is NIL, return
    printf(" %d", u->key); // Print current node's key
    preorder(u->left); // Recursively visit left subtree
    preorder(u->right); // Recursively visit right subtree
}

int main() {
    int n, x; // n for number of commands, x for key value
    string com; // com for command string

    // Initialize NIL node. This is crucial for correctly handling tree boundaries.
    NIL = (Node *)malloc(sizeof(Node));
    NIL->left = NIL;
    NIL->right = NIL;
    NIL->parent = NIL;
    root = NIL; // Initialize root to NIL (empty tree)

    scanf("%d", &n); // Read the number of commands

    for (int i = 0; i < n; i++) {
        cin >> com; // Read the command string
        if (com[0] == 'f') { // If command starts with 'f' (find)
            scanf("%d", &x); // Read the key to find
            Node *t = find(root, x); // Call find function
            if (t != NIL) {
                printf("yes\n");
            } else {
                printf("no\n");
            }
        } else if (com == "insert") { // If command is "insert"
            scanf("%d", &x); // Read the key to insert
            insert(x); // Call insert function
        } else if (com == "print") { // If command is "print"
            inorder(root); // Perform in-order traversal
            printf("\n");
            preorder(root); // Correction: 'preorder("root");' was a type mismatch. Should be 'preorder(root);'
            printf("\n");
        }
    }

    // Free the NIL node when done. (Optional for competitive programming, good practice for full applications)
    free(NIL);

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 22200
final_rank: 8

code_snippet: #include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;

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

Node *root, *NIL;

Node * find(Node *u, int k) {
    while ( u != NIL && k != u->key) {
        if (k < u->key) u = u->left;
        else u = u->right;
    }
    
    return u;
}

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[0] == 'f') {
            scanf("%d", &x);
            Node *t = find(root, x);
            if (t != NIL) printf("yes\n");
            else printf("no\n");
        } else 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: 350
memory_kb: 22284
final_rank: 11