Skip to content

Commit abb92aa

Browse files
authored
Merge pull request #6 from BianchTech/develop
feat: kNearestNeighbor and insert new point
2 parents 4bb218d + aa50d07 commit abb92aa

File tree

4 files changed

+255
-36
lines changed

4 files changed

+255
-36
lines changed

examples/main.cpp

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,52 @@
33

44
int main(int argc, char const *argv[])
55
{
6+
int x, y, k;
7+
if (argc < 4) {
8+
std::cerr << "Uso: " << argv[0] << " <x> <y> <k>\n";
9+
x = 7;
10+
y = 4;
11+
k = 2;
12+
// return 1;
13+
} else {
14+
x = std::atoi(argv[1]);
15+
y = std::atoi(argv[2]);
16+
k = std::atoi(argv[3]);
17+
}
18+
619
std::vector<std::vector<int>> points = {
7-
{1, 2, 3},
8-
{2, 1, 3},
9-
{3, 2, 1},
10-
{7, 4, 0},
11-
{5, 9, 2},
12-
{6, 1, 8},
13-
{0, 3, 5},
14-
{4, 7, 6},
15-
{8, 2, 9},
16-
{3, 5, 7}
20+
{1, 2},
21+
{2, 1},
22+
{3, 2},
23+
{7, 4},
24+
{5, 9},
25+
{6, 1},
26+
{0, 3},
27+
{4, 7},
28+
{8, 2},
29+
{3, 5}
1730
};
1831

1932
auto tree = new KDTree();
2033
tree->BuildTree(points);
2134
tree->PrintInorder();
35+
36+
std::vector<std::vector<int>> points_knn = tree->KNearestNeighbor({x, y}, k);
37+
38+
for (size_t i = 0; i < points_knn.size(); ++i) {
39+
std::cout << "Point " << i << ": ";
40+
for (size_t j = 0; j < points_knn[i].size(); ++j) {
41+
std::cout << points_knn[i][j] << " ";
42+
}
43+
std::cout << std::endl;
44+
}
45+
46+
std::cout << std::endl;
47+
std::cout << std::endl;
48+
std::cout << std::endl;
49+
50+
tree->Insert({x+1, y-1});
51+
tree->PrintInorder();
52+
2253
return 0;
2354
}

examples/plot_results.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import matplotlib.pyplot as plt
2+
3+
# pontos fornecidos
4+
points = [
5+
(1, 2),
6+
(2, 1),
7+
(3, 2),
8+
(7, 4),
9+
(5, 9),
10+
(6, 1),
11+
(0, 3),
12+
(4, 7),
13+
(8, 2),
14+
(3, 5)
15+
]
16+
17+
# separar em listas de x e y
18+
x_vals = [p[0] for p in points]
19+
y_vals = [p[1] for p in points]
20+
21+
# plot
22+
plt.figure(figsize=(6,6))
23+
plt.scatter(x_vals, y_vals, color="blue")
24+
25+
# adicionar labels
26+
for (x, y) in points:
27+
plt.text(x+0.1, y+0.1, f"({x},{y})", fontsize=8)
28+
29+
plt.xlabel("x")
30+
plt.ylabel("y")
31+
plt.title("Plot dos pontos fornecidos")
32+
plt.grid(True)
33+
plt.show()

include/mlcpppy/classifiers/neighbors/kdtree.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,29 @@
1818
#define KDTREE_H
1919

2020
#include <vector>
21+
#include <queue>
2122
#include "nearest_neighbor.h"
2223

2324
class KDTree : public NearestNeighbor {
2425
private:
25-
std::vector<std::vector<int>> points;
2626
class Node;
27-
Node* root;
27+
Node* root_;
28+
int K_;
29+
std::priority_queue<std::pair<double, Node*>> bests_;
30+
2831
Node* Build(std::vector<std::vector<int>> points, int depth);
32+
Node* NearestNeighbor(Node* root, std::vector<int>& target, int depth);
33+
void KNearestNeighbor(Node* root, std::vector<int>& target, int depth);
34+
35+
Node* Closest(Node* n0, Node* n1, std::vector<int>& target);
36+
double DistSquared(const std::vector<int>& p0, const std::vector<int>& p1);
2937
static void Inorder(Node* root);
3038

3139
public:
3240
KDTree();
3341
void Insert(std::vector<int> point) override;
3442
void BuildTree(std::vector<std::vector<int>> points) override;
35-
std::vector<std::vector<int>> KNearestNeighbor(std::vector<int> target_points,
36-
int k) override;
43+
std::vector<std::vector<int>> KNearestNeighbor(std::vector<int> target_points, int k) override;
3744
void PrintInorder();
3845

3946
~KDTree() override;

0 commit comments

Comments
 (0)