-
Notifications
You must be signed in to change notification settings - Fork 0
/
Árvores balanceadas.java
153 lines (139 loc) · 4.21 KB
/
Árvores balanceadas.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
import java.io.*;
import java.lang.Math.*;
import java.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 No {
private String info;
private No left;
private No right;
private int altura;
public No(String info){
this.info = info;
this.altura = 0;
}
public String getInfo(){
return this.info;
}
public No getLeft(){
return this.left;
}
public No getRight(){
return this.right;
}
public void setLeft(No left){
this.left = left;
}
public void setRight(No right){
this.right = right;
}
public int getAltura(){
return this.altura;
}
public void setAltura(int altura){
this.altura = altura;
}
}
class ArvoreBinaria {
protected No a;
public void add(String e) {
if(a == null){
a = new No(e);
}else{
No temp = a;
while(temp!=null){
if(e.compareTo(temp.getInfo())>0){
if(temp.getRight()==null){
temp.setRight(new No(e));
break;
}else{
temp = temp.getRight();
}
}else if(e.compareTo(temp.getInfo())<=0){
if(temp.getLeft()==null){
temp.setLeft(new No(e));
break;
}else{
temp = temp.getLeft();
}
}else{
break;
}
}
}
}
public void setAltura(No 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);
}
}
public Boolean isBalanced(No a){
Boolean r;
if(a.getRight()==null && a.getLeft()!=null){
if(a.getLeft().getAltura()>0){
r = false;
}else{
r = true;
}
}else if(a.getRight()!=null && a.getLeft()==null){
if(a.getRight().getAltura()>0){
r = false;
}else{
r = true;
}
}else if(a.getRight()!=null && a.getLeft()!=null){
if(Math.abs(a.getRight().getAltura()-a.getLeft().getAltura())>1){
r = false;
}else{
r = true;
}
}else{
r = true;
}
return r;
}
}
class Result {
private static ArvoreBinaria arv = new ArvoreBinaria();
public static String NodeInformation(String values) {
String r = "";
String[] infos = values.trim().split(" ");
for(int i=0;i<infos.length;i++){
arv.add(infos[i]);
}
arv.setAltura(arv.a);
if(arv.isBalanced(arv.a)){
r = "E balanceada";
}else{
r = "Nao e balanceada";
}
return r;
}
}
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 treeNodes = bufferedReader.readLine();
String result = Result.NodeInformation(treeNodes);
bufferedWriter.write(result);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}