Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,52 @@

int main(int argc, char const *argv[])
{
int x, y, k;
if (argc < 4) {
std::cerr << "Uso: " << argv[0] << " <x> <y> <k>\n";
x = 7;
y = 4;
k = 2;
// return 1;
} else {
x = std::atoi(argv[1]);
y = std::atoi(argv[2]);
k = std::atoi(argv[3]);
}

std::vector<std::vector<int>> points = {
{1, 2, 3},
{2, 1, 3},
{3, 2, 1},
{7, 4, 0},
{5, 9, 2},
{6, 1, 8},
{0, 3, 5},
{4, 7, 6},
{8, 2, 9},
{3, 5, 7}
{1, 2},
{2, 1},
{3, 2},
{7, 4},
{5, 9},
{6, 1},
{0, 3},
{4, 7},
{8, 2},
{3, 5}
};

auto tree = new KDTree();
tree->BuildTree(points);
tree->PrintInorder();

std::vector<std::vector<int>> points_knn = tree->KNearestNeighbor({x, y}, k);

for (size_t i = 0; i < points_knn.size(); ++i) {
std::cout << "Point " << i << ": ";
for (size_t j = 0; j < points_knn[i].size(); ++j) {
std::cout << points_knn[i][j] << " ";
}
std::cout << std::endl;
}

std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;

tree->Insert({x+1, y-1});
tree->PrintInorder();

return 0;
}
33 changes: 33 additions & 0 deletions examples/plot_results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import matplotlib.pyplot as plt

# pontos fornecidos
points = [
(1, 2),
(2, 1),
(3, 2),
(7, 4),
(5, 9),
(6, 1),
(0, 3),
(4, 7),
(8, 2),
(3, 5)
]

# separar em listas de x e y
x_vals = [p[0] for p in points]
y_vals = [p[1] for p in points]

# plot
plt.figure(figsize=(6,6))
plt.scatter(x_vals, y_vals, color="blue")

# adicionar labels
for (x, y) in points:
plt.text(x+0.1, y+0.1, f"({x},{y})", fontsize=8)

plt.xlabel("x")
plt.ylabel("y")
plt.title("Plot dos pontos fornecidos")
plt.grid(True)
plt.show()
15 changes: 11 additions & 4 deletions include/mlcpppy/classifiers/neighbors/kdtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@
#define KDTREE_H

#include <vector>
#include <queue>
#include "nearest_neighbor.h"

class KDTree : public NearestNeighbor {
private:
std::vector<std::vector<int>> points;
class Node;
Node* root;
Node* root_;
int K_;
std::priority_queue<std::pair<double, Node*>> bests_;

Node* Build(std::vector<std::vector<int>> points, int depth);
Node* NearestNeighbor(Node* root, std::vector<int>& target, int depth);
void KNearestNeighbor(Node* root, std::vector<int>& target, int depth);

Node* Closest(Node* n0, Node* n1, std::vector<int>& target);
double DistSquared(const std::vector<int>& p0, const std::vector<int>& p1);
static void Inorder(Node* root);

public:
KDTree();
void Insert(std::vector<int> point) override;
void BuildTree(std::vector<std::vector<int>> points) override;
std::vector<std::vector<int>> KNearestNeighbor(std::vector<int> target_points,
int k) override;
std::vector<std::vector<int>> KNearestNeighbor(std::vector<int> target_points, int k) override;
void PrintInorder();

~KDTree() override;
Expand Down
Loading