Javatpoint Logo
Javatpoint Logo

K-Dimensional tree in C++

A data structure is called a K-dimensional tree, or simply a K-D tree. It is made for effective spatial searches in K-dimensional domains. It is a multi-dimensional generalization of the binary search tree. K-D trees are used in a variety of domains, including computer graphics, nearest-neighbor analysis, and data mining. They are very helpful for tackling a variety of computational geometry issues, such as range search and nearest neighbor search.

Properties and Structure:

  1. Each node in a K-D tree represents a point in the K-dimensional space.
  2. Each node contains two children: a left child and a right child, arranged in a binary tree structure.
  3. At every level of the tree, points are split alternating along several dimensions.
  4. The selection of the dividing dimension at each level might be based on several heuristics, such as choosing the dimension with the greatest spread or cycling through the different dimensions.


  1. Insertion: To add a new point to the K-D tree, one must navigate the tree to choose the best location for the new node by the splitting rule.
  2. Search: K-D trees allow several search functions, such as range search, which locates all points inside a given range, and nearest neighbor search, which locates the point in the tree that is closest to a particular query point.
  3. Deletion: Carefully examining the tree structure and rearranging the tree as necessary are required when removing a node from the K-D tree while preserving its attributes.


K-D trees are widely used in many different fields, such as:

  1. Applications such as databases and data mining use nearest neighbor search.
  2. Geographic information systems (GIS) use spatial indexing.
  3. Effective range search for geographical information systems and computational geometry.
  4. To produce scenes more quickly in computer graphics by speeding up ray tracing algorithms.


Let's take an example to illustrate the K-Dimensional tree in C++:


Nearest Neighbor: [6, 12]
Points within range:
Point: [6, 12]
  1. Construction of a 2D point K-D tree.
  2. Find the closest neighbor for a specific query point.
  3. Use range search to locate points within a given range.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA