-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL2Q2.java
282 lines (256 loc) · 5.97 KB
/
L2Q2.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
class Retorno{
Dep root;
int v;
BSTProd bstprod;
Retorno(Dep root, int v, BSTProd bstprod){
this.root = null;
this.v = v;
this.bstprod = new BSTProd();
}
}
class RetornoProd{
Prod root;
int v;
int qtd;
RetornoProd(Prod root, int v, int qtd){
this.root = root;
this.v = v;
this.qtd = qtd;
}
}
class Prod{
Prod left;
Prod right;
int codigo;
int qtd;
Prod (int codigo, int qtd){
this.left = null;
this.right = null;
this.codigo = codigo;
this.qtd = qtd;
}
}
class Dep{
Dep left;
Dep right;
BSTProd bst_prod;
int code;
Dep(int code){
this.left = null;
this.right = null;
this.code = code;
this.bst_prod = new BSTProd();
}
}
class BSTProd{
Prod root;
BSTProd(){
this.root = null;
}
Prod search(Prod root, int codigo){
Prod aux = null;
if (root!=null){
if (root.codigo==codigo){
return root;
} else if (codigo < root.codigo){
aux = search(root.left, codigo);
return aux;
} else {
aux = search(root.right, codigo);
return aux;
}
} else {
return aux;
}
}
Prod insert(Prod novo, Prod curr){
if (curr==null){
return novo;
} else {
if (novo.codigo < curr.codigo){
curr.left = insert(novo, curr.left);
} else if (novo.codigo==curr.codigo){
curr.qtd = curr.qtd + novo.qtd;
} else {
curr.right = insert(novo, curr.right);
}
return curr;
}
}
RetornoProd del_min(Prod root){
if (root.left == null){
RetornoProd ret = new RetornoProd(root.right, root.codigo, root.qtd);
root = null;
return ret;
} else {
RetornoProd ret = del_min(root.left);
root.left = ret.root;
ret.root = root;
return ret;
}
}
Prod del(Prod in, int codigo){
if (in == null) { }
else if (in.codigo == codigo) {
if (in.left == null && in.right == null) {
in = null;
}
else if (in.left != null && in.right != null) {
RetornoProd ret = del_min(in.right);
in.right = ret.root;
in.codigo = ret.v;
} else if (in.left != null && in.right == null) {
in = in.left;
} else if (in.left == null && in.right != null) {
in = in.right;
}
} else if (in.codigo > codigo) {
in.left = del(in.left, codigo);
} else if (in.codigo < codigo) {
in.right = del(in.right, codigo);
}
return in;
}
void print(Prod root, Arquivo arq){
if (root==null) return;
print(root.left, arq);
arq.println(root.codigo + " " + root.qtd);
print(root.right, arq);
}
}
class BSTDep {
Dep root;
BSTDep(){
}
Dep search(Dep root, int code){
Dep aux = null;
if (root==null){
return root;
}
if (root.code==code){
return root;
} else if (code < root.code){
aux = search(root.left, code);
return aux;
} else {
aux = search(root.right, code);
return aux;
}
}
Dep insert(Dep novo, Dep att){
if (att == null){
return novo;
} else {
if (novo.code<att.code){
att.left = insert(novo, att.left);
} else {
att.right = insert(novo, att.right);
}
return att;
}
}
Retorno del_min(Dep root){
if (root.left == null){
Retorno ret = new Retorno(root.right, root.code, root.bst_prod);
root = null;
return ret;
} else {
Retorno ret = del_min(root.left);
root.left = ret.root;
ret.root = root;
return ret;
}
}
Dep del(Dep in, int code){
if (in == null) { }
else if (in.code == code) {
if (in.left == null && in.right == null) {
in = null;
}
else if (in.left != null && in.right != null) {
Retorno ret = del_min(in.right);
in.right = ret.root;
in.code = ret.v;
in.bst_prod = ret.bstprod;
} else if (in.left != null && in.right == null) {
in = in.left;
} else if (in.left == null && in.right != null) {
in = in.right;
}
} else if (in.code > code) {
in.left = del(in.left, code);
} else if (in.code < code) {
in.right = del(in.right, code);
}
return in;
}
void print(Dep root, Arquivo arq){
if (root==null)return;
print(root.left, arq);
arq.println("DEPARTAMENTO " + root.code);
root.bst_prod.print(root.bst_prod.root, arq);
print(root.right, arq);
}
}
public class L2Q2{
public static void main(String[] args) {
Arquivo arq = new Arquivo("L2Q2.in", "L2Q2.out");
BSTDep bstdep = new BSTDep();
int cases=1;
while (!arq.isEndOfFile()){
char entrada = arq.readString().charAt(0);
switch (entrada){
case 'I': //inserir
char in = arq.readString().charAt(0);
if (in == 'D'){ //departamento
int code = arq.readInt();
if (bstdep.root==null){
bstdep.root=bstdep.insert(new Dep(code), null);
} else {
bstdep.insert(new Dep(code), bstdep.root);
}
} else { //produto
int codigo = arq.readInt();
int qtd = arq.readInt();
int depCode = arq.readInt();
BSTProd bprod = bstdep.search(bstdep.root, depCode).bst_prod;
if (bprod.root==null){
bprod.root = bprod.insert(new Prod(codigo, qtd), null);
} else {
bprod.insert(new Prod(codigo, qtd), bprod.root);
}
}
break;
case 'R': //remover
in = arq.readString().charAt(0);
if (in == 'D'){//remover departamento
int code = arq.readInt();
bstdep.root = bstdep.del(bstdep.root, code);
} else {//remover produto
int codigo = arq.readInt();
int qtd = arq.readInt();
int depCode = arq.readInt();
BSTProd bprodFound = bstdep.search(bstdep.root, depCode).bst_prod; //acha a árvore de produtos daquele departamento
Prod prodFound = bprodFound.search(bprodFound.root, codigo); //acha o produto específico
if (prodFound!= null){
if (qtd >= prodFound.qtd){ //qtd a deletar maior que o que tem daquele produto deleta o produto
bprodFound.root = bprodFound.del(bprodFound.root, codigo);
} else {
prodFound.qtd = prodFound.qtd - qtd;
}
}
}
break;
case 'F':
arq.print("CASO #" + cases);
arq.println();
arq.println();
if (bstdep.root!=null) bstdep.print(bstdep.root, arq);
cases = cases + 1;
bstdep.root = null;
arq.println();
break;
}
}//arquivo EOF
}
}