Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<a href="./src/java/ArvoreBinaria.java">
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
</a>
</td>
Expand Down
118 changes: 118 additions & 0 deletions src/java/ArvoreBinaria.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Utiliza a classe No
*/


public class ArvoreBinaria {


public static void main(String[] args) {
No raiz = new No(9);

inserir(raiz, 3);
inserir(raiz, 4);
inserir(raiz, 6);
inserir(raiz, 7);
inserir(raiz, 8);
inserir(raiz, 10);
inserir(raiz, 13);
inserir(raiz, 14);

System.out.println();

System.out.printf("Em ordem: ");
emOrdem(raiz);
System.out.println();

System.out.printf("Pós-fixado: ");
posFixado(raiz);
System.out.println();

System.out.printf("Pré-fixado: ");
preFixado(raiz);
System.out.println();

System.out.println("Remove Valor: "+ removeValorMinimoDaArvore(raiz));
System.out.println("Altura Arvore: "+AlturaArvore(raiz));
}


public static void inserir(No node, int valor) {
if (node != null) {
if (valor < node.getChave()) {
if (node.getEsq() != null) {
inserir(node.getEsq(), valor);
} else {
System.out.println(" Inserindo " + valor + " a esquerda de " + node.getChave());
node.setEsq(new No(valor)); ;
}
} else if (valor > node.getChave()) {
if (node.getDir() != null) {
inserir(node.getDir(), valor);
} else {
System.out.println(" Inserindo " + valor + " a direita de " + node.getChave());
node.setDir(new No(valor));
}
}
}
}

public static void preFixado(No no) {
if (no != null) {
System.out.print(no.getChave() + " ");
preFixado(no.getEsq());
preFixado(no.getDir());
}
}

public static void posFixado(No no) {
if (no != null) {
posFixado(no.getEsq());
posFixado(no.getDir());
System.out.print(no.getChave() + " ");
}
}

public static void emOrdem(No no) {
if (no != null) {
emOrdem(no.getEsq());
System.out.print(no.getChave() + " ");
emOrdem(no.getDir());
}
}

public static No removeValorMinimoDaArvore(No node) {
if (node == null) {
System.out.println(" ERROR ");
} else if (node.getEsq() != null) {
node.setEsq(removeValorMinimoDaArvore(node.getEsq()));
return node;
} else {
return node.getDir();
}
return null;
}

public static int AlturaArvore(No no){

if(no == null){
return -1;
}
else{

int esquerda = AlturaArvore(no.getEsq());
int direita = AlturaArvore(no.getDir());

if(esquerda < direita ){
return direita+1;
}
else{
return esquerda+1;
}
}
}

}



10 changes: 9 additions & 1 deletion src/java/No.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,13 @@ public No getDir() {
public void setDir(No dir) {
this.dir = dir;
}


@Override
public String toString() {
return "No{" +
"chave: " + chave +
", esq: " + esq +
", dir: " + dir +
'}';
}
}