-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathBinarySearchTree.java
144 lines (125 loc) · 3.78 KB
/
BinarySearchTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Utiliza a classe No
*/
public class ArvoreDeBuscaBinaria {
public static void main(String[] args) {
No Arvore = new No(45);
insereArvore(Arvore, 56);
insereArvore(Arvore, 67);
insereArvore(Arvore, 24);
insereArvore(Arvore, 61);
insereArvore(Arvore, 52);
System.out.print("Pre : ");
preOrdem(Arvore);
System.out.print("\nEm : ");
emOrdem(Arvore);
System.out.print("\nPos : ");
posOrdem(Arvore);
excluiArvore(Arvore, 45);
excluiArvore(Arvore, 24);
excluiArvore(Arvore, 56);
System.out.println("\n");
System.out.print("Pre : ");
preOrdem(Arvore);
System.out.print("\nEm : ");
emOrdem(Arvore);
System.out.print("\nPos : ");
posOrdem(Arvore);
System.out.println("\n");
int chave = 56;
if (buscaBinaria(Arvore, chave) != null) System.out.println("\nEncontrou a chave " + chave);
else System.out.println("\nNao encontrou a chave " + chave);
System.out.println("Altura da arvore : " + alturaArvore(Arvore));
}
public static No buscaBinaria(No no, int chave) {
while (no != null) {
if (no.getChave() == chave) return no;
if (chave > no.getChave()) no = no.getDir();
else no = no.getEsq();
}
return null;
}
public static No buscaNoPai(No atual, int ch) {
No noPai = null;
while (atual != null) {
if (atual.getChave() == ch) return noPai;
noPai = atual;
if (atual.getChave() < ch) atual = atual.getDir();
else atual = atual.getEsq();
}
return noPai;
}
public static No maiorAesquerda(No atual) {
atual = atual.getEsq();
while (atual.getDir() != null) atual = atual.getDir();
return atual;
}
public static boolean excluiArvore(No no, int chave) {
No atual, noPai, substituto;
substituto = null;
atual = buscaBinaria(no, chave);
if (atual == null) return false; // Não encontrou a chave
noPai = buscaNoPai(no, chave);
if (atual.getEsq() == null || atual.getDir() == null) { // Se tem 0 ou 1 filho
if (atual.getEsq() == null) {
substituto = atual.getDir();
} else {
substituto = atual.getEsq();
}
if (noPai == null) { // Único que não tem pai é a raiz
no.setChave(substituto.getChave());
no.setEsq(substituto.getEsq());
no.setDir(substituto.getDir());
} else {
if (chave < noPai.getChave()) noPai.setEsq(substituto);
else noPai.setDir(substituto);
}
} else {
substituto = maiorAesquerda(atual);
atual.setChave(substituto.getChave());
if (substituto.getEsq() != null) atual.setEsq(substituto.getEsq());
else atual.setEsq(null);
}
return true;
}
public static void insereArvore(No arvore, int chave) {
No anterior = arvore;
while (arvore != null) {
anterior = arvore;
if (arvore.getChave() < chave) arvore = arvore.getDir();
else arvore = arvore.getEsq();
}
No novo = new No(chave);
if (anterior.getChave() < chave) anterior.setDir(novo);
else anterior.setEsq(novo);
}
public static void preOrdem(No no) {
if (no != null) {
System.out.print(no.getChave() + ", ");
preOrdem(no.getEsq());
preOrdem(no.getDir());
}
}
public static void posOrdem(No no) {
if (no != null) {
posOrdem(no.getEsq());
posOrdem(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 int max(int a, int b) {
if (a > b) return a;
return b;
}
public static int alturaArvore(No arvore) {
if (arvore == null) return 0;
return 1 + max(alturaArvore(arvore.getEsq()), alturaArvore(arvore.getDir()));
}
}