all
This commit is contained in:
408
AVL.java
Normal file
408
AVL.java
Normal file
@@ -0,0 +1,408 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class AVL {
|
||||
private Node root = new Node(null);
|
||||
// necessario para verificacao de quantos nos visitados pela funcao search
|
||||
// (desempenho)
|
||||
private int count = 0;
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
// Construtor
|
||||
public AVL() {
|
||||
this.root = null;
|
||||
}
|
||||
|
||||
// Métodos de inserção e busca
|
||||
public void insert(ProgramaNetflix programa) {
|
||||
root = insertRec(root, programa);
|
||||
}
|
||||
|
||||
private Node insertRec(Node root, ProgramaNetflix programa) {
|
||||
|
||||
if (root == null) {
|
||||
return new Node(programa);
|
||||
}
|
||||
|
||||
if (programa.getId().compareTo(root.programa.getId()) < 0) {
|
||||
root.left = insertRec(root.left, programa);
|
||||
} else if (programa.getId().compareTo(root.programa.getId()) > 0) {
|
||||
root.right = insertRec(root.right, programa);
|
||||
} else {
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// Atualize a altura do nó atual
|
||||
root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
|
||||
|
||||
// Verifique o fator de equilíbrio e faça as rotações necessárias
|
||||
int balance = getBalance(root);
|
||||
|
||||
// Casos de rotação
|
||||
// Esquerda-Esquerda
|
||||
if (balance > 1 && programa.getId().compareTo(root.left.programa.getId()) < 0) {
|
||||
return rotateRight(root);
|
||||
}
|
||||
// Direita-Direita
|
||||
if (balance < -1 && programa.getId().compareTo(root.right.programa.getId()) > 0) {
|
||||
return rotateLeft(root);
|
||||
}
|
||||
// Esquerda-Direita
|
||||
if (balance > 1 && programa.getId().compareTo(root.left.programa.getId()) > 0) {
|
||||
root.left = rotateLeft(root.left);
|
||||
return rotateRight(root);
|
||||
}
|
||||
// Direita-Esquerda
|
||||
if (balance < -1 && programa.getId().compareTo(root.right.programa.getId()) < 0) {
|
||||
root.right = rotateRight(root.right);
|
||||
return rotateLeft(root);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public ProgramaNetflix search(String id) {
|
||||
count = 1;
|
||||
return searchRec(root, id);
|
||||
}
|
||||
|
||||
private ProgramaNetflix searchRec(Node root, String id) {
|
||||
count++;
|
||||
// correspondente
|
||||
if (root == null || root.programa.getId().equals(id)) {
|
||||
return root != null ? root.programa : null;
|
||||
}
|
||||
|
||||
// Se o ID a ser pesquisado for menor que o ID do nó atual, busca na subárvore
|
||||
// esquerda
|
||||
if (id.compareTo(root.programa.getId()) < 0) {
|
||||
return searchRec(root.left, id);
|
||||
}
|
||||
// Se o ID a ser pesquisado for maior que o ID do nó atual, busca na subárvore
|
||||
// direita
|
||||
return searchRec(root.right, id);
|
||||
}
|
||||
|
||||
public int height() {
|
||||
return height(root);
|
||||
}
|
||||
|
||||
// Método privado para calcular a altura da árvore
|
||||
private int height(Node root) {
|
||||
if (root == null) {
|
||||
return -1; // Árvore vazia tem altura -1
|
||||
}
|
||||
|
||||
int leftHeight = height(root.left);
|
||||
int rightHeight = height(root.right);
|
||||
|
||||
// A altura da árvore é o máximo entre as alturas das subárvores esquerda e
|
||||
// direita
|
||||
return Math.max(leftHeight, rightHeight) + 1;
|
||||
}
|
||||
|
||||
private int getHeight(Node node) {
|
||||
return (node != null) ? node.height : 0;
|
||||
}
|
||||
|
||||
private int getBalance(Node node) {
|
||||
return (node != null) ? getHeight(node.left) - getHeight(node.right) : 0;
|
||||
}
|
||||
|
||||
private Node rotateRight(Node y) {
|
||||
Node x = y.left;
|
||||
Node T2 = x.right;
|
||||
|
||||
// Realiza a rotação
|
||||
x.right = y;
|
||||
y.left = T2;
|
||||
|
||||
// Atualiza alturas
|
||||
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
|
||||
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
private Node rotateLeft(Node x) {
|
||||
Node y = x.right;
|
||||
Node T2 = y.left;
|
||||
|
||||
// Realiza a rotação
|
||||
y.left = x;
|
||||
x.right = T2;
|
||||
|
||||
// Atualiza alturas
|
||||
x.height = Math.max(getHeight(x.left), getHeight(x.right)) + 1;
|
||||
y.height = Math.max(getHeight(y.left), getHeight(y.right)) + 1;
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
public void remove(String id) {
|
||||
root = removeRec(root, id);
|
||||
}
|
||||
|
||||
private Node removeRec(Node root, String id) {
|
||||
if (root == null) {
|
||||
return root;
|
||||
}
|
||||
|
||||
// Encontra o nó a ser removido
|
||||
if (id.compareTo(root.programa.getId()) < 0) {
|
||||
root.left = removeRec(root.left, id);
|
||||
} else if (id.compareTo(root.programa.getId()) > 0) {
|
||||
root.right = removeRec(root.right, id);
|
||||
} else {
|
||||
// Nó com um filho ou sem filhos
|
||||
if (root.left == null) {
|
||||
return root.right;
|
||||
} else if (root.right == null) {
|
||||
return root.left;
|
||||
}
|
||||
|
||||
// Nó com dois filhos: encontra o sucessor in-order (menor nó na subárvore
|
||||
// direita)
|
||||
root.programa = minValue(root.right);
|
||||
|
||||
// Remove o sucessor in-order
|
||||
root.right = removeRec(root.right, root.programa.getId());
|
||||
}
|
||||
|
||||
// Atualiza a altura do nó atual
|
||||
root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
|
||||
|
||||
// Verifica o fator de equilíbrio e realiza as rotações necessárias
|
||||
int balance = getBalance(root);
|
||||
|
||||
// Casos de rotação
|
||||
// Esquerda-Esquerda
|
||||
if (balance > 1 && getBalance(root.left) >= 0) {
|
||||
return rotateRight(root);
|
||||
}
|
||||
// Direita-Direita
|
||||
if (balance < -1 && getBalance(root.right) <= 0) {
|
||||
return rotateLeft(root);
|
||||
}
|
||||
// Esquerda-Direita
|
||||
if (balance > 1 && getBalance(root.left) < 0) {
|
||||
root.left = rotateLeft(root.left);
|
||||
return rotateRight(root);
|
||||
}
|
||||
// Direita-Esquerda
|
||||
if (balance < -1 && getBalance(root.right) > 0) {
|
||||
root.right = rotateRight(root.right);
|
||||
return rotateLeft(root);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private ProgramaNetflix minValue(Node root) {
|
||||
ProgramaNetflix minValue = root.programa;
|
||||
while (root.left != null) {
|
||||
minValue = root.left.programa;
|
||||
root = root.left;
|
||||
}
|
||||
return minValue;
|
||||
}
|
||||
|
||||
// 1 ///////////////////////////////////////////
|
||||
public void pontuacaoMediaPorPais() {
|
||||
Map<String, Double> somaPontuacaoPorPais = new HashMap<>();
|
||||
Map<String, Integer> contagemPorPais = new HashMap<>();
|
||||
|
||||
inorderCalculaPontuacaoPorPais(root, somaPontuacaoPorPais, contagemPorPais);
|
||||
|
||||
// Calcula a média
|
||||
Map<String, Double> mediaPorPais = new HashMap<>();
|
||||
for (Map.Entry<String, Double> entry : somaPontuacaoPorPais.entrySet()) {
|
||||
String pais = entry.getKey();
|
||||
double somaPontuacao = entry.getValue();
|
||||
int contagem = contagemPorPais.get(pais);
|
||||
double media = contagem > 0 ? somaPontuacao / contagem : 0.0;
|
||||
mediaPorPais.put(pais, media);
|
||||
}
|
||||
|
||||
// Ordena os resultados por pontuação média em ordem decrescente
|
||||
List<Map.Entry<String, Double>> sortedEntries = mediaPorPais.entrySet()
|
||||
.stream()
|
||||
.sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Exibe os 10 primeiros países
|
||||
int limite = Math.min(sortedEntries.size(), 10);
|
||||
for (int i = 0; i < limite; i++) {
|
||||
Map.Entry<String, Double> entry = sortedEntries.get(i);
|
||||
System.out.println(entry.getKey() + ": " + Double.parseDouble(String.format("%.3f", entry.getValue())));
|
||||
}
|
||||
}
|
||||
|
||||
private void inorderCalculaPontuacaoPorPais(Node no, Map<String, Double> somaPontuacaoPorPais,
|
||||
Map<String, Integer> contagemPorPais) {
|
||||
if (no != null) {
|
||||
inorderCalculaPontuacaoPorPais(no.left, somaPontuacaoPorPais, contagemPorPais);
|
||||
|
||||
List<String> paises = no.programa.getProductionCountries();
|
||||
double pontuacaoIMDB = no.programa.getImdb_score();
|
||||
|
||||
for (String pais : paises) {
|
||||
// Atualiza a soma da pontuação e a contagem para cada país
|
||||
somaPontuacaoPorPais.put(pais, somaPontuacaoPorPais.getOrDefault(pais, 0.0) + pontuacaoIMDB);
|
||||
contagemPorPais.put(pais, contagemPorPais.getOrDefault(pais, 0) + 1);
|
||||
}
|
||||
|
||||
inorderCalculaPontuacaoPorPais(no.right, somaPontuacaoPorPais, contagemPorPais);
|
||||
}
|
||||
}
|
||||
|
||||
// 2 ///////////////////////////////////////////
|
||||
public void filmesPorDecada() {
|
||||
Map<String, Integer> filmesPorDecada = new HashMap<>();
|
||||
inorderContadorPorDecada(root, filmesPorDecada);
|
||||
|
||||
// Exibe os resultados
|
||||
filmesPorDecada.forEach((decada, quantidade) -> {
|
||||
System.out.println(decada + ": " + quantidade);
|
||||
});
|
||||
}
|
||||
|
||||
private void inorderContadorPorDecada(Node no, Map<String, Integer> filmesPorDecada) {
|
||||
if (no != null) {
|
||||
inorderContadorPorDecada(no.left, filmesPorDecada);
|
||||
|
||||
int anoLancamento = no.programa.getRelease_year();
|
||||
String decada = getDecada(anoLancamento);
|
||||
|
||||
// Incrementa a contagem para a década correspondente
|
||||
filmesPorDecada.put(decada, filmesPorDecada.getOrDefault(decada, 0) + 1);
|
||||
|
||||
inorderContadorPorDecada(no.right, filmesPorDecada);
|
||||
}
|
||||
}
|
||||
|
||||
private String getDecada(int ano) {
|
||||
int decada = (ano / 10) * 10;
|
||||
return decada + "s";
|
||||
}
|
||||
|
||||
// 3 ///////////////////////////////////////////
|
||||
public void mediaDuracaoPorClassificacao() {
|
||||
Map<String, Double> somaDuracaoPorClassificacao = new HashMap<>();
|
||||
Map<String, Integer> contagemPorClassificacao = new HashMap<>();
|
||||
inorderMediaDuracaoPorClassificacao(root, somaDuracaoPorClassificacao, contagemPorClassificacao);
|
||||
|
||||
// Exibe os resultados
|
||||
somaDuracaoPorClassificacao.forEach((classificacao, somaDuracao) -> {
|
||||
int contagem = contagemPorClassificacao.get(classificacao);
|
||||
double media = contagem > 0 ? somaDuracao / contagem : 0.0;
|
||||
Double mediaf = Double.parseDouble(String.format("%.2f", media));
|
||||
System.out.println(classificacao + ": " + mediaf + " minutos em média");
|
||||
});
|
||||
}
|
||||
|
||||
private void inorderMediaDuracaoPorClassificacao(Node no, Map<String, Double> somaDuracaoPorClassificacao,
|
||||
Map<String, Integer> contagemPorClassificacao) {
|
||||
if (no != null) {
|
||||
inorderMediaDuracaoPorClassificacao(no.left, somaDuracaoPorClassificacao, contagemPorClassificacao);
|
||||
|
||||
String classificacao = no.programa.getAge_certification();
|
||||
int duracao = no.programa.getRuntime();
|
||||
|
||||
// Atualiza a soma da duração e a contagem para a classificação indicativa
|
||||
somaDuracaoPorClassificacao.put(classificacao,
|
||||
somaDuracaoPorClassificacao.getOrDefault(classificacao, 0.0) + duracao);
|
||||
contagemPorClassificacao.put(classificacao, contagemPorClassificacao.getOrDefault(classificacao, 0) + 1);
|
||||
|
||||
inorderMediaDuracaoPorClassificacao(no.right, somaDuracaoPorClassificacao, contagemPorClassificacao);
|
||||
}
|
||||
}
|
||||
|
||||
// 4 ///////////////////////////////////////////
|
||||
|
||||
public void percentualPorClassificacaoEtaria() {
|
||||
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
|
||||
inorderContador(root, map);
|
||||
|
||||
int total = map.values().stream()
|
||||
.mapToInt(Integer::intValue)
|
||||
.sum();
|
||||
|
||||
map.forEach((certificacao, quantidade) -> {
|
||||
double percentual = (double) quantidade / total * 100;
|
||||
Double percentualf = Double.parseDouble(String.format("%.2f", percentual));
|
||||
System.out.println(certificacao + ": " + percentualf + "%");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void inorderContador(Node no, Map<String, Integer> map) {
|
||||
if (no != null) {
|
||||
inorderContador(no.left, map);
|
||||
|
||||
String certificacao = no.programa.getAge_certification();
|
||||
int qtd = map.getOrDefault(certificacao, 0);
|
||||
map.put(certificacao, qtd + 1);
|
||||
|
||||
inorderContador(no.right, map);
|
||||
}
|
||||
}
|
||||
|
||||
// 5 ///////////////////////////////////////////
|
||||
|
||||
public void paisesComMaisTitulos() {
|
||||
Map<String, Integer> contagemPorPais = new HashMap<>();
|
||||
inorderContadorPaises(root, contagemPorPais);
|
||||
|
||||
// Ordena o mapa por contagem em ordem decrescente
|
||||
List<Map.Entry<String, Integer>> sortedEntries = contagemPorPais.entrySet()
|
||||
.stream()
|
||||
.sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// Exibe os 10 primeiros países e suas contagens
|
||||
int limite = Math.min(sortedEntries.size(), 10);
|
||||
for (int i = 0; i < limite; i++) {
|
||||
Map.Entry<String, Integer> entry = sortedEntries.get(i);
|
||||
System.out.println(entry.getKey() + ": " + entry.getValue() + " títulos");
|
||||
}
|
||||
}
|
||||
|
||||
private void inorderContadorPaises(Node no, Map<String, Integer> contagemPorPais) {
|
||||
if (no != null) {
|
||||
inorderContadorPaises(no.left, contagemPorPais);
|
||||
|
||||
List<String> paises = no.programa.getProductionCountries();
|
||||
|
||||
// Atualiza a contagem para cada país na lista
|
||||
for (String pais : paises) {
|
||||
contagemPorPais.put(pais, contagemPorPais.getOrDefault(pais, 0) + 1);
|
||||
}
|
||||
|
||||
inorderContadorPaises(no.right, contagemPorPais);
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> dataToStringList() {
|
||||
List<String> result = new ArrayList<>();
|
||||
dataToStringListRec(root, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void dataToStringListRec(Node root, List<String> result) {
|
||||
if (root != null) {
|
||||
dataToStringListRec(root.left, result);
|
||||
result.add(root.programa.toString());
|
||||
dataToStringListRec(root.right, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
141
BST.java
Normal file
141
BST.java
Normal file
@@ -0,0 +1,141 @@
|
||||
public class BST {
|
||||
private Node root;
|
||||
// necessario para verificacao de quantos nos visitados pela funcao search
|
||||
// (desempenho)
|
||||
private int count = 0;
|
||||
|
||||
// Construtor
|
||||
public BST() {
|
||||
this.root = null;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
// Métodos de inserção e busca
|
||||
public void insert(ProgramaNetflix programa) {
|
||||
root = insertRec(root, programa);
|
||||
}
|
||||
|
||||
// Método para encontrar o valor mínimo na árvore
|
||||
public ProgramaNetflix minValue() {
|
||||
return minValue(root);
|
||||
}
|
||||
|
||||
// Método privado para encontrar o valor mínimo na subárvore
|
||||
|
||||
private Node insertRec(Node root, ProgramaNetflix programa) {
|
||||
if (root == null) {
|
||||
root = new Node(programa);
|
||||
return root;
|
||||
}
|
||||
|
||||
// Adicione a lógica de comparação para decidir em qual subárvore inserir
|
||||
if (programa.getId().compareTo(root.programa.getId()) < 0) {
|
||||
root.left = insertRec(root.left, programa);
|
||||
} else if (programa.getId().compareTo(root.programa.getId()) > 0) {
|
||||
root.right = insertRec(root.right, programa);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public ProgramaNetflix search(String id) {
|
||||
count = 0;
|
||||
return searchRec(root, id);
|
||||
}
|
||||
|
||||
private ProgramaNetflix searchRec(Node root, String id) {
|
||||
count++;
|
||||
// Caso base: a árvore ou subárvore está vazia ou encontramos o nó com o ID
|
||||
// correspondente
|
||||
if (root == null || root.programa.getId().equals(id)) {
|
||||
return (root != null) ? root.programa : null;
|
||||
}
|
||||
|
||||
// Se o ID a ser pesquisado for menor que o ID do nó atual, busca na subárvore
|
||||
// esquerda
|
||||
if (id.compareTo(root.programa.getId()) < 0) {
|
||||
return searchRec(root.left, id);
|
||||
}
|
||||
|
||||
// Se o ID a ser pesquisado for maior que o ID do nó atual, busca na subárvore
|
||||
// direita
|
||||
return searchRec(root.right, id);
|
||||
}
|
||||
|
||||
// Método para calcular a altura da árvore
|
||||
public int height() {
|
||||
return height(root);
|
||||
}
|
||||
|
||||
// Método privado para calcular a altura da árvore
|
||||
private int height(Node root) {
|
||||
if (root == null) {
|
||||
return -1; // Árvore vazia tem altura -1
|
||||
}
|
||||
|
||||
int leftHeight = height(root.left);
|
||||
int rightHeight = height(root.right);
|
||||
|
||||
// A altura da árvore é o máximo entre as alturas das subárvores esquerda e
|
||||
// direita
|
||||
return Math.max(leftHeight, rightHeight) + 1;
|
||||
}
|
||||
|
||||
// Outros métodos BST conforme necessário
|
||||
public void remove(String id) {
|
||||
root = removeRec(root, id);
|
||||
}
|
||||
|
||||
private Node removeRec(Node root, String id) {
|
||||
// Caso base: árvore ou subárvore está vazia
|
||||
if (root == null) {
|
||||
return root;
|
||||
}
|
||||
|
||||
// Encontra o nó a ser removido
|
||||
if (id.compareTo(root.programa.getId()) < 0) {
|
||||
root.left = removeRec(root.left, id);
|
||||
} else if (id.compareTo(root.programa.getId()) > 0) {
|
||||
root.right = removeRec(root.right, id);
|
||||
} else {
|
||||
// Nó com um filho ou sem filhos
|
||||
if (root.left == null) {
|
||||
return root.right;
|
||||
} else if (root.right == null) {
|
||||
return root.left;
|
||||
}
|
||||
// Nó com dois filhos: encontra o sucessor in-order (menor nó na subárvore
|
||||
// direit)
|
||||
root.programa = minValue(root.right);
|
||||
|
||||
// Remove o sucessor in-order
|
||||
root.right = removeRec(root.right, root.programa.getId());
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private ProgramaNetflix minValue(Node root) {
|
||||
// O valor mínimo é o nó mais à esquerda na árvore
|
||||
while (root.left != null) {
|
||||
root = root.left;
|
||||
}
|
||||
return root.programa;
|
||||
}
|
||||
|
||||
// Classe interna Node para representar os nós da árvore
|
||||
private class Node {
|
||||
private ProgramaNetflix programa;
|
||||
private Node left;
|
||||
private Node right;
|
||||
|
||||
public Node(ProgramaNetflix programa) {
|
||||
this.programa = programa;
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
EDII-2023.2 - Apl2 (2) (2).pdf
Normal file
BIN
EDII-2023.2 - Apl2 (2) (2).pdf
Normal file
Binary file not shown.
290
Main.java
Normal file
290
Main.java
Normal file
@@ -0,0 +1,290 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static void lerDadosDeArquivo(BST arvoreBST, AVL arvoreAVL) {
|
||||
int incompletecount = 0;
|
||||
|
||||
try (InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("titles.csv");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
br.readLine();
|
||||
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] fields = splitCSVLine(line);
|
||||
|
||||
boolean empty = false;
|
||||
for (String field : fields) {
|
||||
if (field.isEmpty()) {
|
||||
empty = true;
|
||||
}
|
||||
}
|
||||
if (empty) {
|
||||
incompletecount++;
|
||||
} else if (fields.length == 15) {
|
||||
ProgramaNetflix programa = new ProgramaNetflix(
|
||||
fields[0], // id
|
||||
fields[1], // título
|
||||
fields[2], // showType
|
||||
fields[3], // descrição
|
||||
Integer.parseInt(fields[4]), // releaseYear
|
||||
fields[5], // ageCertification
|
||||
Integer.parseInt(fields[6]), // runtime
|
||||
fields[7], // generos
|
||||
fields[8], // productionCountries
|
||||
Float.parseFloat(fields[9]), // temporadas
|
||||
fields[10], // imdbId
|
||||
Float.parseFloat(fields[11]), // imdbScore
|
||||
Float.parseFloat(fields[12]), // imdbVotes
|
||||
Float.parseFloat(fields[13]), // tmdbPopularity
|
||||
Float.parseFloat(fields[14]) // tmdbScore
|
||||
);
|
||||
|
||||
// Inserir o objeto nas árvores BST e AVL
|
||||
arvoreBST.insert(programa);
|
||||
arvoreAVL.insert(programa);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(incompletecount + " linhas ignoradas devido à falta de informações.");
|
||||
}
|
||||
|
||||
private static void treeToFile(List<String> data, String fileName) {
|
||||
try (PrintWriter writer = new PrintWriter(fileName)) {
|
||||
for (String line : data) {
|
||||
writer.println(line);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static String[] splitCSVLine(String line) {
|
||||
List<String> fields = new ArrayList<>();
|
||||
boolean aspas = false;
|
||||
StringBuilder atual = new StringBuilder();
|
||||
for (char c : line.toCharArray()) {
|
||||
if (c == '"') {
|
||||
aspas = !aspas;
|
||||
} else if (c == ',' && !aspas) {
|
||||
fields.add(atual.toString().trim());
|
||||
atual.setLength(0);
|
||||
} else {
|
||||
atual.append(c);
|
||||
}
|
||||
}
|
||||
fields.add(atual.toString().trim());
|
||||
return fields.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BST arvoreBST = new BST();
|
||||
AVL arvoreAVL = new AVL();
|
||||
int opcao;
|
||||
|
||||
String id = null;
|
||||
String titulo;
|
||||
String show_type;
|
||||
String descricao;
|
||||
int release_year;
|
||||
String age_certification;
|
||||
int runtime;
|
||||
String generos;
|
||||
String production_countries;
|
||||
float temporadas;
|
||||
String imdb_id;
|
||||
double imdb_score;
|
||||
float imdb_votes;
|
||||
double tmdb_popularity;
|
||||
double tmdb_score;
|
||||
Scanner entrada = new Scanner(System.in);
|
||||
|
||||
do {
|
||||
System.out.println("\nMenu:");
|
||||
System.out.println("1 - Ler dados do arquivo");
|
||||
System.out.println("2 - Análise de dados");
|
||||
System.out.println("3 - Inserir Programa");
|
||||
System.out.println("4 - Buscar Programa");
|
||||
System.out.println("5 - Remover Programa");
|
||||
System.out.println("6 - Exibir a altura das árvores");
|
||||
System.out.println("7 - Salvar dados em arquivo");
|
||||
System.out.println("8 - Sair");
|
||||
opcao = entrada.nextInt();
|
||||
entrada.nextLine();
|
||||
|
||||
switch (opcao) {
|
||||
case 1: // FUNCIONANDO
|
||||
lerDadosDeArquivo(arvoreBST, arvoreAVL);
|
||||
break;
|
||||
case 2: // FUNCIONANDO
|
||||
// 1
|
||||
System.out.print("10 países com a melhor pontuação no IMDB\n");
|
||||
arvoreAVL.pontuacaoMediaPorPais();
|
||||
|
||||
System.out.print("\nPressione ENTER para continuar...\n");
|
||||
entrada.nextLine();
|
||||
|
||||
// 2
|
||||
System.out.print("Quantidade de filmes por década\n");
|
||||
arvoreAVL.filmesPorDecada();
|
||||
|
||||
System.out.print("\nPressione ENTER para continuar...\n");
|
||||
entrada.nextLine();
|
||||
|
||||
// 3
|
||||
System.out.print("Duração média por classificação etária\n");
|
||||
arvoreAVL.mediaDuracaoPorClassificacao();
|
||||
|
||||
System.out.print("\nPressione ENTER para continuar...\n");
|
||||
entrada.nextLine();
|
||||
|
||||
// 4
|
||||
System.out.print("Percentual de títulos por classificação etária\n");
|
||||
arvoreAVL.percentualPorClassificacaoEtaria();
|
||||
|
||||
System.out.print("\nPressione ENTER para continuar...\n");
|
||||
entrada.nextLine();
|
||||
|
||||
// 5
|
||||
System.out.print("Países com mais títulos\n");
|
||||
arvoreAVL.paisesComMaisTitulos();
|
||||
break;
|
||||
|
||||
case 3: // FUNCIONANDO
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Insira o ID do título");
|
||||
id = sc.nextLine();
|
||||
|
||||
System.out.println("Insira o título do título");
|
||||
titulo = sc.nextLine();
|
||||
|
||||
System.out.println("Insira o tipo (SHOW | MOVIE)");
|
||||
show_type = sc.nextLine();
|
||||
|
||||
System.out.println("Insira a descrição do título");
|
||||
descricao = sc.nextLine();
|
||||
|
||||
System.out.println("Insira o ano de lançamento do título");
|
||||
release_year = sc.nextInt();
|
||||
|
||||
System.out.println("Insira a certificação parental do título");
|
||||
age_certification = sc.nextLine();
|
||||
age_certification = sc.nextLine();
|
||||
|
||||
System.out.println("Insira o tempo (em minutos) do título");
|
||||
runtime = sc.nextInt();
|
||||
|
||||
System.out.println("Insira os gêneros do título");
|
||||
generos = sc.nextLine();
|
||||
generos = sc.nextLine();
|
||||
|
||||
System.out.println(
|
||||
"Insira os países de produção do título (No formato ['Country Code', 'Country Code', 'Country Code'] )");
|
||||
production_countries = sc.nextLine();
|
||||
|
||||
System.out.println("Insira a quantidade de temporadas do título");
|
||||
temporadas = sc.nextFloat();
|
||||
|
||||
System.out.println("Insira o imdb id do título");
|
||||
imdb_id = sc.nextLine();
|
||||
imdb_id = sc.nextLine();
|
||||
|
||||
System.out.println("Insira o imdb score do título");
|
||||
imdb_score = sc.nextDouble();
|
||||
|
||||
System.out.println("Insira o imdb votes do título");
|
||||
imdb_votes = sc.nextFloat();
|
||||
|
||||
System.out.println("Insira o tmdb popularity do título");
|
||||
tmdb_popularity = sc.nextDouble();
|
||||
|
||||
System.out.println("Insira o tmdb_score do título");
|
||||
tmdb_score = sc.nextDouble();
|
||||
|
||||
ProgramaNetflix programa = new ProgramaNetflix(id, titulo, show_type, descricao, release_year,
|
||||
age_certification, runtime, generos, production_countries, temporadas, imdb_id, imdb_score,
|
||||
imdb_votes, tmdb_popularity, tmdb_score);
|
||||
|
||||
arvoreAVL.insert(programa);
|
||||
arvoreBST.insert(programa);
|
||||
System.out.println("Valor na Árvore AVL: " + arvoreAVL.search(id));
|
||||
System.out.println("Valor na Árvore BST: " + arvoreBST.search(id));
|
||||
break;
|
||||
|
||||
case 4: // FUNCIONANDO
|
||||
System.out.print("Informe o ID do programa: ");
|
||||
String programaId = entrada.nextLine().trim().toLowerCase();
|
||||
|
||||
// Buscar na AVL
|
||||
long startTimeAVL = System.nanoTime();
|
||||
arvoreAVL.search(programaId);
|
||||
long endTimeAVL = System.nanoTime();
|
||||
long durationAVL = (endTimeAVL - startTimeAVL);
|
||||
|
||||
// Buscar na BST
|
||||
long startTimeBST = System.nanoTime();
|
||||
arvoreBST.search(programaId);
|
||||
long endTimeBST = System.nanoTime();
|
||||
long durationBST = (endTimeBST - startTimeBST);
|
||||
|
||||
// Exibir as estatísticas da AVL
|
||||
System.out.println("Nodes tocados pela arvore AVL: " + arvoreAVL.getCount());
|
||||
System.out.println("Tempo de execução da busca na AVL: " + durationAVL + " nanossegundos");
|
||||
|
||||
// Exibir as estatísticas da BST
|
||||
System.out.println("Nodes tocados pela arvore BST: " + arvoreBST.getCount());
|
||||
System.out.println("Tempo de execução da busca na BST: " + durationBST + " nanossegundos");
|
||||
|
||||
break;
|
||||
|
||||
case 5: // FUNCIONANDO
|
||||
System.out.print("Informe o ID do programa a ser removido: ");
|
||||
String id_remove = entrada.nextLine().trim().toLowerCase();
|
||||
|
||||
// Remover da AVL
|
||||
arvoreAVL.remove(id_remove);
|
||||
System.out.println("Programa removido da AVL");
|
||||
|
||||
// Remover da BST
|
||||
arvoreBST.remove(id_remove);
|
||||
System.out.println("Programa removido da BST");
|
||||
break;
|
||||
|
||||
case 6:
|
||||
// Exibir a altura da árvore AVL
|
||||
int alturaAVL = arvoreAVL.height();
|
||||
System.out.println("Altura da Árvore AVL: " + alturaAVL);
|
||||
|
||||
// Exibir a altura da árvore BST
|
||||
int alturaBST = arvoreBST.height();
|
||||
System.out.println("Altura da Árvore BST: " + alturaBST);
|
||||
break;
|
||||
|
||||
case 7:
|
||||
System.out.print("Insira o nome do arquivo: ");
|
||||
String fileName = entrada.nextLine();
|
||||
treeToFile(arvoreAVL.dataToStringList(), fileName);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
System.out.println("Saindo...\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opção inválida");
|
||||
}
|
||||
|
||||
} while (opcao != 8);
|
||||
entrada.close();
|
||||
}
|
||||
}
|
||||
11
Node.java
Normal file
11
Node.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Node {
|
||||
protected ProgramaNetflix programa;
|
||||
protected Node left, right;
|
||||
protected int height;
|
||||
|
||||
public Node(ProgramaNetflix programa) {
|
||||
this.programa = programa;
|
||||
this.left = this.right = null;
|
||||
this.height = 1;
|
||||
}
|
||||
}
|
||||
178
ProgramaNetflix.java
Normal file
178
ProgramaNetflix.java
Normal file
@@ -0,0 +1,178 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramaNetflix {
|
||||
|
||||
private String id;
|
||||
private String titulo;
|
||||
private String show_type;
|
||||
private String descricao;
|
||||
private int release_year;
|
||||
private String age_certification;
|
||||
private int runtime;
|
||||
private String generos;
|
||||
private String production_countries;
|
||||
private float temporadas;
|
||||
private String imdb_id;
|
||||
private double imdb_score;
|
||||
private float imdb_votes;
|
||||
private double tmdb_popularity;
|
||||
private double tmdb_score;
|
||||
|
||||
public ProgramaNetflix(String id, String titulo, String show_type, String descricao, int release_year,
|
||||
String age_certification, int runtime, String generos, String production_countries, float temporadas,
|
||||
String imdb_id, double imdb_score, float imdb_votes, double tmdb_popularity, double tmdb_score) {
|
||||
this.id = id;
|
||||
this.titulo = titulo;
|
||||
this.show_type = show_type;
|
||||
this.descricao = descricao;
|
||||
this.release_year = release_year;
|
||||
this.age_certification = age_certification;
|
||||
this.runtime = runtime;
|
||||
this.generos = generos;
|
||||
this.production_countries = production_countries;
|
||||
this.temporadas = temporadas;
|
||||
this.imdb_id = imdb_id;
|
||||
this.imdb_score = imdb_score;
|
||||
this.imdb_votes = imdb_votes;
|
||||
this.tmdb_popularity = tmdb_popularity;
|
||||
this.tmdb_score = tmdb_score;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitulo() {
|
||||
return titulo;
|
||||
}
|
||||
|
||||
public void setTitulo(String titulo) {
|
||||
this.titulo = titulo;
|
||||
}
|
||||
|
||||
public String getShow_type() {
|
||||
return show_type;
|
||||
}
|
||||
|
||||
public void setShow_type(String show_type) {
|
||||
this.show_type = show_type;
|
||||
}
|
||||
|
||||
public String getDescricao() {
|
||||
return descricao;
|
||||
}
|
||||
|
||||
public void setDescricao(String descricao) {
|
||||
this.descricao = descricao;
|
||||
}
|
||||
|
||||
public int getRelease_year() {
|
||||
return release_year;
|
||||
}
|
||||
|
||||
public void setRelease_year(int release_year) {
|
||||
this.release_year = release_year;
|
||||
}
|
||||
|
||||
public String getAge_certification() {
|
||||
return age_certification;
|
||||
}
|
||||
|
||||
public void setAge_certification(String age_certification) {
|
||||
this.age_certification = age_certification;
|
||||
}
|
||||
|
||||
public int getRuntime() {
|
||||
return runtime;
|
||||
}
|
||||
|
||||
public void setRuntime(int runtime) {
|
||||
this.runtime = runtime;
|
||||
}
|
||||
|
||||
public String getGeneros() {
|
||||
return generos;
|
||||
}
|
||||
|
||||
public void setGeneros(String generos) {
|
||||
this.generos = generos;
|
||||
}
|
||||
|
||||
public List<String> getProductionCountries() {
|
||||
List<String> countries = new ArrayList<>();
|
||||
|
||||
if (production_countries != null && !production_countries.isEmpty()) {
|
||||
// Remove os colchetes e aspas e divide a string em valores individuais
|
||||
String[] countryArray = production_countries.replace("[", "").replace("]", "").replace("'", "").split(", ");
|
||||
|
||||
// Adiciona os valores à lista
|
||||
for (String country : countryArray) {
|
||||
countries.add(country);
|
||||
}
|
||||
}
|
||||
|
||||
return countries;
|
||||
}
|
||||
|
||||
public void setProduction_countries(String production_countries) {
|
||||
this.production_countries = production_countries;
|
||||
}
|
||||
|
||||
public float getTemporadas() {
|
||||
return temporadas;
|
||||
}
|
||||
|
||||
public void setTemporadas(int temporadas) {
|
||||
this.temporadas = temporadas;
|
||||
}
|
||||
|
||||
public String getImdb_id() {
|
||||
return imdb_id;
|
||||
}
|
||||
|
||||
public void setImdb_id(String imdb_id) {
|
||||
this.imdb_id = imdb_id;
|
||||
}
|
||||
|
||||
public double getImdb_score() {
|
||||
return Double.parseDouble(String.format("%.1f", imdb_score));
|
||||
}
|
||||
|
||||
public void setImdb_score(double imdb_score) {
|
||||
this.imdb_score = imdb_score;
|
||||
}
|
||||
|
||||
public float getImdb_votes() {
|
||||
return imdb_votes;
|
||||
}
|
||||
|
||||
public void setImdb_votes(int imdb_votes) {
|
||||
this.imdb_votes = imdb_votes;
|
||||
}
|
||||
|
||||
public double getTmdb_popularity() {
|
||||
return tmdb_popularity;
|
||||
}
|
||||
|
||||
public void setTmdb_popularity(double tmdb_popularity) {
|
||||
this.tmdb_popularity = tmdb_popularity;
|
||||
}
|
||||
|
||||
public double getTmdb_score() {
|
||||
return tmdb_score;
|
||||
}
|
||||
|
||||
public void setTmdb_score(double tmdb_score) {
|
||||
this.tmdb_score = tmdb_score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Title=" + titulo + " (" + release_year + ")" + ", imdb_score=" + getImdb_score();
|
||||
}
|
||||
}
|
||||
6003
titles.csv
Normal file
6003
titles.csv
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user