2
2
* Utiliza a classe No
3
3
*/
4
4
5
+
5
6
public class ArvoreBinaria {
6
7
7
8
@@ -31,25 +32,26 @@ public static void main(String[] args) {
31
32
preFixado (raiz );
32
33
System .out .println ();
33
34
34
- System .out .println (removeValorMinimoDaArvore (raiz ));
35
+ System .out .println ("Remove Valor: " + removeValorMinimoDaArvore (raiz ));
36
+ System .out .println ("Altura Arvore: " +AlturaArvore (raiz ));
35
37
}
36
38
37
39
38
40
public static void inserir (No node , int valor ) {
39
41
if (node != null ) {
40
- if (valor < node .chave ) {
42
+ if (valor < node .getChave () ) {
41
43
if (node .getEsq () != null ) {
42
44
inserir (node .getEsq (), valor );
43
45
} else {
44
46
System .out .println (" Inserindo " + valor + " a esquerda de " + node .getChave ());
45
- node .esq = new No (valor );
47
+ node .setEsq ( new No (valor )); ;
46
48
}
47
49
} else if (valor > node .getChave ()) {
48
50
if (node .getDir () != null ) {
49
- inserir (node .dir , valor );
51
+ inserir (node .getDir () , valor );
50
52
} else {
51
53
System .out .println (" Inserindo " + valor + " a direita de " + node .getChave ());
52
- node .dir = new No (valor );
54
+ node .setDir ( new No (valor ) );
53
55
}
54
56
}
55
57
}
@@ -73,24 +75,44 @@ public static void posFixado(No no) {
73
75
74
76
public static void emOrdem (No no ) {
75
77
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 () );
79
81
}
80
82
}
81
83
82
84
public static No removeValorMinimoDaArvore (No node ) {
83
85
if (node == null ) {
84
- System .out .println (" ERRO " );
86
+ System .out .println (" ERROR " );
85
87
} else if (node .getEsq () != null ) {
86
- node .esq = removeValorMinimoDaArvore (node .getEsq ());
88
+ node .setEsq ( removeValorMinimoDaArvore (node .getEsq () ));
87
89
return node ;
88
90
} else {
89
91
return node .getDir ();
90
92
}
91
93
return null ;
92
94
}
93
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
+ }
94
114
95
115
}
96
116
117
+
118
+
0 commit comments