-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodoKD.cxx
206 lines (188 loc) · 5.55 KB
/
NodoKD.cxx
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
#include "NodoKD.h"
#include <queue>
#include <list>
#include <math.h>
// ------------------------------------------------------------------------
NodoKD::NodoKD() {
this->hijoIzq = NULL;
this->hijoDer = NULL;
}
// ------------------------------------------------------------------------
NodoKD::NodoKD(point &val){
this->dato = val;
this->hijoIzq = NULL;
this->hijoDer = NULL;
}
// ------------------------------------------------------------------------
NodoKD::NodoKD(point &val, NodoKD *izq, NodoKD *der) {
this->dato = val;
this->hijoIzq = izq;
this->hijoDer = der;
}
// ------------------------------------------------------------------------
NodoKD::~NodoKD() {
if (this->hijoIzq != NULL) {
delete this->hijoIzq;
this->hijoIzq = NULL;
}
if (this->hijoDer != NULL) {
delete this->hijoDer;
this->hijoDer = NULL;
}
}
// ------------------------------------------------------------------------
bool NodoKD::esHoja() {
return (this->hijoIzq == NULL && this->hijoDer == NULL);
}
// ------------------------------------------------------------------------
point& NodoKD::obtenerDato() {
return this->dato;
}
// ------------------------------------------------------------------------
void NodoKD::fijarDato(point &val) {
this->dato = val;
}
// ------------------------------------------------------------------------
NodoKD* NodoKD::obtenerHijoIzq() {
return this->hijoIzq;
}
// ------------------------------------------------------------------------
NodoKD* NodoKD::obtenerHijoDer() {
return this->hijoDer;
}
// ------------------------------------------------------------------------
void NodoKD::fijarHijoIzq(NodoKD *izq) {
this->hijoIzq = izq;
}
// ------------------------------------------------------------------------
void NodoKD::fijarHijoDer(NodoKD *der) {
this->hijoDer = der;
}
// ------------------------------------------------------------------------
void NodoKD::preOrden() {
std::cout << this->dato << "\t";
if (this->hijoIzq != NULL)
this->hijoIzq->preOrden();
if (this->hijoDer != NULL)
this->hijoDer->preOrden();
}
// ------------------------------------------------------------------------
void NodoKD::inOrden() {
if (this->hijoIzq != NULL)
this->hijoIzq->inOrden();
std::cout << this->dato << "\t";
if (this->hijoDer != NULL)
this->hijoDer->inOrden();
}
// ------------------------------------------------------------------------
void NodoKD::posOrden() {
if (this->hijoIzq != NULL)
this->hijoIzq->posOrden();
if (this->hijoDer != NULL)
this->hijoDer->posOrden();
std::cout << this->dato << "\t";
}
// ------------------------------------------------------------------------
void NodoKD::nivelOrden() {
std::queue< NodoKD* > q;
NodoKD *t = this;
while (t != NULL) {
std::cout << t->dato << "\t";
if (t->hijoIzq != NULL)
q.push(t->hijoIzq);
if (t->hijoDer != NULL)
q.push(t->hijoDer);
if (!q.empty()) {
t = q.front();
q.pop();
} else
t = NULL;
}
}
// ------------------------------------------------------------------------
bool NodoKD::insertar(point &val) {
NodoKD *p = this;
NodoKD *pp = NULL;
bool insertado = false;
bool duplicado = false;
unsigned int dim = 0;
while (p != NULL) {
pp = p;
if (val == p->dato) {
duplicado = true;
break;
} else {
if (dim == 0) {
if (val.x < (p->dato).x)
p = p->hijoIzq;
else if (val.x > (p->dato).x)
p = p->hijoDer;
} else if (dim == 1) {
if (val.y < (p->dato).y)
p = p->hijoIzq;
else if (val.y > (p->dato).y)
p = p->hijoDer;
}
else if(dim==2){
if (val.z < (p->dato).z)
p = p->hijoIzq;
else if (val.z > (p->dato).z)
p = p->hijoDer;
}
dim = (dim + 1) % 3;
}
}
if (!duplicado) {
NodoKD *n = new NodoKD(val);
dim = (dim - 1) % 3;
if (n != NULL) {
if (dim == 0) {
if (val.x < (pp->dato).x)
pp->fijarHijoIzq(n);
else
pp->fijarHijoDer(n);
} else if (dim == 1) {
if (val.y < (pp->dato).y)
pp->fijarHijoIzq(n);
else
pp->fijarHijoDer(n);
}
else if (dim == 2) {
if (val.z < (pp->dato).z)
pp->fijarHijoIzq(n);
else
pp->fijarHijoDer(n);
}
}
insertado = true;
}
return insertado;
}
void NodoKD::cola(std::queue<int> &colita) {
colita.push(this->dato.x);
colita.push(this->dato.y);
colita.push(this->dato.z);
if (this->hijoIzq != NULL)
this->hijoIzq->cola(colita);
if (this->hijoDer != NULL)
this->hijoDer->cola(colita);
}
void NodoKD::cercano(point valoraux,float &resultado,point &valorfinal){
float dist;
dist=this->distancia(valoraux.x,valoraux.y,valoraux.z,this->dato.x,this->dato.y,this->dato.z);
if(dist<resultado){
resultado=dist;
valorfinal=this->dato;
}
if (this->hijoIzq != NULL)
this->hijoIzq->cercano(valoraux,resultado,valorfinal);
if (this->hijoDer != NULL)
this->hijoDer->cercano(valoraux,resultado,valorfinal);
}
float NodoKD::distancia(float x1,float y1,float z1,float x2,float y2,float z2){
float Xresul=pow((x2-x1),2);
float Yresul=pow((y2-y1),2);
float Zresul=pow((z2-z1),2);
return sqrt(Xresul+Yresul+Zresul);
}
// eof - NodoKD.hxx