-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_search_tree.cpp
119 lines (100 loc) · 1.36 KB
/
binary_search_tree.cpp
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
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("arb.in");
struct nod{
nod *st, *dr;
int nr;
int f;
} *rad;
void creare(nod *&rad, int x){
if(rad){
if(x < rad->nr){
creare(rad->st, x);
}
else if(x > rad->nr){
creare(rad->dr, x);
}
else{
rad->f++;
}
}
else{
rad = new nod;
rad->st = rad->dr = NULL;
rad->nr = x;
rad->f = 1;
}
}
void SRD(nod *r){ // in
if(r != NULL){
SRD(r->st);
cout<<r->nr<<"("<<r->f<<")"<<" ";
SRD(r->dr);
}
}
void el_sterge_mx(nod *&p, nod*&q){
if(q->dr){
el_sterge_mx(p, q->dr);
}
else{
nod *r = q->st;
p->nr = q->nr;
p->f = q->f;
delete q;
q = r;
}
}
void sterge(nod *&r, int x){
if(r){
nod *p;
if(x < r->nr){
sterge(r->st, x);
}
else if(x > r->nr){
sterge(r->dr, x);
}
else if(!r->st && !r->dr){
p=r;
delete p;
r = NULL;
}
else if(!r->st){
p = r->dr;
delete r;
r = p;
}
else if(!r->dr){
p = r->st;
delete r;
r = p;
}
else{
el_sterge_mx(r, r->st);
}
}
}
void sterge_el_cu_fr_x(nod *&r, int x){
if(r){
if(r->f >= x){
sterge(r, r->nr);
}
sterge_el_cu_fr_x(r->st, x);
sterge_el_cu_fr_x(r->dr, x);
}
}
int main(){
long long x;
int fr;
f>>fr>>x;
while(x){
creare(rad, x%10);
x /= 10;
}
SRD(rad);
cout<<"\n";
sterge_el_cu_fr_x(rad, fr);
SRD(rad);
cout<<"\n";
f.close();
}