-
Notifications
You must be signed in to change notification settings - Fork 0
/
Árvore AVL.java
300 lines (282 loc) · 9.78 KB
/
Árvore AVL.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import java.io.*;
import java.math.*;
import java.lang.Math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Node{
private int value;
private Node left;
private Node right;
private int altura;
public Node(int value){
this.value = value;
this.altura = 0;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public Node getLeft(){
return this.left;
}
public Node getRight(){
return this.right;
}
public void setLeft(Node left){
this.left = left;
}
public void setRight(Node right){
this.right = right;
}
public int getAltura(){
return this.altura;
}
public void setAltura(int altura){
this.altura = altura;
}
}
class BinaryTree {
private Node initialNode;
public void add(int elem) {
if(initialNode==null){
initialNode = new Node(elem);
}else{
Node temp = initialNode;
while(temp!=null){
if(elem>temp.getValue()){
if(temp.getRight()==null){
temp.setRight(new Node(elem));
setAltura(initialNode);
if(checkBalance(initialNode)!=null){
fixTree();
}
break;
}else{
temp = temp.getRight();
}
}else if(elem<temp.getValue()){
if(temp.getLeft()==null){
temp.setLeft(new Node(elem));
setAltura(initialNode);
if(checkBalance(initialNode)!=null){
fixTree();
}
break;
}else{
temp = temp.getLeft();
}
}else{
break;
}
}
}
}
public void setAltura(Node a){
if(a.getRight()!=null && a.getLeft()!=null){
setAltura(a.getRight());
setAltura(a.getLeft());
a.setAltura((a.getRight().getAltura()>a.getLeft().getAltura()?a.getRight().getAltura():a.getLeft().getAltura())+1);
}else if(a.getRight()!=null){
setAltura(a.getRight());
a.setAltura(a.getRight().getAltura()+1);
}else if(a.getLeft()!=null){
setAltura(a.getLeft());
a.setAltura(a.getLeft().getAltura()+1);
}
//System.out.println("altura de: " + a.getValue() + " = " + a.getAltura());
}
public Node checkBalance(Node a){
Node r = null;
if(a.getRight()==null && a.getLeft()!=null){
if(a.getLeft().getAltura()>0){
if(checkBalance(a.getLeft())==null){
r = a;
}else{
r = checkBalance(a.getLeft());
}
}
}else if(a.getRight()!=null && a.getLeft()==null){
if(a.getRight().getAltura()>0){
if(checkBalance(a.getRight())==null){
r = a;
}else{
r = checkBalance(a.getRight());
}
}
}else if(a.getRight()!=null && a.getLeft()!=null){
if(Math.abs(a.getRight().getAltura()-a.getLeft().getAltura())>1){
if(checkBalance(a.getRight())!=null){
r = checkBalance(a.getRight());
}else if(checkBalance(a.getLeft())!=null){
r = checkBalance(a.getLeft());
}else{
r = a;
}
}else{
if(checkBalance(a.getRight())!=null){
r = checkBalance(a.getRight());
}else if(checkBalance(a.getLeft())!=null){
r = checkBalance(a.getLeft());
}else{
r = null;
}
}
}else{
r = null;
}
return r;
}
public void fixTree(){
Node a, b, c = checkBalance(initialNode);
//System.out.println("estou consertando esse vagabundo:" + c.getValue());
Node paiDoC = initialNode;
while(paiDoC!=null){
if(paiDoC.getValue()<c.getValue()){
if(paiDoC.getRight() == c){
break;
}else{
paiDoC = paiDoC.getRight();
}
}else if(paiDoC.getValue()>c.getValue()){
if(paiDoC.getLeft() == c){
break;
}else{
paiDoC = paiDoC.getLeft();
}
}else{
paiDoC = null;
}
}
Boolean paiDoRight = false;
if(paiDoC!=null){
//System.out.println("paidoC: " + paiDoC.getValue());
if(paiDoC.getRight()==c){
paiDoRight = true;
}
}
if(c.getRight() == null){
b = c.getLeft();
}else if(c.getLeft() == null){
b = c.getRight();
}else{
b = (c.getRight().getAltura()>c.getLeft().getAltura())?c.getRight():c.getLeft();
}
if(b.getRight() == null){
a = b.getLeft();
}else if(b.getLeft() == null){
a = b.getRight();
}else{
a = (b.getRight().getAltura()>b.getLeft().getAltura())?b.getRight():b.getLeft();
}
Node t0, t1, t2, t3;
if(b == c.getRight()){
t0 = c.getLeft();
if(a == b.getRight()){
t1 = b.getLeft();
t2 = a.getLeft();
t3 = a.getRight();
}else{
t1 = a.getLeft();
t2 = a.getRight();
t3 = b.getRight();
}
}else{
t3 = c.getRight();
if(a == b.getRight()){
t0 = b.getLeft();
t1 = a.getLeft();
t2 = a.getRight();
}else{
t0 = a.getLeft();
t1 = a.getRight();
t2 = b.getRight();
}
}
if(c.getValue()>b.getValue() && a.getValue()>b.getValue()){
Node temp = b;
b = a;
a = temp;
}else if(c.getValue()<b.getValue() && a.getValue()<b.getValue()){
Node temp1 = a, temp2 = b;
a = c;
b = temp1;
c = temp2;
}else if(c.getValue()<b.getValue() && a.getValue()>b.getValue()){
Node temp = c;
c = a;
a = temp;
}
if(paiDoC==null){
initialNode = new Node(b.getValue());
initialNode.setLeft(new Node(a.getValue()));
initialNode.getLeft().setLeft(t0);
initialNode.getLeft().setRight(t1);
initialNode.setRight(new Node(c.getValue()));
initialNode.getRight().setLeft(t2);
initialNode.getRight().setRight(t3);
}else{
if(paiDoRight){
paiDoC.setRight(new Node(b.getValue()));
paiDoC.getRight().setLeft(new Node(a.getValue()));
paiDoC.getRight().getLeft().setLeft(t0);
paiDoC.getRight().getLeft().setRight(t1);
paiDoC.getRight().setRight(new Node(c.getValue()));
paiDoC.getRight().getRight().setLeft(t2);
paiDoC.getRight().getRight().setRight(t3);
}else{
paiDoC.setLeft(new Node(b.getValue()));
paiDoC.getLeft().setLeft(new Node(a.getValue()));
paiDoC.getLeft().getLeft().setLeft(t0);
paiDoC.getLeft().getLeft().setRight(t1);
paiDoC.getLeft().setRight(new Node(c.getValue()));
paiDoC.getLeft().getRight().setLeft(t2);
paiDoC.getLeft().getRight().setRight(t3);
}
}
}
private String treeSearch(Node actual) {
if (actual == null) {
return "(null)";
}
if(actual.getLeft() == null && actual.getRight() == null )
return "" + actual.getValue();
return String.format("l(%s) - %s - r(%s)", treeSearch(actual.getLeft()), actual.getValue(), treeSearch(actual.getRight()));
}
public String toString() {
return treeSearch(initialNode);
}
}
class Result {
private static BinaryTree arvore = new BinaryTree();
public static void addToTree(String values) {
String[] infos = values.trim().split(" ");
for(int i=0;i<infos.length;i++){
arvore.add(Integer.valueOf(infos[i]));
}
}
public static String printTree() {
return arvore.toString();
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String addValues = bufferedReader.readLine();
Result.addToTree(addValues);
String result = Result.printTree();
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}