Skip to content

Commit 13f1c54

Browse files
authored
Merge pull request kelvins#147 from AJohnsoon/main
Arvore Binaria
2 parents 79177eb + 4009f83 commit 13f1c54

File tree

3 files changed

+129
-3
lines changed

3 files changed

+129
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,8 +1587,8 @@ Com o objetivo de alcançar uma abrangência maior e encorajar novas pessoas a c
15871587
</a>
15881588
</td>
15891589
<td> <!-- Java -->
1590-
<a href="./CONTRIBUTING.md">
1591-
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/github/github-original.svg" />
1590+
<a href="./src/java/ArvoreBinaria.java">
1591+
<img align="center" height="25" src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/java/java-original.svg" />
15921592
</a>
15931593
</td>
15941594
<td> <!-- Python -->

src/java/ArvoreBinaria.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Utiliza a classe No
3+
*/
4+
5+
6+
public class ArvoreBinaria {
7+
8+
9+
public static void main(String[] args) {
10+
No raiz = new No(9);
11+
12+
inserir(raiz, 3);
13+
inserir(raiz, 4);
14+
inserir(raiz, 6);
15+
inserir(raiz, 7);
16+
inserir(raiz, 8);
17+
inserir(raiz, 10);
18+
inserir(raiz, 13);
19+
inserir(raiz, 14);
20+
21+
System.out.println();
22+
23+
System.out.printf("Em ordem: ");
24+
emOrdem(raiz);
25+
System.out.println();
26+
27+
System.out.printf("Pós-fixado: ");
28+
posFixado(raiz);
29+
System.out.println();
30+
31+
System.out.printf("Pré-fixado: ");
32+
preFixado(raiz);
33+
System.out.println();
34+
35+
System.out.println("Remove Valor: "+ removeValorMinimoDaArvore(raiz));
36+
System.out.println("Altura Arvore: "+AlturaArvore(raiz));
37+
}
38+
39+
40+
public static void inserir(No node, int valor) {
41+
if (node != null) {
42+
if (valor < node.getChave()) {
43+
if (node.getEsq() != null) {
44+
inserir(node.getEsq(), valor);
45+
} else {
46+
System.out.println(" Inserindo " + valor + " a esquerda de " + node.getChave());
47+
node.setEsq(new No(valor)); ;
48+
}
49+
} else if (valor > node.getChave()) {
50+
if (node.getDir() != null) {
51+
inserir(node.getDir(), valor);
52+
} else {
53+
System.out.println(" Inserindo " + valor + " a direita de " + node.getChave());
54+
node.setDir(new No(valor));
55+
}
56+
}
57+
}
58+
}
59+
60+
public static void preFixado(No no) {
61+
if (no != null) {
62+
System.out.print(no.getChave() + " ");
63+
preFixado(no.getEsq());
64+
preFixado(no.getDir());
65+
}
66+
}
67+
68+
public static void posFixado(No no) {
69+
if (no != null) {
70+
posFixado(no.getEsq());
71+
posFixado(no.getDir());
72+
System.out.print(no.getChave() + " ");
73+
}
74+
}
75+
76+
public static void emOrdem(No no) {
77+
if (no != null) {
78+
emOrdem(no.getEsq());
79+
System.out.print(no.getChave() + " ");
80+
emOrdem(no.getDir());
81+
}
82+
}
83+
84+
public static No removeValorMinimoDaArvore(No node) {
85+
if (node == null) {
86+
System.out.println(" ERROR ");
87+
} else if (node.getEsq() != null) {
88+
node.setEsq(removeValorMinimoDaArvore(node.getEsq()));
89+
return node;
90+
} else {
91+
return node.getDir();
92+
}
93+
return null;
94+
}
95+
96+
public static int AlturaArvore(No no){
97+
98+
if(no == null){
99+
return -1;
100+
}
101+
else{
102+
103+
int esquerda = AlturaArvore(no.getEsq());
104+
int direita = AlturaArvore(no.getDir());
105+
106+
if(esquerda < direita ){
107+
return direita+1;
108+
}
109+
else{
110+
return esquerda+1;
111+
}
112+
}
113+
}
114+
115+
}
116+
117+
118+

src/java/No.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ public No getDir() {
2525
public void setDir(No dir) {
2626
this.dir = dir;
2727
}
28-
28+
29+
@Override
30+
public String toString() {
31+
return "No{" +
32+
"chave: " + chave +
33+
", esq: " + esq +
34+
", dir: " + dir +
35+
'}';
36+
}
2937
}

0 commit comments

Comments
 (0)