我们强烈建议您参考下面的帖子,以此作为前提条件。
K维树|设置1(搜索和插入)
在这篇文章中,讨论了最小查找量。该操作是在给定尺寸中找到最小值。这在删除操作中特别需要。
例如,在KD树下面考虑,如果给定尺寸为x,则输出应为5,如果给定尺寸为y,则输出应为12。
在KD树中,点是按维度划分的。例如,如果k大于2(否则按维度0),则根将键按维度0进行划分,将根级按维度1划分,将根级按维度2进行划分,依此类推。
为了找到最小值,我们从根开始遍历节点。如果当前级别的尺寸与给定的尺寸相同,则如果有左子级,则所需的最小值位于左侧。这与“二进制搜索树最小值”相同。
上面很简单,当当前级别的尺寸不同时该怎么办。当当前级别的维数不同时,最小值可以在左子树或右子树中,或者当前节点也可以是最小值。因此,我们至少取三个,然后返回。这与二进制搜索树不同。
以下是查找最小操作的C++实现。
// A C++ program to demonstrate find minimum on KD tree
#include
using namespace std;
const int k = 2;
// A structure to represent node of kd tree
struct Node {
int point[k]; // To store k dimensional point
Node *left, *right;
};
// A method to create a node of K D tree
struct Node* newNode(int arr[])
{
struct Node* temp = new Node;
for (int i = 0; i < k; i++)
temp->point[i] = arr[i];
temp->left = temp->right = NULL;
return temp;
}
// Inserts a new node and returns root of modified tree
// The parameter depth is used to decide axis of comparison
Node* insertRec(Node* root, int point[], unsigned depth)
{
// Tree is empty?
if (root == NULL)
return newNode(point);
// Calculate current dimension (cd) of comparison
unsigned cd = depth % k;
// Compare the new point with root on current dimension 'cd'
// and decide the left or right subtree
if (point[cd] < (root->point[cd]))
root->left = insertRec(root->left, point, depth + 1);
else
root->right = insertRec(root->right, point, depth + 1);
return root;
}
// Function to insert a new point with given point in
// KD Tree and return new root. It mainly uses above recursive
// function "insertRec()"
Node* insert(Node* root, int point[])
{
return insertRec(root, point, 0);
}
// A utility function to find minimum of three integers
int min(int x, int y, int z)
{
return min(x, min(y, z));
}
// Recursively finds minimum of d'th dimension in KD tree
// The parameter depth is used to determine current axis.
int findMinRec(Node* root, int d, unsigned depth)
{
// Base cases
if (root == NULL)
return INT_MAX;
// Current dimension is computed using current depth and total
// dimensions (k)
unsigned cd = depth % k;
// Compare point with root with respect to cd (Current dimension)
if (cd == d) {
if (root->left == NULL)
return root->point[d];
return min(root->point[d], findMinRec(root->left, d, depth + 1));
}
// If current dimension is different then minimum can be anywhere
// in this subtree
return min(root->point[d],
findMinRec(root->left, d, depth + 1),
findMinRec(root->right, d, depth + 1));
}
// A wrapper over findMinRec(). Returns minimum of d'th dimension
int findMin(Node* root, int d)
{
// Pass current level or depth as 0
return findMinRec(root, d, 0);
}
// Driver program to test above functions
int main()
{
struct Node* root = NULL;
int points[][k] = { { 30, 40 }, { 5, 25 },
{ 70, 70 }, { 10, 12 }, { 50, 30 }, { 35, 45 } };
int n = sizeof(points) / sizeof(points[0]);
for (int i = 0; i < n; i++)
root = insert(root, points[i]);
cout << "Minimum of 0'th dimension is " << findMin(root, 0) << endl;
cout << "Minimum of 1'th dimension is " << findMin(root, 1) << endl;
return 0;
}
输出:
Minimum of 0'th dimension is 5
Minimum of 1'th dimension is 12
来源:
https://www.cs.umd.edu/class/spring2008/cmsc420/L19.kd-trees.pdf