Skip to content

Arvore Binaria #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1587,8 +1587,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
<a href="./src/java/ArvoreBinaria.java">
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" />
</a>
</td>
<td> <!-- Python -->
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 +
'}';
}
}