-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTP2_TarefaD.java
279 lines (238 loc) · 7.62 KB
/
TP2_TarefaD.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
import java.io.IOException;
import java.util.StringTokenizer;
// classe que representa no da arvore
class Node {
public String matricula;
public int divida;
public Node left;
public Node right;
Node(String matricula, int divida) {
this.matricula = matricula;
this.divida = divida;
left = null;
right = null;
}
// Comparadores. Unico criterio e a String matricula
int compareTo(String matricula) {
return this.matricula.compareTo(matricula);
}
}
class Splay{
/* raiz da arvore. Null quando arvore vazia */
private Node root = null;
//construtor
public Splay() {
root = null;
}
// encontra determinado veiculo na arvore (null se nao encontrado)
public Node get(String matricula) {
Node no = root;
while (no != null) {
if (no.compareTo(matricula) == 0) {
return no;
}
no = ((no.compareTo(matricula) > 0) ? no.left : no.right);
}
return null;
}
private Node rightRotation(Node y){
Node x = y.left;
Node aux = x.right;
//rotacao
x.right = y;
y.left = aux;
//novo no raiz
return x;
}
private Node leftRotate(Node x) {
Node y = x.right;
Node aux = y.left;
//rotacao
y.left = x;
x.right = aux;
//novo no raiz
return y;
}
public Node add(String matricula, int divida){
// veiculo ainda nao existe, cria novo no para este veiculo.
if(root==null){
root = new Node(matricula, divida);
return root;
}
// traz o no da folha mais proximo
root = splay(matricula,root);
//novo no
Node no = new Node(matricula, divida);
// veiculo existe. adiciona a valor acumulado a divida
if(matricula.compareTo(root.matricula) == 0){
root.divida += divida;
}
else { // veiculo nao existe -> insere novo no
if ( matricula.compareTo(root.matricula) < 0 ) {
no.right = root;
no.left = root.left;
root.left = null;
root = no;
}else{ // < 0
no.left = root;
no.right = root.right;
root.right = null;
root = no;
}
}
//novo no
return no;
}
public void remove(String matricula){
root = remove(matricula, root);
}
private Node remove(String matricula, Node node){
// arvore vazia ou no nao encontrado
if(node==null){
return null;
}
node = splay(matricula,node);
Node temp;
// existe matricula
if(node.compareTo(matricula) == 0){
if (node.left == null){ // nao existe no filho esquerdo -> no pai fica o filho direito
node = node.right;
}
else{ // se o no filho esquerdo existir
temp = node;
//novo no
node = splay(matricula,node.left);
// o filho direito do no anterior passa a ser o novo no filho direito
node.right = temp.right;
}
}
// retorna novo no da Splay Tree depois de remover
return node;
}
//se a matricula nao estiver presente, a funcao considera o ultimo no acecedido
//modifica a arvore e retorna nova raiz
private Node splay(String matricula, Node no) {
if (no == null){
return null;
}
//matricula no lado esquerdo da sub arvore
if (matricula.compareTo(no.matricula) < 0) {
//matricula nao esta na arvore
if (no.left == null) {
return no;
}
if (matricula.compareTo(no.left.matricula) < 0) {
//matricula fica como raiz de left-left
no.left.left = splay(matricula,no.left.left);
no = rightRotation(no);
}
else if (matricula.compareTo(no.left.matricula) > 0) {
//raiz de left-right
no.left.right = splay(matricula, no.left.right);
if (no.left.right != null) {
no.left = leftRotate(no.left);
}
}
if (no.left == null){
return no;
} else{
return rightRotation(no);
}
}
// lado direito da sub arvore
else if (matricula.compareTo(no.matricula) > 0) {
if (no.right == null) {
return no;
}
if (matricula.compareTo(no.right.matricula) < 0){
// RIGHT LEFT
no.right.left = splay(matricula,no.right.left);
if (no.right.left != null) {
no.right = rightRotation(no.right);
}
}
else if (matricula.compareTo(no.right.matricula) > 0) {
// RIGHT RIGHT
no.right.right = splay(matricula, no.right.right);
no = leftRotate(no);
}
if (no.right == null) {
return no;
}else{
return leftRotate(no);
}
}
return no;
}
protected void printInOrder(){
printInOrder(root);
}
private void printInOrder(Node no){
if (no==null)
return;
printInOrder(no.left);
System.out.println(no.matricula + " VALOR EM DIVIDA " + no.divida);
printInOrder(no.right);
}
}
public class TP2_TarefaD{
public static void main(String[] arguments) {
String input, comando;
int valor;
String matricula;
StringTokenizer st;
Splay tree = new Splay();
do {
input = readLn(200);
st= new StringTokenizer(input.trim());
comando = st.nextToken();
if (comando.equals("PORTICO")){
matricula = st.nextToken();
valor = Integer.parseInt(st.nextToken());
tree.add (matricula, valor);
Node no = tree.get(matricula);
if (no.divida == 0){
tree.remove(matricula);
}
}
else if (comando.equals("PAGAMENTO")){
matricula = st.nextToken();
valor = Integer.parseInt(st.nextToken());
tree.add (matricula, (valor * -1));
Node no = tree.get(matricula);
if (no.divida == 0){
tree.remove(matricula);
}
}
else if (comando.equals("SALDO")){
matricula = st.nextToken();
Node no = tree.get(matricula);
if (no==null)
System.out.println(matricula + " SEM REGISTO");
else
System.out.println(matricula + " VALOR EM DIVIDA " + no.divida);
}
else if (comando.equals("LISTA")){
tree.printInOrder();
}
else if (comando.equals("TERMINA"))
return;
} while (true);
}
private static String readLn (int maxLg){
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try {
while (lg < maxLg){
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e){
return (null);
}
if ((car < 0) && (lg == 0)) return (null);
return (new String (lin, 0, lg));
}
}