Merge branch 'master' of https://github.com/icei-pucminas/aeds2
This commit is contained in:
parent
9398a9aede
commit
79c88b3c4a
|
|
@ -2,7 +2,8 @@
|
|||
* Arvore binaria de pesquisa
|
||||
* @author Max do Val Machado
|
||||
*/
|
||||
public class AVL {
|
||||
|
||||
public class AVL {
|
||||
private No raiz; // Raiz da arvore.
|
||||
|
||||
/**
|
||||
|
|
@ -15,8 +16,8 @@ public class AVL {
|
|||
/**
|
||||
* Metodo publico iterativo para pesquisar elemento.
|
||||
* @param x Elemento que sera procurado.
|
||||
* @return <code>true</code> se o elemento existir,
|
||||
* <code>false</code> em caso contrario.
|
||||
* @return <code>true</code> se o elemento existir, <code>false</code> em caso
|
||||
* contrario.
|
||||
*/
|
||||
public boolean pesquisar(int x) {
|
||||
return pesquisar(x, raiz);
|
||||
|
|
@ -26,24 +27,21 @@ public class AVL {
|
|||
* Metodo privado recursivo para pesquisar elemento.
|
||||
* @param x Elemento que sera procurado.
|
||||
* @param i No em analise.
|
||||
* @return <code>true</code> se o elemento existir,
|
||||
* <code>false</code> em caso contrario.
|
||||
* @return <code>true</code> se o elemento existir, <code>false</code> em caso
|
||||
* contrario.
|
||||
*/
|
||||
private boolean pesquisar(int x, No i) {
|
||||
boolean resp;
|
||||
boolean resp;
|
||||
if (i == null) {
|
||||
resp = false;
|
||||
|
||||
} else if (x == i.elemento) {
|
||||
resp = true;
|
||||
|
||||
} else if (x < i.elemento) {
|
||||
resp = pesquisar(x, i.esq);
|
||||
|
||||
} else {
|
||||
resp = pesquisar(x, i.dir);
|
||||
}
|
||||
return resp;
|
||||
resp = false;
|
||||
} else if (x == i.elemento) {
|
||||
resp = true;
|
||||
} else if (x < i.elemento) {
|
||||
resp = pesquisar(x, i.esq);
|
||||
} else {
|
||||
resp = pesquisar(x, i.dir);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -109,7 +107,6 @@ public class AVL {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Metodo publico iterativo para inserir elemento.
|
||||
* @param x Elemento a ser inserido.
|
||||
|
|
@ -128,22 +125,17 @@ public class AVL {
|
|||
*/
|
||||
private No inserir(int x, No i) throws Exception {
|
||||
if (i == null) {
|
||||
i = new No(x);
|
||||
|
||||
} else if (x < i.elemento) {
|
||||
i.esq = inserir(x, i.esq);
|
||||
|
||||
} else if (x > i.elemento) {
|
||||
i.dir = inserir(x, i.dir);
|
||||
|
||||
} else {
|
||||
throw new Exception("Erro ao inserir!");
|
||||
}
|
||||
|
||||
i = new No(x);
|
||||
} else if (x < i.elemento) {
|
||||
i.esq = inserir(x, i.esq);
|
||||
} else if (x > i.elemento) {
|
||||
i.dir = inserir(x, i.dir);
|
||||
} else {
|
||||
throw new Exception("Erro ao inserir!");
|
||||
}
|
||||
return balancear(i);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Metodo publico iterativo para remover elemento.
|
||||
* @param x Elemento a ser removido.
|
||||
|
|
@ -154,40 +146,33 @@ public class AVL {
|
|||
}
|
||||
|
||||
/**
|
||||
* Metodo privado recursivo para remover elemento.
|
||||
* Metodo privado recursivo para remover elemento.
|
||||
* @param x Elemento a ser removido.
|
||||
* @param i No em analise.
|
||||
* @return No em analise, alterado ou nao.
|
||||
* @throws Exception Se nao encontrar elemento.
|
||||
*/
|
||||
private No remover(int x, No i) throws Exception {
|
||||
|
||||
if (i == null) {
|
||||
throw new Exception("Erro ao remover!");
|
||||
|
||||
} else if (x < i.elemento) {
|
||||
i.esq = remover(x, i.esq);
|
||||
|
||||
} else if (x > i.elemento) {
|
||||
i.dir = remover(x, i.dir);
|
||||
|
||||
// Sem no a direita.
|
||||
} else if (i.dir == null) {
|
||||
i = i.esq;
|
||||
|
||||
// Sem no a esquerda.
|
||||
} else if (i.esq == null) {
|
||||
i = i.dir;
|
||||
|
||||
// No a esquerda e no a direita.
|
||||
} else {
|
||||
i.esq = maiorEsq(i, i.esq);
|
||||
throw new Exception("Erro ao remover!");
|
||||
} else if (x < i.elemento) {
|
||||
i.esq = remover(x, i.esq);
|
||||
} else if (x > i.elemento) {
|
||||
i.dir = remover(x, i.dir);
|
||||
// Sem no a direita.
|
||||
} else if (i.dir == null) {
|
||||
i = i.esq;
|
||||
// Sem no a esquerda.
|
||||
} else if (i.esq == null) {
|
||||
i = i.dir;
|
||||
// No a esquerda e no a direita.
|
||||
} else {
|
||||
i.esq = maiorEsq(i, i.esq);
|
||||
}
|
||||
|
||||
return balancear(i);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Metodo para trocar o elemento "removido" pelo maior da esquerda.
|
||||
* @param i No que teve o elemento removido.
|
||||
|
|
@ -195,83 +180,72 @@ public class AVL {
|
|||
* @return No em analise, alterado ou nao.
|
||||
*/
|
||||
private No maiorEsq(No i, No j) {
|
||||
|
||||
// Encontrou o maximo da subarvore esquerda.
|
||||
// Encontrou o maximo da subarvore esquerda.
|
||||
if (j.dir == null) {
|
||||
i.elemento = j.elemento; // Substitui i por j.
|
||||
j = j.esq; // Substitui j por j.ESQ.
|
||||
|
||||
// Existe no a direita.
|
||||
// Existe no a direita.
|
||||
} else {
|
||||
// Caminha para direita.
|
||||
// Caminha para direita.
|
||||
j.dir = maiorEsq(i, j.dir);
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
private No balancear(No no) throws Exception {
|
||||
if (no != null) {
|
||||
int fator = No.getNivel(no.dir) - No.getNivel(no.esq);
|
||||
// Se balanceada
|
||||
if (Math.abs(fator) <= 1) {
|
||||
no.setNivel();
|
||||
// Se desbalanceada para a direita
|
||||
} else if (fator == 2) {
|
||||
int fatorFilhoDir = No.getNivel(no.dir.dir) - No.getNivel(no.dir.esq);
|
||||
// Se o filho a direita tambem estiver desbalanceado
|
||||
if (fatorFilhoDir == -1) {
|
||||
no.dir = rotacionarDir(no.dir);
|
||||
}
|
||||
no = rotacionarEsq(no);
|
||||
// Se desbalanceada para a esquerda
|
||||
} else if (fator == -2) {
|
||||
int fatorFilhoEsq = No.getNivel(no.esq.dir) - No.getNivel(no.esq.esq);
|
||||
// Se o filho a esquerda tambem estiver desbalanceado
|
||||
if (fatorFilhoEsq == 1) {
|
||||
no.esq = rotacionarEsq(no.esq);
|
||||
}
|
||||
no = rotacionarDir(no);
|
||||
} else {
|
||||
throw new Exception(
|
||||
"Erro no No(" + no.elemento + ") com fator de balanceamento (" + fator + ") invalido!");
|
||||
}
|
||||
}
|
||||
return no;
|
||||
}
|
||||
|
||||
private No balancear(No no) throws Exception {
|
||||
if(no != null){
|
||||
int fator = No.getNivel(no.dir) - no.getNivel(no.esq);
|
||||
private No rotacionarDir(No no) {
|
||||
//System.out.println("Rotacionar DIR(" + no.elemento + ")");
|
||||
No noEsq = no.esq;
|
||||
No noEsqDir = noEsq.dir;
|
||||
|
||||
//Se balanceada
|
||||
if (Math.abs(fator) <= 1){
|
||||
no.setNivel();
|
||||
noEsq.dir = no;
|
||||
no.esq = noEsqDir;
|
||||
|
||||
//Se desbalanceada para a direita
|
||||
}else if (fator == 2){
|
||||
no.setNivel(); // Atualizar o nivel do no
|
||||
noEsq.setNivel(); // Atualizar o nivel do noEsq
|
||||
|
||||
int fatorFilhoDir = No.getNivel(no.dir.dir) - No.getNivel(no.dir.esq);
|
||||
return noEsq;
|
||||
}
|
||||
|
||||
//Se o filho a direita tambem estiver desbalanceado
|
||||
if (fatorFilhoDir == -1) {
|
||||
no.dir = rotacionarDir(no.dir);
|
||||
}
|
||||
no = rotacionarEsq(no);
|
||||
private No rotacionarEsq(No no) {
|
||||
//System.out.println("Rotacionar ESQ(" + no.elemento + ")");
|
||||
No noDir = no.dir;
|
||||
No noDirEsq = noDir.esq;
|
||||
|
||||
//Se desbalanceada para a esquerda
|
||||
}else if (fator == -2){
|
||||
noDir.esq = no;
|
||||
no.dir = noDirEsq;
|
||||
|
||||
int fatorFilhoEsq = No.getNivel(no.esq.dir) - No.getNivel(no.esq.esq);
|
||||
|
||||
//Se o filho a esquerda tambem estiver desbalanceado
|
||||
if (fatorFilhoEsq == 1) {
|
||||
no.esq = rotacionarEsq(no.esq);
|
||||
}
|
||||
no = rotacionarDir(no);
|
||||
|
||||
}else{
|
||||
throw new Exception("Erro no No(" + no.elemento + ") com fator de balanceamento (" + fator + ") invalido!");
|
||||
}
|
||||
}
|
||||
|
||||
return no;
|
||||
}
|
||||
|
||||
private No rotacionarDir(No no) {
|
||||
System.out.println("Rotacionar DIR(" + no.elemento + ")");
|
||||
No noEsq = no.esq;
|
||||
No noEsqDir = noEsq.dir;
|
||||
|
||||
noEsq.dir = no;
|
||||
no.esq = noEsqDir;
|
||||
|
||||
no.setNivel(); //Atualizar o nivel do no
|
||||
noEsq.setNivel(); //Atualizar o nivel do noEsq
|
||||
|
||||
return noEsq;
|
||||
}
|
||||
|
||||
private No rotacionarEsq(No no) {
|
||||
System.out.println("Rotacionar ESQ(" + no.elemento + ")");
|
||||
No noDir = no.dir;
|
||||
No noDirEsq = noDir.esq;
|
||||
|
||||
noDir.esq = no;
|
||||
no.dir = noDirEsq;
|
||||
|
||||
no.setNivel(); //Atualizar o nivel do no
|
||||
noDir.setNivel(); //Atualizar o nivel do noDir
|
||||
return noDir;
|
||||
}
|
||||
no.setNivel(); // Atualizar o nivel do no
|
||||
noDir.setNivel(); // Atualizar o nivel do noDir
|
||||
return noDir;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
*/
|
||||
class No {
|
||||
public int elemento; // Conteudo do no.
|
||||
public No esq, dir; // Filhos da esq e dir.
|
||||
public int nivel; //Numero de niveis abaixo do no
|
||||
public No esq, dir; // Filhos da esq e dir.
|
||||
public int nivel; // Numero de niveis abaixo do no
|
||||
|
||||
/**
|
||||
* Construtor da classe.
|
||||
* Construtor da classe
|
||||
* @param elemento Conteudo do no.
|
||||
*/
|
||||
public No(int elemento) {
|
||||
|
|
@ -18,28 +18,28 @@ class No {
|
|||
/**
|
||||
* Construtor da classe.
|
||||
* @param elemento Conteudo do no.
|
||||
* @param esq No da esquerda.
|
||||
* @param dir No da direita.
|
||||
* @param esq No da esquerda.
|
||||
* @param dir No da direita.
|
||||
*/
|
||||
public No(int elemento, No esq, No dir, int nivel) {
|
||||
this.elemento = elemento;
|
||||
this.esq = esq;
|
||||
this.dir = dir;
|
||||
this.nivel = nivel;
|
||||
this.nivel = nivel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cálculo do número de níveis a partir de um vértice
|
||||
*/
|
||||
public void setNivel() {
|
||||
this.nivel = 1 + Math.max(getNivel(esq),getNivel(dir));
|
||||
}
|
||||
public void setNivel() {
|
||||
this.nivel = 1 + Math.max(getNivel(esq), getNivel(dir));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retorna o número de níveis a partir de um vértice
|
||||
* Retorna o número de níveis a partir de um vértice
|
||||
* @param no nó que se deseja o nível.
|
||||
*/
|
||||
public static int getNivel(No no) {
|
||||
return (no == null) ? 0 : no.nivel;
|
||||
}
|
||||
public static int getNivel(No no) {
|
||||
return (no == null) ? 0 : no.nivel;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,70 +6,12 @@ public class Principal {
|
|||
public static void main(String[] args) {
|
||||
try {
|
||||
AVL avl = new AVL();
|
||||
avl.inserir(9);
|
||||
int array[] = {1,2,3,4,5,6,7,8,9,10};
|
||||
for(int item: array){
|
||||
avl.inserir(item);
|
||||
}
|
||||
avl.caminharPre();
|
||||
avl.inserir(8);
|
||||
avl.caminharPre();
|
||||
avl.inserir(4);
|
||||
avl.caminharPre();
|
||||
avl.inserir(6);
|
||||
avl.caminharPre();
|
||||
avl.inserir(5);
|
||||
avl.caminharPre();
|
||||
avl.inserir(3);
|
||||
avl.caminharPre();
|
||||
avl.inserir(7);
|
||||
avl.caminharPre();
|
||||
avl.inserir(2);
|
||||
avl.caminharPre();
|
||||
avl.inserir(1);
|
||||
avl.caminharPre();
|
||||
/*
|
||||
avl.inserir(8);
|
||||
avl.caminharPre();
|
||||
avl.inserir(10);
|
||||
avl.caminharPre();
|
||||
avl.inserir(5);
|
||||
avl.caminharPre();
|
||||
avl.inserir(6);
|
||||
avl.caminharPre();
|
||||
avl.inserir(2);
|
||||
avl.caminharPre();
|
||||
avl.inserir(9);
|
||||
avl.caminharPre();
|
||||
avl.inserir(11);
|
||||
avl.caminharPre();
|
||||
avl.inserir(1);
|
||||
avl.caminharPre();
|
||||
avl.inserir(4);
|
||||
avl.caminharPre();
|
||||
avl.inserir(7);
|
||||
avl.caminharPre();
|
||||
avl.inserir(12);
|
||||
avl.caminharPre();
|
||||
avl.inserir(3);
|
||||
avl.caminharPre();
|
||||
|
||||
for(int i = 40; i >= 21; i--){
|
||||
avl.inserir(i);
|
||||
avl.caminharPre();
|
||||
System.out.println("Inserindo o " + i + " (altura = " + avl.getAltura() +")");
|
||||
}
|
||||
|
||||
avl.caminharCentral();
|
||||
avl.caminharPre();
|
||||
avl.caminharPos();
|
||||
|
||||
avl.remover(6);
|
||||
avl.remover(2);
|
||||
avl.remover(4);
|
||||
|
||||
avl.caminharCentral();
|
||||
avl.caminharPre();
|
||||
avl.caminharPos();
|
||||
*/
|
||||
}
|
||||
catch(Exception erro) {
|
||||
} catch (Exception erro) {
|
||||
System.out.println(erro.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
import java.io.*;
|
||||
import java.io.FileReader;
|
||||
|
||||
class Serie{
|
||||
class Serie {
|
||||
//declaração dos atributos
|
||||
private String name;
|
||||
private String format;
|
||||
|
|
@ -189,7 +189,7 @@ class Serie{
|
|||
BufferedReader br = new BufferedReader(fileReader); //declaração do bufferedReader para leitura do arquivo
|
||||
|
||||
//set nome da série
|
||||
this.name = searchName(fileName);
|
||||
this.name = searchName(fileName).trim();
|
||||
|
||||
//set Formato da série
|
||||
while(!br.readLine().contains("Formato"));
|
||||
|
|
@ -223,8 +223,6 @@ class Serie{
|
|||
while(!br.readLine().contains("N.º de episódios"));
|
||||
this.episodes = justInt(removeTags(br.readLine()));
|
||||
|
||||
//método para mostrar a classe
|
||||
this.printClass();
|
||||
//fechamento do bufferedReader
|
||||
br.close();
|
||||
//Tratamento de exceções
|
||||
|
|
@ -234,4 +232,5 @@ class Serie{
|
|||
System.out.println("Error reading file '" + fileName + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue