Skip to content

Commit b7daaaa

Browse files
committed
Arvore AVL added
1 parent 6ea723a commit b7daaaa

File tree

4 files changed

+4377
-0
lines changed

4 files changed

+4377
-0
lines changed

src/ArvoreAVL/Main.java

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import java.io.*;
2+
import java.util.ArrayList;
3+
import java.util.Scanner;
4+
5+
class Jogador {
6+
private final int id, altura, peso, anoNascimento;
7+
private final String nome, universidade, cidadeNascimento, estadoNascimento;
8+
9+
Jogador(int id, String nome, int altura, int peso, String universidade, int anoNascimento, String cidadeNascimento,
10+
String estadoNascimento) {
11+
this.id = id;
12+
this.altura = altura;
13+
this.peso = peso;
14+
this.anoNascimento = anoNascimento;
15+
this.nome = nome;
16+
this.universidade = universidade;
17+
this.cidadeNascimento = cidadeNascimento;
18+
this.estadoNascimento = estadoNascimento;
19+
}
20+
21+
public String getNome() {
22+
return nome;
23+
}
24+
}
25+
26+
class NoAVL {
27+
Jogador jogador;
28+
int altura;
29+
NoAVL esq, dir;
30+
31+
NoAVL(Jogador jogador) {
32+
this.jogador = jogador;
33+
this.altura = 1;
34+
this.esq = this.dir = null;
35+
}
36+
}
37+
38+
class ArvoreAVL {
39+
private NoAVL raiz;
40+
public int numComparacoes;
41+
42+
int altura(NoAVL N) {
43+
if (N == null)
44+
return 0;
45+
return N.altura;
46+
}
47+
48+
int max(int a, int b) {
49+
return (a > b) ? a : b;
50+
}
51+
52+
NoAVL rotacaoDireita(NoAVL y) {
53+
NoAVL x = y.esq;
54+
NoAVL T2 = x.dir;
55+
x.dir = y;
56+
y.esq = T2;
57+
y.altura = max(altura(y.esq), altura(y.dir)) + 1;
58+
x.altura = max(altura(x.esq), altura(x.dir)) + 1;
59+
return x;
60+
}
61+
62+
NoAVL rotacaoEsquerda(NoAVL x) {
63+
NoAVL y = x.dir;
64+
NoAVL T2 = y.esq;
65+
y.esq = x;
66+
x.dir = T2;
67+
x.altura = max(altura(x.esq), altura(x.dir)) + 1;
68+
y.altura = max(altura(y.esq), altura(y.dir)) + 1;
69+
return y;
70+
}
71+
72+
int getBalance(NoAVL N) {
73+
if (N == null)
74+
return 0;
75+
return altura(N.esq) - altura(N.dir);
76+
}
77+
78+
NoAVL inserir(NoAVL no, Jogador jogador) {
79+
if (no == null)
80+
return (new NoAVL(jogador));
81+
if (jogador.getNome().compareTo(no.jogador.getNome()) < 0)
82+
no.esq = inserir(no.esq, jogador);
83+
else if (jogador.getNome().compareTo(no.jogador.getNome()) > 0)
84+
no.dir = inserir(no.dir, jogador);
85+
else
86+
return no;
87+
88+
no.altura = 1 + max(altura(no.esq), altura(no.dir));
89+
int balance = getBalance(no);
90+
91+
if (balance > 1 && jogador.getNome().compareTo(no.esq.jogador.getNome()) < 0)
92+
return rotacaoDireita(no);
93+
94+
if (balance < -1 && jogador.getNome().compareTo(no.dir.jogador.getNome()) > 0)
95+
return rotacaoEsquerda(no);
96+
97+
if (balance > 1 && jogador.getNome().compareTo(no.esq.jogador.getNome()) > 0) {
98+
no.esq = rotacaoEsquerda(no.esq);
99+
return rotacaoDireita(no);
100+
}
101+
102+
if (balance < -1 && jogador.getNome().compareTo(no.dir.jogador.getNome()) < 0) {
103+
no.dir = rotacaoDireita(no.dir);
104+
return rotacaoEsquerda(no);
105+
}
106+
return no;
107+
}
108+
109+
void inserir(Jogador jogador) {
110+
raiz = inserir(raiz, jogador);
111+
}
112+
113+
boolean pesquisar(String nome) {
114+
return pesquisar(raiz, nome);
115+
}
116+
117+
private boolean pesquisar(NoAVL no, String nome) {
118+
if (no == null) {
119+
return false;
120+
}
121+
numComparacoes++;
122+
System.out.printf("%s ", no.jogador.getNome());
123+
if (nome.equals(no.jogador.getNome())) {
124+
return true;
125+
}
126+
if (nome.compareTo(no.jogador.getNome()) < 0) {
127+
return pesquisar(no.esq, nome);
128+
}
129+
return pesquisar(no.dir, nome);
130+
}
131+
}
132+
133+
public class Main {
134+
private static final String FIM = "FIM";
135+
private ArrayList<Jogador> jogadores = new ArrayList<>();
136+
137+
public static void main(String[] args) throws IOException {
138+
Main aplicacao = new Main();
139+
aplicacao.carregarJogadores("/tmp/jogadores.txt");
140+
141+
ArvoreAVL arvore = new ArvoreAVL();
142+
Scanner scanner = new Scanner(System.in);
143+
inserirJogadores(scanner, arvore, aplicacao);
144+
145+
long startTime = System.currentTimeMillis();
146+
pesquisarJogadores(scanner, arvore);
147+
long endTime = System.currentTimeMillis();
148+
scanner.close();
149+
150+
gerarArquivoLog(endTime - startTime, arvore.numComparacoes);
151+
}
152+
153+
private static void inserirJogadores(Scanner scanner, ArvoreAVL arvore, Main aplicacao) {
154+
String entrada;
155+
do {
156+
entrada = scanner.nextLine();
157+
if (!entrada.equals(FIM)) {
158+
int id = Integer.parseInt(entrada);
159+
Jogador jogador = aplicacao.jogadores.get(id);
160+
arvore.inserir(jogador);
161+
}
162+
} while (!entrada.equals(FIM));
163+
}
164+
165+
private static void pesquisarJogadores(Scanner scanner, ArvoreAVL arvore) {
166+
while (scanner.hasNextLine()) {
167+
String entrada = scanner.nextLine();
168+
if (!entrada.equals(FIM)) {
169+
boolean encontrado = arvore.pesquisar(entrada);
170+
System.out.println(encontrado ? " SIM" : " NAO");
171+
}
172+
}
173+
}
174+
175+
private static void gerarArquivoLog(long tempo, int numComparacoes) throws IOException {
176+
try (FileWriter writer = new FileWriter("793938_arvoreAVL.txt")) {
177+
writer.write("793938\t" + tempo + "\t" + numComparacoes);
178+
}
179+
}
180+
181+
private void carregarJogadores(String arquivo) throws IOException {
182+
try (BufferedReader leitor = new BufferedReader(new FileReader(arquivo))) {
183+
leitor.readLine();
184+
String linha;
185+
while ((linha = leitor.readLine()) != null) {
186+
String[] campos = linha.split(",", -1);
187+
jogadores.add(criarJogador(campos));
188+
}
189+
}
190+
}
191+
192+
private Jogador criarJogador(String[] campos) {
193+
int id = Integer.parseInt(campos[0]);
194+
String nome = campos[1];
195+
int altura = Integer.parseInt(campos[2]);
196+
int peso = Integer.parseInt(campos[3]);
197+
String universidade = campos.length > 4 ? campos[4] : "";
198+
int anoNascimento = Integer.parseInt(campos[5]);
199+
String cidadeNascimento = campos.length > 6 ? campos[6] : "";
200+
String estadoNascimento = campos.length > 7 ? campos[7] : "";
201+
return new Jogador(id, nome, altura, peso, universidade, anoNascimento, cidadeNascimento, estadoNascimento);
202+
}
203+
}

src/ArvoreAVL/pub/pub-in.txt

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
1
2+
104
3+
1047
4+
1087
5+
1124
6+
119
7+
1425
8+
1456
9+
1487
10+
149
11+
1494
12+
1525
13+
1565
14+
1572
15+
1583
16+
1702
17+
1731
18+
1742
19+
1748
20+
1786
21+
1796
22+
1809
23+
1868
24+
1880
25+
193
26+
1998
27+
2011
28+
2035
29+
2083
30+
217
31+
2177
32+
2183
33+
2203
34+
228
35+
401
36+
412
37+
448
38+
479
39+
53
40+
533
41+
557
42+
627
43+
687
44+
721
45+
797
46+
812
47+
848
48+
950
49+
98
50+
FIM
51+
A.J. Bramlett
52+
Aaron Williams
53+
Adrian Smith
54+
Al Ferrari
55+
Alec Kessler
56+
Andy Johnson
57+
Ariel Maughan
58+
Art Burris
59+
Ben McDonald
60+
Ben Poquette
61+
Bill Ebben
62+
Bill Laimbeer
63+
Bob Kinney
64+
Bob Schafer
65+
Bones McKinney
66+
Carl Shaeffer
67+
Cheick Diallo
68+
Chris Harris
69+
Chris Welp
70+
Cliff Barker
71+
Craig Dykema
72+
Curtis Kitchen
73+
Damian Jones
74+
Damjan Rudez
75+
Darnell Mee
76+
Darren Tillis
77+
Darryl Johnson
78+
Dejounte Murray
79+
Devin Booker
80+
Devyn Marble
81+
Dick Schnittker
82+
Dijon Thompson
83+
Donatas Motiejunas
84+
Ed Dahler
85+
Eddie Phillips
86+
Eric Riley
87+
Frank Johnson
88+
Frank Selvy
89+
Gaylon Nickerson
90+
George Reynolds
91+
Glenn McDonald
92+
Harold Pressley
93+
Hassan Whiteside
94+
Herb Williams
95+
Hiram Fuller
96+
Ira Newble
97+
Jack George
98+
Jack Molinas
99+
James Blackwell
100+
Jaren Jackson
101+
Jeff Sanders
102+
Jeremy Evans
103+
Jerry Fleishman
104+
Joe Buckhalter
105+
Joe Caldwell
106+
Joe Young
107+
John Johnson
108+
John Stroud
109+
Johnny Jones
110+
Johnny Orr
111+
Ken Johnson
112+
Ken Norman
113+
Kevin Grevey
114+
Kevin Willis
115+
Larry Drew
116+
Larry Sykes
117+
Leo Kubiak
118+
Lionel Chalmers
119+
Lorenzen Wright
120+
Luigi Datome
121+
Luis Flores
122+
Mamadou N'Diaye
123+
Marcus Fizer
124+
Mark Strickland
125+
Marvin Barnes
126+
Mason Plumlee
127+
Michael Phelps
128+
Michael Stewart
129+
Mike Davis
130+
Montrezl Harrell
131+
Nick Fazekas
132+
Nikola Vucevic
133+
Nolan Smith
134+
Ollie Johnson
135+
Omri Casspi
136+
Othello Hunter
137+
Paul Walther
138+
Peyton Siva
139+
Predrag Drobnjak
140+
Quincy Acy
141+
Ralph Drollinger
142+
Robert Horry
143+
Ron Harper
144+
Russ Schoene
145+
Sarunas Marciulionis*
146+
Stephen Thompson
147+
Thaddeus Young
148+
Toby Kimball
149+
Tony Bennett
150+
Walter Dukes
151+
FIM

0 commit comments

Comments
 (0)