Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbstops.cpp
More file actions
Latest commit
executable file
·152 lines (141 loc) · 4.18 KB
/
Copy pathbstops.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>
#definefastioios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#defineendl'\n'
#defineintlonglongint
usingnamespacestd;
#definepii pair <int, int>
#definemii map<int, int>
#definepb push_back
#definedeb(x) cout << #x << "" << x << endl
#definedeb2(x, y) cout << #x << "" << x << "" << #y << "" << y << endl
#defineLoop(s, e, itr) for (int itr = s; itr < e; itr++)
#defineloop(n) for(int i = 0; i < n; i++)
#definevin vector<int>
#definew(t) int tc; cin >> tc; for(int t = 1; t <= tc; t++)
#definevec vector
#definemk_arr(n, t, s) t* n = new t[s]; loop(s) cin >> n[i];
#definemi_arr(n, s) int* n = newint[s]; loop(s) cin >> n[i];
#definearr_out(n, s) Loop(0, s, lout) cout << n[lout] << "";
#definepi(x) printf("%lld ", x);
classBST{
public:
BST(){
root = NULL;
}
structnode
{
int data;
int val;
node* l;
node* r;
node(int a){
data = a;
l = r = NULL;
}
};
node* root;
intinsert(int a){
node *temp = newnode(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;
}
intfind(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;
}
voidinorder(node* root){
if(root == NULL) return;
inorder(root->l);
cout << root->data << "";
inorder(root->r);
}
};
int32_tmain(){
BST *bst = newBST();
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;
}
}