In this post find minimum is discussed. The operation is to find minimum in the given dimension. This is especially needed in delete operation.
For example, consider below KD Tree, if given dimension is x, then output should be 5 and if given dimensions is y, then output should be 12.
In KD tree, points are divided dimension by dimension. For example, root divides keys by dimension 0, level next to root divides by dimension 1, next level by dimension 2 if k is more then 2 (else by dimension 0), and so on.
To find minimum we traverse nodes starting from root. If dimension of current level is same as given dimension, then required minimum lies on left side if there is left child. This is same as Binary Search Tree Minimum. Above is simple, what to do when current level’s dimension is different. When dimension of current level is different, minimum may be either in left subtree or right subtree or current node may also be minimum. So we take minimum of three and return. This is different from Binary Search tree.
Below is C++ implementation of find minimum operation.
// A C++ program to demonstrate find minimum on KD tree
constintk = 2;
// A structure to represent node of kd tree
intpoint[k]; // To store k dimensional point
Node *left, *right;
// A method to create a node of K D tree
structNode* temp = newNode;
for(inti = 0; i < k; i++)
temp->point[i] = arr[i];
temp->left = temp->right = NULL;
// Inserts a new node and returns root of modified tree
// The parameter depth is used to decide axis of comparison