|
| 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 | +} |
0 commit comments