91 lines
2.5 KiB
Java
91 lines
2.5 KiB
Java
class ArvoreTrie {
|
|
private No raiz;
|
|
|
|
public ArvoreTrie(){
|
|
raiz = new No();
|
|
}
|
|
|
|
|
|
public boolean pesquisar(String s) throws Exception {
|
|
return pesquisar(s, raiz, 0);
|
|
}
|
|
|
|
public boolean pesquisar(String s, No no, int i) throws Exception {
|
|
boolean resp;
|
|
if(no.prox[s.charAt(i)] == null){
|
|
resp = false;
|
|
} else if(i == s.length() - 1){
|
|
resp = (no.prox[s.charAt(i)].folha == true);
|
|
} else if(i < s.length() - 1 ){
|
|
resp = pesquisar(s, no.prox[s.charAt(i)], i + 1);
|
|
} else {
|
|
throw new Exception("Erro ao pesquisar!");
|
|
}
|
|
return resp;
|
|
}
|
|
|
|
public void inserir(String s) throws Exception {
|
|
inserir(s, raiz, 0);
|
|
}
|
|
|
|
private void inserir(String s, No no, int i) throws Exception {
|
|
System.out.print("\nEM NO(" + no.elemento + ") (" + i + ")");
|
|
if(no.prox[s.charAt(i)] == null){
|
|
System.out.print("--> criando filho(" + s.charAt(i) + ")");
|
|
no.prox[s.charAt(i)] = new No(s.charAt(i));
|
|
|
|
if(i == s.length() - 1){
|
|
System.out.print("(folha)");
|
|
no.prox[s.charAt(i)].folha = true;
|
|
}else{
|
|
inserir(s, no.prox[s.charAt(i)], i + 1);
|
|
}
|
|
|
|
} else if (no.prox[s.charAt(i)].folha == false && i < s.length() - 1){
|
|
inserir(s, no.prox[s.charAt(i)], i + 1);
|
|
|
|
} else {
|
|
throw new Exception("Erro ao inserir!");
|
|
}
|
|
}
|
|
|
|
public void mostrar(){
|
|
mostrar("", raiz);
|
|
}
|
|
|
|
public void mostrar(String s, No no) {
|
|
if(no.folha == true){
|
|
System.out.println("Palavra: " + (s + no.elemento));
|
|
} else {
|
|
for(int i = 0; i < no.prox.length; i++){
|
|
if(no.prox[i] != null){
|
|
System.out.println("ESTOU EM (" + no.elemento + ") E VOU PARA (" + no.prox[i].elemento + ")");
|
|
mostrar(s + no.elemento, no.prox[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public int contarAs(){
|
|
int resp = 0;
|
|
if(raiz != null){
|
|
resp = contarAs(raiz);
|
|
}
|
|
return resp;
|
|
}
|
|
|
|
public int contarAs(No no) {
|
|
int resp = (no.elemento == 'A') ? 1 : 0;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|