//// Create by Running Photon on 2015-03-10
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define CLR(x) memset(x,0,sizeof x)
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const int MOD=5e5+5;
/*
//优先队列
int heap[maxn], sz = 0;
void push(int x)
{
int i = sz++;
while(i > 0){
int p = (i - 1) / 2;
if(heap[p] <= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
int pop()
{
int ret = heap[0];
int x = heap[--sz];
int i = 0;
while(i * 2 + 1 < sz){
int a = i * 2 + 1, b = i * 2 + 2;
if(b < sz && heap[b] < heap[a]) a = b;
if(x <= heap[a]) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
bool Empty()
{
return sz == 0;
}
*/
#define ll long long
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const int MOD=5e5+5;
/*
//优先队列
int heap[maxn], sz = 0;
void push(int x)
{
int i = sz++;
while(i > 0){
int p = (i - 1) / 2;
if(heap[p] <= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
int pop()
{
int ret = heap[0];
int x = heap[--sz];
int i = 0;
while(i * 2 + 1 < sz){
int a = i * 2 + 1, b = i * 2 + 2;
if(b < sz && heap[b] < heap[a]) a = b;
if(x <= heap[a]) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
bool Empty()
{
return sz == 0;
}
*/
//二叉搜索树
struct node
{
int val;
node *lch, *rch;
node(int val = 0, node *lch = NULL, node *rch = NULL):
val(val), lch(lch), rch(rch){}
};
struct node
{
int val;
node *lch, *rch;
node(int val = 0, node *lch = NULL, node *rch = NULL):
val(val), lch(lch), rch(rch){}
};
node *Insert(node *p, int x)
{
if(p == NULL){
node *q = new node;
q -> val = x;
return q;
}
else{
if(x < p -> val) p -> lch = Insert(p -> lch, x);
else p -> rch = Insert(p -> rch, x);
return p;
}
}
{
if(p == NULL){
node *q = new node;
q -> val = x;
return q;
}
else{
if(x < p -> val) p -> lch = Insert(p -> lch, x);
else p -> rch = Insert(p -> rch, x);
return p;
}
}
bool Find(node *p, int x)
{
if(p == NULL) return false;
else if(x == p -> val) return true;
else if(x < p -> val) return Find(p -> lch, x);
else return Find(p -> rch, x);
}
{
if(p == NULL) return false;
else if(x == p -> val) return true;
else if(x < p -> val) return Find(p -> lch, x);
else return Find(p -> rch, x);
}
node *Remove(node *p, int x)
{
if(p == NULL) return NULL;
else if(x < p -> val) return p -> lch = Remove(p -> lch, x);
else if(x > p -> val) return p -> rch = Remove(p -> rch, x);
else if(p -> lch == NULL){
node *q = p -> rch;
delete p;
return q;
}
else if(p -> lch -> rch == NULL){
node *q = p -> lch;
q -> rch = p -> rch;
delete p;
return q;
}
else{
node *q;
for(q = p -> lch; q -> rch -> rch != NULL; q = q -> rch);
node *r = q -> rch;
q -> rch = r -> lch;
r -> lch = p -> lch;
r -> rch = p -> rch;
delete p;
return r;
}
return p;
}
{
if(p == NULL) return NULL;
else if(x < p -> val) return p -> lch = Remove(p -> lch, x);
else if(x > p -> val) return p -> rch = Remove(p -> rch, x);
else if(p -> lch == NULL){
node *q = p -> rch;
delete p;
return q;
}
else if(p -> lch -> rch == NULL){
node *q = p -> lch;
q -> rch = p -> rch;
delete p;
return q;
}
else{
node *q;
for(q = p -> lch; q -> rch -> rch != NULL; q = q -> rch);
node *r = q -> rch;
q -> rch = r -> lch;
r -> lch = p -> lch;
r -> rch = p -> rch;
delete p;
return r;
}
return p;
}
/*
//并查集
int par[maxn], high[maxn];
void init(int n)
{
for(int i = 0; i < n; i++) par[i] = i, high[i] = 0;
}
int Find_boot(int x)
{
if(par[x] == x) return x;
else return par[x] = Find_boot(par[x]);
}
void unite(int x, int y)
{
x = Find_boot(x);
y = Find_boot(y);
if(x == y) return;
if(high[x] < high[y]) par[x] = par[y];
else{
par[y] = par[x];
if(high[x] == high[y]) high[x]++;
}
}
bool same(int x, int y)
{
return Find_boot(x) == Find_boot(y);
}
*/
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
/*
//优先队列测试
push(3), push(5), push(1), push(21);
while(!Empty())
{
printf("%d ",pop());
printf("\n");
}
*/
//并查集
int par[maxn], high[maxn];
void init(int n)
{
for(int i = 0; i < n; i++) par[i] = i, high[i] = 0;
}
int Find_boot(int x)
{
if(par[x] == x) return x;
else return par[x] = Find_boot(par[x]);
}
void unite(int x, int y)
{
x = Find_boot(x);
y = Find_boot(y);
if(x == y) return;
if(high[x] < high[y]) par[x] = par[y];
else{
par[y] = par[x];
if(high[x] == high[y]) high[x]++;
}
}
bool same(int x, int y)
{
return Find_boot(x) == Find_boot(y);
}
*/
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
/*
//优先队列测试
push(3), push(5), push(1), push(21);
while(!Empty())
{
printf("%d ",pop());
printf("\n");
}
*/
//二叉搜索树测试
node *root = NULL;
root = Insert(root, 1);
Insert(root, 2),Insert(root, 5),Insert(root, 3),Insert(root, 4),Insert(root, 100);
for(int i = 0; i <= 100; i++){
if(Find(root, i)) printf("%d ", i);
}
root = Remove(root, 1);
for(int i = 0; i <= 100; i++){
if(Find(root, i)) printf("%d ", i);
}
node *root = NULL;
root = Insert(root, 1);
Insert(root, 2),Insert(root, 5),Insert(root, 3),Insert(root, 4),Insert(root, 100);
for(int i = 0; i <= 100; i++){
if(Find(root, i)) printf("%d ", i);
}
root = Remove(root, 1);
for(int i = 0; i <= 100; i++){
if(Find(root, i)) printf("%d ", i);
}
//并查集测试
return 0;
}
}