-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArbolKD.cxx
105 lines (87 loc) · 2.88 KB
/
ArbolKD.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
#include "ArbolKD.h"
#include "NodoKD.h"
#include "math.h"
// ------------------------------------------------------------------------
ArbolKD::ArbolKD() {
this->raiz = NULL;
}
// ------------------------------------------------------------------------
ArbolKD::ArbolKD(point &val) {
NodoKD *nnodo = new NodoKD(val);
this->raiz = nnodo;
}
// ------------------------------------------------------------------------
ArbolKD::~ArbolKD() {
if (this->raiz != NULL) {
delete this->raiz;
this->raiz = NULL;
}
}
// ------------------------------------------------------------------------
bool ArbolKD::esVacio() {
return (this->raiz == NULL);
}
// ------------------------------------------------------------------------
point& ArbolKD::datoRaiz() {
return (this->raiz)->obtenerDato();
}
// ------------------------------------------------------------------------
NodoKD* ArbolKD::obtenerRaiz() {
return this->raiz;
}
// ------------------------------------------------------------------------
void ArbolKD::fijarRaiz(NodoKD *n_raiz) {
this->raiz = n_raiz;
}
// ------------------------------------------------------------------------
void ArbolKD::preOrden() {
if (this->raiz != NULL)
this->raiz->preOrden();
std::cout << std::endl;
}
// ------------------------------------------------------------------------
void ArbolKD::inOrden() {
if (this->raiz != NULL)
this->raiz->inOrden();
std::cout << std::endl;
}
// ------------------------------------------------------------------------
void ArbolKD::posOrden() {
if (this->raiz != NULL)
this->raiz->posOrden();
std::cout << std::endl;
}
// ------------------------------------------------------------------------
void ArbolKD::nivelOrden() {
if (this->raiz != NULL)
this->raiz->nivelOrden();
std::cout << std::endl;
}
// ------------------------------------------------------------------------
bool ArbolKD::insertar(point& val) {
bool res;
if (this->raiz == NULL) {
this->raiz = new NodoKD(val);
res = true;
} else
res = this->raiz->insertar(val);
return res;
}
void ArbolKD::cola(std::queue<int> &colita) {
if (this->raiz != NULL)
this->raiz->cola(colita);
}
void ArbolKD::cercano(point valoraux,float &resultado,point &valorfinal){
if(resultado==0){
valorfinal=this->raiz->obtenerDato();
resultado=distancia(valoraux.x,valoraux.y,valoraux.z,this->raiz->obtenerDato().x,this->raiz->obtenerDato().y,this->raiz->obtenerDato().z);
}
this->raiz->cercano(valoraux,resultado,valorfinal);
}
float ArbolKD::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 - ArbolKD.hxx