This commit is contained in:
Felipe Domingos 2021-11-15 15:59:52 -03:00
commit f041b8353d
1 changed files with 359 additions and 351 deletions

View File

@ -33,10 +33,13 @@ public class ArvoreBinaria {
boolean resp; boolean resp;
if (i == null) { if (i == null) {
resp = false; resp = false;
} else if (x == i.elemento) { } else if (x == i.elemento) {
resp = true; resp = true;
} else if (x < i.elemento) { } else if (x < i.elemento) {
resp = pesquisar(x, i.esq); resp = pesquisar(x, i.esq);
} else { } else {
resp = pesquisar(x, i.dir); resp = pesquisar(x, i.dir);
} }
@ -126,13 +129,17 @@ public class ArvoreBinaria {
private No inserir(int x, No i) throws Exception { private No inserir(int x, No i) throws Exception {
if (i == null) { if (i == null) {
i = new No(x); i = new No(x);
} else if (x < i.elemento) { } else if (x < i.elemento) {
i.esq = inserir(x, i.esq); i.esq = inserir(x, i.esq);
} else if (x > i.elemento) { } else if (x > i.elemento) {
i.dir = inserir(x, i.dir); i.dir = inserir(x, i.dir);
} else { } else {
throw new Exception("Erro ao inserir!"); throw new Exception("Erro ao inserir!");
} }
return i; return i;
} }
@ -247,11 +254,13 @@ public class ArvoreBinaria {
*/ */
public int getMaior(){ public int getMaior(){
int resp = -1; int resp = -1;
if(raiz != null){ if(raiz != null){
No i; No i;
for(i = raiz; i.dir != null; i = i.dir); for(i = raiz; i.dir != null; i = i.dir);
resp = i.elemento; resp = i.elemento;
} }
return resp; return resp;
} }
@ -262,11 +271,13 @@ public class ArvoreBinaria {
*/ */
public int getMenor(){ public int getMenor(){
int resp = -1; int resp = -1;
if(raiz != null){ if(raiz != null){
No i; No i;
for(i = raiz; i.esq != null; i = i.esq); for(i = raiz; i.esq != null; i = i.esq);
resp = i.elemento; resp = i.elemento;
} }
return resp; return resp;
} }
@ -279,6 +290,7 @@ public class ArvoreBinaria {
return getAltura(raiz, 0); return getAltura(raiz, 0);
} }
/** /**
* Metodo que retorna a altura da árvore * Metodo que retorna a altura da árvore
* @return int altura da árvore * @return int altura da árvore
@ -366,9 +378,7 @@ public class ArvoreBinaria {
public int soma(No i){ public int soma(No i){
int resp = 0; int resp = 0;
if(i != null){ if(i != null){
resp = i.elemento; resp = i.elemento + soma(i.esq) + soma(i.dir);
resp += soma(i.esq);
resp += soma(i.dir);
} }
return resp; return resp;
} }
@ -380,9 +390,7 @@ public class ArvoreBinaria {
public int quantidadePares(No i){ public int quantidadePares(No i){
int resp = 0; int resp = 0;
if(i != null){ if(i != null){
resp = ((i.elemento % 2 == 0) ? 1 : 0); resp = ((i.elemento % 2 == 0) ? 1 : 0) + quantidadePares(i.esq) + quantidadePares(i.dir);
resp += quantidadePares(i.esq);
resp += quantidadePares(i.dir);
} }
return resp; return resp;
} }