Share kd tree c++ source code

quochienphamnha

New member
## KD Tree C ++ Mã nguồn

Cây KD (cây K-chiều) là một cấu trúc dữ liệu phân vùng không gian có thể được sử dụng để tìm kiếm hàng xóm gần nhất hiệu quả trong không gian đa chiều.Nó là một cây nhị phân trong đó mỗi nút được liên kết với một siêu phẳng chia không gian thành hai phần.Subtree bên trái của một nút chứa tất cả các điểm nhỏ hơn siêu phẳng và cây con bên phải chứa tất cả các điểm lớn hơn siêu phẳng.

Cây KD thường được sử dụng cho tìm kiếm hàng xóm gần nhất vì chúng có thể được xây dựng theo thời gian O (n log n), trong đó n là số điểm trong tập dữ liệu.Chúng cũng có thể được sử dụng để tìm K hàng xóm gần nhất của một điểm nhất định trong thời gian O (K log n).

Sau đây là mã nguồn C ++ cho cây KD:

`` `C ++
#include <Istream>
#include <Vector>

sử dụng không gian tên STD;

// Một điểm trong không gian 2D
Cấu trúc điểm {
gấp đôi x;
gấp đôi y;
};

// một nút trong cây KD
Nút cấu trúc {
Điểm điểm;
Nút *trái;
Nút *phải;
};

// một chức năng để tạo một nút mới
Nút *newnode (điểm điểm) {
Nút *node = new node ();
nút-> điểm = điểm;
nút-> trái = nullptr;
nút-> right = nullptr;
trả về nút;
}

// một chức năng để đệ quy một điểm vào cây KD
Nút *chèn (nút *gốc, điểm điểm, kích thước int) {
// Nếu cây trống, hãy tạo một nút gốc mới
if (root == nullptr) {
trả về newnode (điểm);
}

// Tìm kích thước nhỏ hơn của điểm và nút hiện tại
int nhỏ hơnDimension = (kích thước + 1) % 2;
Double pointValue = point [commentDimension];
nodevalue kép = root-> point [nhỏ hơn];

// Nếu điểm nhỏ hơn nút hiện tại, hãy chèn nó vào cây con bên trái
if (pointValue <nodeValue) {
root-> trái = chèn (root-> trái, điểm, kích thước);
} khác {
// Nếu không, hãy chèn nó vào Subtree bên phải
root-> right = chèn (root-> right, điểm, kích thước);
}

// Trả về nút gốc
trả lại gốc;
}

// một chức năng để tìm người lân cận gần nhất của một điểm trong cây KD
Nút *findnearestNeighbor (nút *root, điểm điểm, kích thước int) {
// Nếu cây trống, hãy trả lại null
if (root == nullptr) {
trả lại nullptr;
}

// Tìm kích thước nhỏ hơn của điểm và nút hiện tại
int nhỏ hơnDimension = (kích thước + 1) % 2;
Double pointValue = point [commentDimension];
nodevalue kép = root-> point [nhỏ hơn];

// Nếu điểm bằng nút hiện tại, hãy trả lại nút hiện tại
if (pointValue == nodeValue) {
trả lại gốc;
}

// Nếu điểm nhỏ hơn nút hiện tại, hãy tìm kiếm đệ quy bên trái
if (pointValue <nodeValue) {
Nút *gần nhất: FindNearestNeighbor (root-> trái, điểm, kích thước);

// Nếu người hàng xóm gần nhất không phải là NULL, hãy trả lại
if (gần nhất! = nullptr) {
trở lại gần nhất;
}
}

// Nếu không, hãy tìm kiếm đệ quy Subtree đúng
trả về findnearestNeighbor (root-> right, point, dimension);
}

int main () {
// Tạo một vectơ điểm
vector <oint> point = {
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
};

// Tạo cây KD từ các điểm
Nút
=======================================
## KD Tree C++ Source Code

KD Tree (K-dimensional tree) is a space-partitioning data structure that can be used for efficient nearest neighbor search in a multidimensional space. It is a binary tree in which each node is associated with a hyperplane that divides the space into two parts. The left subtree of a node contains all of the points that are less than the hyperplane, and the right subtree contains all of the points that are greater than the hyperplane.

KD Trees are often used for nearest neighbor search because they can be constructed in O(n log n) time, where n is the number of points in the dataset. They can also be used to find the k nearest neighbors of a given point in O(k log n) time.

The following is a C++ source code for a KD Tree:

```c++
#include <iostream>
#include <vector>

using namespace std;

// A point in 2D space
struct Point {
double x;
double y;
};

// A node in a KD Tree
struct Node {
Point point;
Node *left;
Node *right;
};

// A function to create a new node
Node *newNode(Point point) {
Node *node = new Node();
node->point = point;
node->left = nullptr;
node->right = nullptr;
return node;
}

// A function to recursively insert a point into a KD Tree
Node *insert(Node *root, Point point, int dimension) {
// If the tree is empty, create a new root node
if (root == nullptr) {
return newNode(point);
}

// Find the smaller dimension of the point and the current node
int smallerDimension = (dimension + 1) % 2;
double pointValue = point[smallerDimension];
double nodeValue = root->point[smallerDimension];

// If the point is less than the current node, insert it into the left subtree
if (pointValue < nodeValue) {
root->left = insert(root->left, point, dimension);
} else {
// Otherwise, insert it into the right subtree
root->right = insert(root->right, point, dimension);
}

// Return the root node
return root;
}

// A function to find the nearest neighbor of a point in a KD Tree
Node *findNearestNeighbor(Node *root, Point point, int dimension) {
// If the tree is empty, return null
if (root == nullptr) {
return nullptr;
}

// Find the smaller dimension of the point and the current node
int smallerDimension = (dimension + 1) % 2;
double pointValue = point[smallerDimension];
double nodeValue = root->point[smallerDimension];

// If the point is equal to the current node, return the current node
if (pointValue == nodeValue) {
return root;
}

// If the point is less than the current node, recursively search the left subtree
if (pointValue < nodeValue) {
Node *nearestNeighbor = findNearestNeighbor(root->left, point, dimension);

// If the nearest neighbor is not null, return it
if (nearestNeighbor != nullptr) {
return nearestNeighbor;
}
}

// Otherwise, recursively search the right subtree
return findNearestNeighbor(root->right, point, dimension);
}

int main() {
// Create a vector of points
vector<Point> points = {
{0, 0},
{1, 1},
{2, 2},
{3, 3},
{4, 4},
};

// Create a KD Tree from the points
Node
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top