-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1560.cpp
More file actions
executable file
·91 lines (84 loc) · 1.84 KB
/
Copy path1560.cpp
File metadata and controls
executable file
·91 lines (84 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* 我恨二叉搜索树
* 会恨一辈子的
**/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
struct node {
int data;
node *left, *right;
node(int dt):data(dt), left(nullptr), right(nullptr) {}
};
void find(node* rt, int key, char* ans) {
if(rt == nullptr) {
strcpy(ans, "Nothing.");
return;
}
if(rt->data == key) {
return;
} else if(rt->data < key) {
strcat(ans, "r");
find(rt->right, key, ans);
} else {
strcat(ans, "l");
find(rt->left, key, ans);
}
}
void insert(node* &rt, int p) {
if(rt == nullptr) {
rt = new node(p);
} else if(rt->data < p) {
insert(rt->right, p);
} else if(rt->data > p) {
insert(rt->left, p);
}
}
void dele(node* &rt) {
rt->left = rt->left->right;
}
void del(node* &rt, int key) {
if(rt == nullptr) return;
if(rt->data < key) {
del(rt->right, key);
return;
} else if(rt->data > key) {
del(rt->left, key);
return;
}
if(rt->right == nullptr) {
rt = rt->left;
} else {
node* tmp = rt->right;
if(tmp->left == nullptr) {
rt->right->left = rt->left;
rt = rt->right;
return;
}
while(tmp->left->left) tmp = tmp->left;
rt->data = tmp->left->data;
dele(tmp);
}
}
node *root;
char op[3], ans[100233];
int x, Q;
int main() {
freopen("a.in", "r", stdin);
freopen("b.out", "w", stdout);
scanf("%d", &Q);
for(int i = 1;i <= Q;++ i) {
scanf("%s", op);
scanf("%d", &x);
if(op[0] == '+') {
insert(root, x);
} else if(op[0] == '-') {
del(root, x);
} else {
strcpy(ans, "*");
find(root, x, ans);
printf("%s\n", ans);
}
}
}