# K-D Tree in Data Structures

## Introduction

The K-D tree, also known as the K dimension tree, serves as a prominent data structure for the organization of points in multidimensional space, often when K represents a considerably high number. The allure of these structures lies in their ability to facilitate diverse methods of searching in the realm of multidimensional spaces, including range and nearest-neighbor searches.

## K-D Trees: The Framework

Every node within a binary tree is a manifestation of a "K-D Tree" and corresponds to a point in the K-dimensional space. The space itself is divided into two realms through the hyperplanes, which each non-leaf node in the tree embodies. The chosen axis, corresponding to one of the K dimensions, aligns itself parallel to these hyperplanes.

The selection of an axis can be approached through various methodologies, though the most conventional approach entails cycling sequentially through each of the K dimensions before defining a midpoint to enact the division of the space.

### Unlocking the Mechanics of a K-D Tree

At its heart, the K-D tree operates as a binary tree, with each node representing a k-dimensional point, thereby bifurcating the space into two distinct half-spaces. Here's a closer examination of the process:

1. Dimension Selection: The initial step revolves around the selection of a dimension. In a 2D K-D tree, a toggling process ensues between the x and y dimensions, while in higher dimensions, it cycles through the coordinates.
2. Discovering the Median: Within the dimension selected, the median point is unveiled by assessing the values within that specific dimension. This median point subsequently takes the throne as the root node of the tree.
3. Dividing the Territory: The dataset gets segmented into two subsets, with one being home to the points residing on one side of the median and the other harboring points on the opposing side. These subsets establish connections with the left and right subtrees of the root node.
4. Recursive Unfolding: Steps 1 through 3 enter a realm of recursion, as each of the subtrees undergoes a repetition of the process. The outcome is a tree structure where every node embodies a median point within a distinct dimension.

### The Blueprint for Constructing a K-D Tree

The construction of a K-D Tree hinges on the recursive division of points within space to yield a binary tree. The journey commences by selecting an axis. Subsequently, the remaining points are partitioned into two subsets based on their location concerning the partitioning hyperplane. This partitioning involves the selection of the midpoint along the axis.

### Algorithm 1: Creating a K-D Tree

When correctly executed, the resulting tree maintains balance, with every leaf node residing at an approximate equidistance from the root. To achieve this equilibrium, it is imperative to consistently opt for the median point. Additionally, it's noteworthy that pinpointing the median introduces complexity, necessitating the employment of a distinct algorithm. Failure to select the median point may result in an imbalanced tree. To address this, one approach involves employing a sorting algorithm to arrange points along the chosen axis and leveraging the median of these points as the splitting plane.

Code:

Output:

```Point found
```

In the presented code, a KD tree (K-dimensional tree) is brought into action to conduct operations such as insertion and the quest for points within the realm of K-dimensional space.