-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArvore.java
224 lines (172 loc) · 6.15 KB
/
Arvore.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//terras dos filhos divididas igualmente entre seus filhos
public class Arvore {
private class Nodo{
private int terras; // valor armazenado no nodo
private Nodo pai; // pai do nodo
private Nodo [] filhos; // filhos do nodo
private int nFilhos; // número de filhos do nodo
private int nivel; // nível do nodo
private String nome;
public Nodo (Integer element, String nome){
pai = null;
filhos = new Nodo[10];
terras = element;
nFilhos = 0;
this.nome = nome;
}
public int terrasFilho(){
//aux.getTerras()/aux.getTamanhoSubArvore()
return terras/nFilhos;
}
public int getTerras()
{
return terras;
}
public void addterras(Integer element){
this.terras += element;
}
public void setPai(Nodo pai){
this.pai = pai;
}
public void setNivel(int nivel){
this.nivel = nivel;
}
public void addSubArvore (Nodo n){
if(nFilhos == filhos.length)
grow();
filhos[nFilhos] = n;
n.pai = this;
nFilhos++;
n.nivel = this.nivel; //aumenta nivel da criança
}
private void grow(){
Nodo aux [] = new Nodo[filhos.length*2];
for(int i=0; i<filhos.length; i++)
aux[i] = filhos[i];
filhos = aux;
}
// busca subtree pelo indice dentro da lista de filhos
Nodo getSubArvore(int i){
if(i>=0 && i<nFilhos)
return filhos[i];
return null;
}
//retorna o número de filhos
int getTamanhoSubArvore(){
return nFilhos;
}
int getNivel(){
return nivel;
}
String getNome(){
return nome;
}
Nodo getFilhos(int i){
return filhos[i];
}
}
private Nodo raiz;
private int tamanho;
private int maiorNivel;
public Arvore(){
this.raiz = null;
this.tamanho = 0;
this.maiorNivel = 0;
}
private Nodo procuraNodo(String pai, Nodo ref){
if(ref!=null){
if(ref.nome.equals(pai))
return ref;
else{
Nodo aux=null;
for(int i=0; i<ref.getTamanhoSubArvore(); i++){
aux = procuraNodo(pai, ref.getSubArvore(i));
if(aux != null)
return aux;
}
return null;
}
}
else
return null;
}
//insere o elemento e como filho de father
// Versao 0.1 -> Inclui root e inclui filho de root
// Versao 0.2 -> Permite a inclusão de mais níveis na árvore
public boolean add(Integer e, String pai, String nome){
Nodo aux;
if(tamanho==0){
this.raiz=new Nodo(e, nome);
raiz.setNivel(1);
}
else{
aux = procuraNodo(pai, raiz);
if(aux==null)
return false;
else{
Nodo jose = new Nodo(e, nome);
aux.addSubArvore(jose);
jose.setNivel(aux.getNivel()+1);
}
}
tamanho++;
return true;
}
// percorremos a árvore com caminhamento em largura e adicionamos as terras para os devidos filhos
public Nodo[] quaseMata(){
Nodo[] nodos = new Nodo[tamanho];
int indice = 0;
int posicao = 0;
nodos[indice] = raiz;
indice++;
while(indice<tamanho){
Nodo aux = procuraNodo(nodos[posicao].nome, raiz); //aux é o pai
posicao++;
if(aux!=null)
for(int i = 0; i<aux.getTamanhoSubArvore(); i++){
aux.getSubArvore(i).addterras(aux.terrasFilho());
nodos[indice] = aux.getSubArvore(i);// aux = searchNode(aux.children[i], aux)
indice++;
if(aux.getFilhos(i).getNivel() > maiorNivel){
this.maiorNivel = aux.getSubArvore(i).getNivel();
}
}
//pegar numero de terras do pai - feito
//dividir terras para cada filho - feito
}
return nodos;
}
//perguntar: se fazemos um caminhamento de pós-ordem ou se fazemos em largura para chegar no maior nível e, depois disso, achar o mais rico
//ou se fazemos um vetor com nodos do maior nivel e, depois, vemos qual deles é mais rico
public void maisRico(Nodo[] nodos){
Nodo Rico = raiz;
for(int i = 0; i < nodos.length; i++){
for(int j = 1; j < nodos.length-1; j++)
if(nodos[i].getNivel() == maiorNivel && nodos[j].getNivel() == maiorNivel) {
if(nodos[i].getTerras() > nodos[j].getTerras())
Rico = nodos[i];
else
Rico = nodos[j];
}
System.out.println(nodos[i].getNome() +", " +nodos[i].getTerras());
}
System.out.println("O mais rico da geração " +Rico.getNivel()+ " foi " +Rico.getNome()+ " com " +Rico.getTerras()+ " terras.");
}
}
// int [] positionsWidth(){
// if(nElements==0)
// return null;
// int [] lista = new int[this.nElements]; = tamanho
// int idx=0; - total de elementos percorridos
// int pos=0;
// lista[idx++]=root.value; - lista[0]= raiz / idx = 1
// while(idx<nElements){ - percorre todos os elementos
// TreeNode aux = searchNode(lista[pos++], this.root); - lista[0] comeca na raiz - lista[1]
// if(aux!=null)
// for(int i=0; i<aux.getSubtreeSize(); i++) - percorre os filhos da raiz
// lista[idx++]=aux.getSubtree(i).value; - adiciona filhos da raiz na lista
// }
// return lista;
//matar nodos e dividir número entre os filhos - feito
//descobrir maior nível?
//pegar mais rico do maior nível