Merge branch 'master' of https://github.com/icei-pucminas/aeds2
This commit is contained in:
commit
f041b8353d
|
|
@ -33,10 +33,13 @@ public class ArvoreBinaria {
|
|||
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);
|
||||
}
|
||||
|
|
@ -126,13 +129,17 @@ public class ArvoreBinaria {
|
|||
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!");
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
|
@ -247,11 +254,13 @@ public class ArvoreBinaria {
|
|||
*/
|
||||
public int getMaior(){
|
||||
int resp = -1;
|
||||
|
||||
if(raiz != null){
|
||||
No i;
|
||||
for(i = raiz; i.dir != null; i = i.dir);
|
||||
resp = i.elemento;
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
|
@ -262,11 +271,13 @@ public class ArvoreBinaria {
|
|||
*/
|
||||
public int getMenor(){
|
||||
int resp = -1;
|
||||
|
||||
if(raiz != null){
|
||||
No i;
|
||||
for(i = raiz; i.esq != null; i = i.esq);
|
||||
resp = i.elemento;
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
|
@ -279,6 +290,7 @@ public class ArvoreBinaria {
|
|||
return getAltura(raiz, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Metodo que retorna a altura da árvore
|
||||
* @return int altura da árvore
|
||||
|
|
@ -366,9 +378,7 @@ public class ArvoreBinaria {
|
|||
public int soma(No i){
|
||||
int resp = 0;
|
||||
if(i != null){
|
||||
resp = i.elemento;
|
||||
resp += soma(i.esq);
|
||||
resp += soma(i.dir);
|
||||
resp = i.elemento + soma(i.esq) + soma(i.dir);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
|
@ -380,9 +390,7 @@ public class ArvoreBinaria {
|
|||
public int quantidadePares(No i){
|
||||
int resp = 0;
|
||||
if(i != null){
|
||||
resp = ((i.elemento % 2 == 0) ? 1 : 0);
|
||||
resp += quantidadePares(i.esq);
|
||||
resp += quantidadePares(i.dir);
|
||||
resp = ((i.elemento % 2 == 0) ? 1 : 0) + quantidadePares(i.esq) + quantidadePares(i.dir);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue