Skip to content

Commit b549790

Browse files
committed
feat: adicionado arvore binaria
1 parent a62cebc commit b549790

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/java/ArvoreBinaria.java

+32-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Utiliza a classe No
33
*/
44

5+
56
public class ArvoreBinaria {
67

78

@@ -31,25 +32,26 @@ public static void main(String[] args) {
3132
preFixado(raiz);
3233
System.out.println();
3334

34-
System.out.println(removeValorMinimoDaArvore(raiz));
35+
System.out.println("Remove Valor: "+ removeValorMinimoDaArvore(raiz));
36+
System.out.println("Altura Arvore: "+AlturaArvore(raiz));
3537
}
3638

3739

3840
public static void inserir(No node, int valor) {
3941
if (node != null) {
40-
if (valor < node.chave) {
42+
if (valor < node.getChave()) {
4143
if (node.getEsq() != null) {
4244
inserir(node.getEsq(), valor);
4345
} else {
4446
System.out.println(" Inserindo " + valor + " a esquerda de " + node.getChave());
45-
node.esq = new No(valor);
47+
node.setEsq(new No(valor)); ;
4648
}
4749
} else if (valor > node.getChave()) {
4850
if (node.getDir() != null) {
49-
inserir(node.dir, valor);
51+
inserir(node.getDir(), valor);
5052
} else {
5153
System.out.println(" Inserindo " + valor + " a direita de " + node.getChave());
52-
node.dir = new No(valor);
54+
node.setDir(new No(valor));
5355
}
5456
}
5557
}
@@ -73,24 +75,44 @@ public static void posFixado(No no) {
7375

7476
public static void emOrdem(No no) {
7577
if (no != null) {
76-
emOrdem(no.esq);
77-
System.out.print(no.chave + " ");
78-
emOrdem(no.dir);
78+
emOrdem(no.getEsq());
79+
System.out.print(no.getChave() + " ");
80+
emOrdem(no.getDir());
7981
}
8082
}
8183

8284
public static No removeValorMinimoDaArvore(No node) {
8385
if (node == null) {
84-
System.out.println(" ERRO ");
86+
System.out.println(" ERROR ");
8587
} else if (node.getEsq() != null) {
86-
node.esq = removeValorMinimoDaArvore(node.getEsq());
88+
node.setEsq(removeValorMinimoDaArvore(node.getEsq()));
8789
return node;
8890
} else {
8991
return node.getDir();
9092
}
9193
return null;
9294
}
9395

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+
}
94114

95115
}
96116

117+
118+

0 commit comments

Comments
 (0)