2021/1 - maxm

This commit is contained in:
Max do Val Machado 2021-02-02 08:15:31 -03:00
parent 82301f8218
commit 0f5a77e84b
14 changed files with 322 additions and 243 deletions

View File

@ -14,10 +14,10 @@ class Patricia {
}
}
public String string(No no){
private String string(No no){
return (no == raiz) ? " " : string(no.i, no.j, no.k);
}
public String string(int i, int j, int k){
private String string(int i, int j, int k){
//System.out.println("i("+i+") j("+j+") k("+k+") array(i)("+array[i]+") ijk(" + array[i].substring(j,k+1) + ")");
return array[i].substring(j,k+1);
}
@ -43,7 +43,7 @@ class Patricia {
int k;
for(k = 1; k < prox.length() && k < inserindo.length() && prox.charAt(k) == inserindo.charAt(k); k++);
System.out.println("k (" + k + ")");
if(k == prox.length()){
if(no.prox[array[i].charAt(j)].folha == true){
throw new Exception("Erro: exite um prefixo de [" + array[i] + "] na arvore");
@ -51,7 +51,7 @@ class Patricia {
inserir(no.prox[array[i].charAt(j)], i, j + k);
}
} else if (k == inserindo.length()){
throw new Exception("Erro: [" + array[i] + "] é prefixo de outra palavra da árvore");
throw new Exception("Erro: [" + array[i] + "] é prefixo de outra palavra da árvore");
} else {
No novo = new No(i, j, j + k - 1, false);
novo.prox[prox.charAt(k)] = no.prox[array[i].charAt(j)];
@ -111,4 +111,31 @@ class Patricia {
}
}
}
public int contarAs(){
int resp = 0;
if(raiz != null){
resp = contarAs(raiz);
}
return resp;
}
public int contarAs(No no) {
int resp = 0;
String palavra = string(no);
for(int i = 0; i < palavra.length(); i++){
if(palavra.charat(i) == 'A'){
resp++;
}
}
if(no.folha == false){
for(int i = 0; i < no.prox.length; i++){
if(no.prox[i] != null){
resp += contarAs(no.prox[i]);
}
}
}
return resp;
}
}

View File

@ -1,84 +0,0 @@
EM NO( ) (0)--> criando filho(A)
EM NO(A) (1)--> criando filho(B)
EM NO(B) (2)--> criando filho(A)
EM NO(A) (3)--> criando filho(C)
EM NO(C) (4)--> criando filho(A)
EM NO(A) (5)--> criando filho(X)
EM NO(X) (6)--> criando filho(I)(folha)
EM NO( ) (0)--> criando filho(B)
EM NO(B) (1)--> criando filho(A)
EM NO(A) (2)--> criando filho(L)
EM NO(L) (3)--> criando filho(A)(folha)
EM NO( ) (0)
EM NO(B) (1)--> criando filho(O)
EM NO(O) (2)--> criando filho(L)
EM NO(L) (3)--> criando filho(O)(folha)
EM NO( ) (0)
EM NO(A) (1)
EM NO(B) (2)
EM NO(A) (3)
EM NO(C) (4)
EM NO(A) (5)--> criando filho(T)
EM NO(T) (6)--> criando filho(E)(folha)
EM NO( ) (0)--> criando filho(g)
EM NO(g) (1)--> criando filho(a)
EM NO(a) (2)--> criando filho(l)
EM NO(l) (3)--> criando filho(o)(folha)
EM NO( ) (0)--> criando filho(p)
EM NO(p) (1)--> criando filho(a)
EM NO(a) (2)--> criando filho(t)
EM NO(t) (3)--> criando filho(a)(folha)
EM NO( ) (0)
EM NO(p) (1)
EM NO(a) (2)
EM NO(t) (3)--> criando filho(o)(folha)
EM NO( ) (0)
EM NO(g) (1)
EM NO(a) (2)--> criando filho(t)
EM NO(t) (3)--> criando filho(o)(folha)ESTOU EM ( ) E VOU PARA (A)
ESTOU EM (A) E VOU PARA (B)
ESTOU EM (B) E VOU PARA (A)
ESTOU EM (A) E VOU PARA (C)
ESTOU EM (C) E VOU PARA (A)
ESTOU EM (A) E VOU PARA (X)
ESTOU EM (X) E VOU PARA (I)
Palavra: ABACAXI
ESTOU EM (A) E VOU PARA (T)
ESTOU EM (T) E VOU PARA (E)
Palavra: ABACATE
ESTOU EM ( ) E VOU PARA (B)
ESTOU EM (B) E VOU PARA (A)
ESTOU EM (A) E VOU PARA (L)
ESTOU EM (L) E VOU PARA (A)
Palavra: BALA
ESTOU EM (B) E VOU PARA (O)
ESTOU EM (O) E VOU PARA (L)
ESTOU EM (L) E VOU PARA (O)
Palavra: BOLO
ESTOU EM ( ) E VOU PARA (g)
ESTOU EM (g) E VOU PARA (a)
ESTOU EM (a) E VOU PARA (l)
ESTOU EM (l) E VOU PARA (o)
Palavra: galo
ESTOU EM (a) E VOU PARA (t)
ESTOU EM (t) E VOU PARA (o)
Palavra: gato
ESTOU EM ( ) E VOU PARA (p)
ESTOU EM (p) E VOU PARA (a)
ESTOU EM (a) E VOU PARA (t)
ESTOU EM (t) E VOU PARA (a)
Palavra: pata
ESTOU EM (t) E VOU PARA (o)
Palavra: pato
Pesquisar(ABACAXI):true
Pesquisar(BALA):true
Pesquisar(BOLO):true
Pesquisar(ABACATE):true
Pesquisar(galo):true
Pesquisar(pata):true
Pesquisar(pato):true
Pesquisar(gato):true
Pesquisar(ABACA):false
Pesquisar(ABACAXIS):false
Pesquisar(gaga):false

View File

@ -16,6 +16,71 @@ public class ArvoreArvore {
//os outros 23 caracteres.
}
public void inserir(String s){
inserir(s, raiz);
}
public void inserir(String s, No i) throws Exception {
if (i == null) {
throw new Exception("Erro ao inserir: caractere invalido!");
} else if (s.charAt(0) < i.elemento) {
inserir(x, i.esq);
} else if (s.charAt(0) > i.elemento) {
inserir(x, i.dir);
} else {
i.outro = inserir(s, i.outro);
}
}
private No inserir(String s, No2 i) throws Exception {
if (i == null) {
i = new No2(x);
} else if (s.compareTo(i.elemento) < 0) {
i.esq = inserir(x, i.esq);
} else if (s.compareTo(i.elemento) > 0) {
i.dir = inserir(x, i.dir);
} else {
throw new Exception("Erro ao inserir: elemento existente!");
}
return i;
}
public boolean hasStringTam10(){
return hasStringTam10(raiz);
}
public boolean hasStringTam10(No i){
boolean resp = false;
if(i != null){
resp = hasStringTam10(i.outro) || hasStringTam10(i.esq) || hasStringTam10(i.dir);
}
return resp;
}
public boolean hasStringTam10(No2 i){
boolean resp = false;
if(i != null){
resp = i.elemento.length() == 10 || hasStringTam10(i.esq) || hasStringTam10(i.dir);
}
return resp;
}
/**
* Metodo publico iterativo para pesquisar elemento.
* @param elemento Elemento que sera procurado.
@ -64,4 +129,37 @@ public class ArvoreArvore {
System.out.println(c);
//implementar
}
public int contPalavra(char letra){
return contPalavra(letra, raiz);
}
public int contPalavra(char letra, No i) throws Exception {
int resp = 0;
if (i == null) {
throw new Exception("Erro ao pesquisar: caractere invalido!");
} else if (letra < i.elemento) {
resp = contPalavra(letra, i.esq);
} else if (letra > i.elemento) {
resp = contPalavra(letra, i.dir);
} else {
resp = contPalavra(i.outro);
}
return resp;
}
public int contPalavra(No2 i){
int resp = 0;
if(i != null){
resp = 1 + contPalavra(i.esq) + contPalavra(i.dir);
}
return resp;
}
}

View File

@ -1,94 +1,98 @@
public class Agenda {
private No raiz;
private No raiz;
public Agenda() {
raiz = new No ('M');
raiz.esq = new No ('F');
raiz.dir = new No ('T');
raiz.esq.esq = new No ('C');
//inserir todas as 26 letras do alfabeto...
}
public Agenda() {
raiz = new No ('M');
raiz.esq = new No ('F');
raiz.dir = new No ('T');
raiz.esq.esq = new No ('C');
//inserir todas as 26 letras do alfabeto...
}
public boolean pesquisarNome(String nome) {
return pesquisarNome(raiz, nome);
}
private boolean pesquisarNome(No no, String nome) {
boolean resp;
if (no == null) {
resp = false;
} else if (Char.toUpper(nome.charAt(0)) == no.letra) {
resp = false;
for(Celula i = no.primeiro.prox; (!resp && i != null); i = i.prox){
if(i.contato.nome.equals(nome) == true){
resp = true;
}
public boolean pesquisarNome(String nome) {
return pesquisarNome(raiz, nome);
}
private boolean pesquisarNome(No no, String nome) {
boolean resp;
if (no == null) {
resp = false;
} else if (Char.toUpper(nome.charAt(0)) == no.letra) {
resp = false;
for(Celula i = no.primeiro.prox; (!resp && i != null); i = i.prox){
if(i.contato.nome.equals(nome) == true){
resp = true;
}
} else if (Char.toUpper(nome.charAt(0)) < no.letra) {
resp = pesquisarNome(no.esq, nome);
}
} else if (Char.toUpper(nome.charAt(0)) < no.letra) {
resp = pesquisarNome(no.esq, nome);
} else {
resp = pesquisarNome(no.dir, nome);
}
return resp;
}
} else {
resp = pesquisarNome(no.dir, nome);
}
return resp;
}
public void inserir(Contato contato) throws Exception {
if(Character.isLetter(contato.nome.charAt(0))){
raiz = inserir(raiz, contato);
} else {
throw new Exception("Erro ao inserir!");
}
}
public void inserir(Contato contato) throws Exception {
if(Character.isLetter(contato.nome.charAt(0))){
raiz = inserir(raiz, contato);
} else {
throw new Exception("Erro ao inserir!");
}
}
private No inserir(No no, Contato contato) throws Exception {
// insere o com a letra
if (no == null) {
no = new no(Character.toUpperCase(contato.nome.charAt(0)));
no.primeiro = no.ultimo = new Celula();
no.ultimo.prox = new Celula(contato);
no.ultimo = no.ultimo.prox;
private No inserir(No no, Contato contato) throws Exception {
// insere o com a letra
if (no == null) {
no = new no(Character.toUpperCase(contato.nome.charAt(0)));
no.primeiro = no.ultimo = new Celula();
no.ultimo.prox = new Celula(contato);
no.ultimo = no.ultimo.prox;
// insere o contatinho
} else if (Character.toUpperCase(contato.nome.charAt(0)) == no.letra) {
no.ultimo.prox = new Celula(contato);
no.ultimo = no.ultimo.prox;
// letra menor, caminha para a esquerda
} else if (Character.toUpperCase(contato.nome.charAt(0)) < no.letra) {
no.esq = inserir(no.esq, contato);
// insere o contatinho
} else if (Character.toUpperCase(contato.nome.charAt(0)) == no.letra) {
no.ultimo.prox = new Celula(contato);
no.ultimo = no.ultimo.prox;
// letra maior, caminha para a direita
} else {
no.dir = inserir(no.dir, contato);
}
return no;
}
public boolean pesquisar(int cpf) {
return pesquisar(raiz, cpf);
}
// letra menor, caminha para a esquerda
} else if (Character.toUpperCase(contato.nome.charAt(0)) < no.letra) {
no.esq = inserir(no.esq, contato);
private boolean pesquisar(No i, int cpf) {
boolean resp = false;
if (i != null) {
resp = pesquisar(i.primeiro.prox, cpf);
if(resp == false){
resp = pesquisar(i.esq, cpf);
if(resp == false){
resp = pesquisar(i.dir, cpf);
}
}
}
return resp;
}
// letra maior, caminha para a direita
} else {
no.dir = inserir(no.dir, contato);
}
return no;
}
public boolean pesquisar(int cpf) {
return pesquisar(raiz, cpf);
}
private boolean pesquisar(No i, int cpf) {
boolean resp = false;
if (i != null) {
resp = pesquisar(i.primeiro.prox, cpf);
if(resp == false){
resp = pesquisar(i.esq, cpf);
if(resp == false){
resp = pesquisar(i.dir, cpf);
}
}
}
return resp;
}
private boolean pesquisar(Celula i, int cpf){
//efeuar a pesquisa na lista a partir do i
}
private boolean pesquisar(Celula i, int cpf){
//efeuar a pesquisa na lista a partir do i
}
}

View File

@ -4,5 +4,3 @@ class Contato {
public String email;
public int cpf;
}

View File

@ -347,4 +347,37 @@ public class ArvoreBinaria {
}
return resp;
}
void exerResol2(){
inserir(2);
//..
inserir(7);
caminharPre();
raiz = rotacionarEsq(raiz);
caminharPre();
}
void exer3(){
int a, b, c;
a = lerInt();
b = lerInt();
c = lerInt();
inserir(a);
inserir(b);
inserir(c);
}
}

View File

@ -49,9 +49,9 @@ public class Alvinegra {
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarCentral() {
public void caminharCentral() {
System.out.print("[ ");
mostrarCentral(raiz);
caminharCentral(raiz);
System.out.println("]");
}
@ -59,20 +59,20 @@ public class Alvinegra {
* Metodo privado recursivo para exibir elementos.
* @param i NoAN em analise.
*/
private void mostrarCentral(NoAN i) {
private void caminharCentral(NoAN i) {
if (i != null) {
mostrarCentral(i.esq); // Elementos da esquerda.
caminharCentral(i.esq); // Elementos da esquerda.
System.out.print(i.elemento + ((i.cor) ? "(p) " : "(b) ")); // Conteudo do no.
mostrarCentral(i.dir); // Elementos da direita.
caminharCentral(i.dir); // Elementos da direita.
}
}
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarPre() {
public void caminharPre() {
System.out.print("[ ");
mostrarPre(raiz);
caminharPre(raiz);
System.out.println("]");
}
@ -80,20 +80,20 @@ public class Alvinegra {
* Metodo privado recursivo para exibir elementos.
* @param i NoAN em analise.
*/
private void mostrarPre(NoAN i) {
private void caminharPre(NoAN i) {
if (i != null) {
System.out.print(i.elemento + ((i.cor) ? "(p) " : "(b) ")); // Conteudo do no.
mostrarPre(i.esq); // Elementos da esquerda.
mostrarPre(i.dir); // Elementos da direita.
caminharPre(i.esq); // Elementos da esquerda.
caminharPre(i.dir); // Elementos da direita.
}
}
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarPos() {
public void caminharPos() {
System.out.print("[ ");
mostrarPos(raiz);
caminharPos(raiz);
System.out.println("]");
}
@ -101,10 +101,10 @@ public class Alvinegra {
* Metodo privado recursivo para exibir elementos.
* @param i NoAN em analise.
*/
private void mostrarPos(NoAN i) {
private void caminharPos(NoAN i) {
if (i != null) {
mostrarPos(i.esq); // Elementos da esquerda.
mostrarPos(i.dir); // Elementos da direita.
caminharPos(i.esq); // Elementos da esquerda.
caminharPos(i.dir); // Elementos da direita.
System.out.print(i.elemento + ((i.cor) ? "(p) " : "(b) ")); // Conteudo do no.
}
}
@ -204,12 +204,10 @@ public class Alvinegra {
if (bisavo == null){
raiz = avo;
} else if(avo.elemento < bisavo.elemento){
bisavo.esq = avo;
} else {
if(avo.elemento < bisavo.elemento){
bisavo.esq = avo;
} else {
bisavo.dir = avo;
}
bisavo.dir = avo;
}
//reestabelecer as cores apos a rotacao
@ -252,6 +250,7 @@ public class Alvinegra {
balancear(bisavo, avo, pai, i);
}
}
if (elemento < i.elemento) {
inserir(elemento, avo, pai, i, i.esq);
} else if (elemento > i.elemento) {

View File

@ -49,9 +49,9 @@ public class AVL {
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarCentral() {
public void caminharCentral() {
System.out.print("[ ");
mostrarCentral(raiz);
caminharCentral(raiz);
System.out.println("]");
}
@ -59,20 +59,20 @@ public class AVL {
* Metodo privado recursivo para exibir elementos.
* @param i No em analise.
*/
private void mostrarCentral(No i) {
private void caminharCentral(No i) {
if (i != null) {
mostrarCentral(i.esq); // Elementos da esquerda.
caminharCentral(i.esq); // Elementos da esquerda.
System.out.print(i.elemento + " "); // Conteudo do no.
mostrarCentral(i.dir); // Elementos da direita.
caminharCentral(i.dir); // Elementos da direita.
}
}
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarPre() {
public void caminharPre() {
System.out.print("[ ");
mostrarPre(raiz);
caminharPre(raiz);
System.out.println("]");
}
@ -80,20 +80,20 @@ public class AVL {
* Metodo privado recursivo para exibir elementos.
* @param i No em analise.
*/
private void mostrarPre(No i) {
private void caminharPre(No i) {
if (i != null) {
System.out.print(i.elemento + "(fator " + (No.getNivel(i.dir) - No.getNivel(i.esq)) + ") "); // Conteudo do no.
mostrarPre(i.esq); // Elementos da esquerda.
mostrarPre(i.dir); // Elementos da direita.
caminharPre(i.esq); // Elementos da esquerda.
caminharPre(i.dir); // Elementos da direita.
}
}
/**
* Metodo publico iterativo para exibir elementos.
*/
public void mostrarPos() {
public void caminharPos() {
System.out.print("[ ");
mostrarPos(raiz);
caminharPos(raiz);
System.out.println("]");
}
@ -101,10 +101,10 @@ public class AVL {
* Metodo privado recursivo para exibir elementos.
* @param i No em analise.
*/
private void mostrarPos(No i) {
private void caminharPos(No i) {
if (i != null) {
mostrarPos(i.esq); // Elementos da esquerda.
mostrarPos(i.dir); // Elementos da direita.
caminharPos(i.esq); // Elementos da esquerda.
caminharPos(i.dir); // Elementos da direita.
System.out.print(i.elemento + " "); // Conteudo do no.
}
}

View File

@ -7,66 +7,66 @@ public class Principal {
try {
AVL avl = new AVL();
avl.inserir(9);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(8);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(4);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(6);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(5);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(3);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(7);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(2);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(1);
avl.mostrarPre();
avl.caminharPre();
/*
avl.inserir(8);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(10);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(5);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(6);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(2);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(9);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(11);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(1);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(4);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(7);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(12);
avl.mostrarPre();
avl.caminharPre();
avl.inserir(3);
avl.mostrarPre();
avl.caminharPre();
for(int i = 40; i >= 21; i--){
avl.inserir(i);
avl.mostrarPre();
avl.caminharPre();
System.out.println("Inserindo o " + i + " (altura = " + avl.getAltura() +")");
}
avl.mostrarCentral();
avl.mostrarPre();
avl.mostrarPos();
avl.caminharCentral();
avl.caminharPre();
avl.caminharPos();
avl.remover(6);
avl.remover(2);
avl.remover(4);
avl.mostrarCentral();
avl.mostrarPre();
avl.mostrarPos();
avl.caminharCentral();
avl.caminharPre();
avl.caminharPos();
*/
}
catch(Exception erro) {

View File

@ -42,7 +42,7 @@ class DoidonaComTADsProntas {
public void inserir(int elemento){
int i = hashT1(elemento);
if(elemento == NULO) {
//????
//gerar msg de erro para o usuario...
} else if(t1[i] == NULO){
t1[i] = elemento;
}else if(hashT2(elemento) == 0){

View File

@ -150,14 +150,16 @@ class MyIO {
public static String readString(){
String s = "";
char tmp;
try{
char tmp = (char) in.read();
while (tmp != '\n' && tmp != ' ' && tmp != '\t' && tmp != (char) -1) {
if (tmp != '\r') s += tmp;
tmp = (char) in.read();
}
do{
tmp = (char)in.read();
if(tmp != '\n' && tmp != ' ' && tmp != 13){
s += tmp;
}
}while(tmp != '\n' && tmp != ' ');
}catch(IOException ioe){
System.out.println("MyIO.readString: " + ioe.getMessage());
System.out.println("lerPalavra: " + ioe.getMessage());
}
return s;
}
@ -172,14 +174,16 @@ class MyIO {
public static String readLine(){
String s = "";
char tmp;
try{
char tmp = (char) in.read();
while (tmp != '\n' && tmp != (char) -1) {
if (tmp != '\r') s += tmp;
tmp = (char) in.read();
}
do{
tmp = (char)in.read();
if(tmp != '\n' && tmp != 13){
s += tmp;
}
}while(tmp != '\n');
}catch(IOException ioe){
System.out.println("MyIO.readLine: " + ioe.getMessage());
System.out.println("lerPalavra: " + ioe.getMessage());
}
return s;
}