K Dimensional Tree - GeeksforGeeks
A K-D Tree(also called as K-Dimensional Tree) is a binary search tree where data in each node is a K-Dimensional point in space. In short, it is a space partitioning(details below) data structure for organizing points in a K-Dimensional space.
A non-leaf node in K-D tree divides the space into two parts, called as half-spaces.
Points to the left of this space are represented by the left subtree of that node and points to the right of the space are represented by the right subtree.
http://www.geeksforgeeks.org/k-dimensional-tree/
http://www.geeksforgeeks.org/k-dimensional-tree-set-2-find-minimum/
Java code: http://www.sanfoundry.com/java-program-construct-k-d-tree-2-dimensional-data-assume-static-data/
https://github.com/phishman3579/java-algorithms-implementation/blob/master/src/com/jwetherell/algorithms/data_structures/KdTree.java
https://www.safaribooksonline.com/library/view/working-with-algorithms/9781491907818/part10.html
Read full article from K Dimensional Tree - GeeksforGeeks
A K-D Tree(also called as K-Dimensional Tree) is a binary search tree where data in each node is a K-Dimensional point in space. In short, it is a space partitioning(details below) data structure for organizing points in a K-Dimensional space.
A non-leaf node in K-D tree divides the space into two parts, called as half-spaces.
Points to the left of this space are represented by the left subtree of that node and points to the right of the space are represented by the right subtree.
The root would have an x-aligned plane, the root’s children would both have y-aligned planes, the root’s grandchildren would all have x-aligned planes, and the root’s great-grandchildren would all have y-aligned planes and so on.
Generalization:
Let us number the planes as 0, 1, 2, …(K – 1). From the above example, it is quite clear that a point (node) at depth D will have A aligned plane where A is calculated as:
Let us number the planes as 0, 1, 2, …(K – 1). From the above example, it is quite clear that a point (node) at depth D will have A aligned plane where A is calculated as:
A = D mod K
// A structure to represent a point in K dimensional space
// k: K dimensional space
// coord: An array to represent position of point
struct
Point
{
unsigned k;
int
* coord;
// Coordinate (A pointer to array of size k)
};
struct
Node
{
Point point;
Node *left, *right;
};
// Root is passed as pointer to pointer so that
// The parameter depth is used to decide axis of comparison
void
InsertKDTreeUtil(Node * * root, Node* newNode, unsigned depth)
{
// Tree is empty?
if
(!*root)
{
*root = newNode;
return
;
}
// Calculate axis of comparison to determine left/right
unsigned axisOfComparison = depth % (newNode->point).k;
// Compare the new point with root and decide the left or
// right subtree
if
((newNode->point).coord[axisOfComparison] <
((*root)->point).coord[axisOfComparison])
InsertKDTreeUtil(&((*root)->left), newNode, depth + 1);
else
InsertKDTreeUtil(&((*root)->right), newNode, depth + 1);
}
// Function to insert a new point in KD Tree. It mainly uses
// above recursive function "InsertKDTreeUtil()"
void
InsertKDTree(Node* *root, Point* point)
{
Node* newNode = CreateNode(point);
unsigned zeroDepth = 0;
InsertKDTreeUtil(root, newNode, zeroDepth);
}
// Searches a Point in the K D tree. The parameter depth is used
// to determine current axis.
int
SearchKDTreeUtil(Node* root, Point point, unsigned depth)
{
if
(!root)
return
0;
if
(ArePointsSame(root->point, point))
return
1;
unsigned axisOfComparison = depth % point.k;
if
(point.coord[axisOfComparison] <
(root->point).coord[axisOfComparison])
return
SearchKDTreeUtil(root->left, point, depth + 1);
return
SearchKDTreeUtil(root->right, point, depth + 1);
}
// Searches a Point in the K D tree. It mainly uses
// SearchKDTreeUtil()
int
SearchKDTree(Node* root, Point point)
{
unsigned zeroDepth = 0;
return
SearchKDTreeUtil(root, point, zeroDepth);
}
// Creates a KD tree from given input points. It mainly
// uses InsertKDTree
Node* CreateKDTree(Input* input)
{
Node* root = NULL;
for
(
int
i = 0; i < input->n; ++i)
InsertKDTree(&root, input->pointArray[i]);
return
root;
}
http://www.geeksforgeeks.org/k-dimensional-tree-set-2-find-minimum/
Java code: http://www.sanfoundry.com/java-program-construct-k-d-tree-2-dimensional-data-assume-static-data/
https://github.com/phishman3579/java-algorithms-implementation/blob/master/src/com/jwetherell/algorithms/data_structures/KdTree.java
https://www.safaribooksonline.com/library/view/working-with-algorithms/9781491907818/part10.html
Read full article from K Dimensional Tree - GeeksforGeeks