-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbstops.cpp
More file actions
executable file
·152 lines (141 loc) · 4.18 KB
/
bstops.cpp
File metadata and controls
executable file
·152 lines (141 loc) · 4.18 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl '\n'
#define int long long int
using namespace std;
#define pii pair <int, int>
#define mii map<int, int>
#define pb push_back
#define deb(x) cout << #x << " " << x << endl
#define deb2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl
#define Loop(s, e, itr) for (int itr = s; itr < e; itr++)
#define loop(n) for(int i = 0; i < n; i++)
#define vin vector<int>
#define w(t) int tc; cin >> tc; for(int t = 1; t <= tc; t++)
#define vec vector
#define mk_arr(n, t, s) t* n = new t[s]; loop(s) cin >> n[i];
#define mi_arr(n, s) int* n = new int[s]; loop(s) cin >> n[i];
#define arr_out(n, s) Loop(0, s, lout) cout << n[lout] << " ";
#define pi(x) printf("%lld ", x);
class BST{
public:
BST(){
root = NULL;
}
struct node
{
int data;
int val;
node* l;
node* r;
node(int a){
data = a;
l = r = NULL;
}
};
node* root;
int insert(int a){
node *temp = new node(a);
node* prev = root;
int ans = 1;
if(root == NULL) root = temp;
else{
while(1){
if(prev->data > a){
if(prev->l == NULL) {prev->l = temp; ans *= 2; break;}
else prev = prev->l, ans *= 2;
}
else{
if(prev->r == NULL) {prev->r = temp; ans = (ans*2+1); break;}
else prev = prev->r, ans = (ans*2+1);
}
}
}
return ans;
}
int find(int a){
node* temp = root;
int ans = 1;
while(temp->data != a){
if(temp->data > a) temp = temp->l, ans *= 2;
else temp = temp->r, ans = ans*2+1;
}
return ans;
}
node* del(int a){
node* temp = root;
node** prev = &root;
while(temp->data != a){
if(temp->data < a) prev = &(temp->r), temp = temp->r;
else prev = &(temp->l), temp = temp->l;
}
node* rep = NULL;
// if(temp->l == NULL){
// if(temp->r == NULL) *prev = NULL;
// else{
// rep = temp->r;
// while(rep->l != NULL) rep = rep->l;
// int rd = rep->data;
// int rv = rep->val;
// del(rep->data);
// (*prev)->data = rd;
// (*prev)->val = rv;
// }
// }
// else {
// rep = temp->l;
// while(rep->r != NULL) rep = rep->r;
// int rd = rep->data;
// int rv = rep->val;
// del(rep->data);
// (*prev)->data = rd;
// (*prev)->val = rv;
// }
if(temp->r == NULL){
if(temp->l == NULL) *prev = NULL;
else{
rep = temp->l;
while(rep->r != NULL) rep = rep->r;
int rd = rep->data;
int rv = rep->val;
del(rep->data);
(*prev)->data = rd;
(*prev)->val = rv;
}
}
else {
rep = temp->r;
while(rep->l != NULL) rep = rep->l;
int rd = rep->data;
int rv = rep->val;
del(rep->data);
(*prev)->data = rd;
(*prev)->val = rv;
}
return temp;
}
void inorder(node* root){
if(root == NULL) return;
inorder(root->l);
cout << root->data << " ";
inorder(root->r);
}
};
int32_t main(){
BST *bst = new BST();
int q;
cin >> q;
char g, ch;
int a;
while(q--){
scanf("%c %c %lld", &g, &ch, &a);
if(ch == 'i'){
cout << bst->insert(a) << endl;
}
else{
cout << bst->find(a) << endl;
bst->del(a);
}
// cout << "inorder ";bst->inorder(bst->root); cout << endl;
}
}