#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Trees

K-Dimensional Tree in Data Structure

A K-D Tree (K-Dimensional Tree) is a binary tree used to organize points in a k-dimensional space, enabling efficient operations like nearest-neighbor search and range search.

How It Works

At each level of the tree, points are split along one of the k dimensions, cycling through dimensions as depth increases (e.g., split by x at depth 0, by y at depth 1, back to x at depth 2, and so on for 2D points). The median point along the chosen dimension becomes the node, dividing the remaining points into left and right subtrees.

class KDNode:
    def __init__(self, point, left=None, right=None):
        self.point = point
        self.left = left
        self.right = right

def build_kdtree(points, depth=0):
    if not points:
        return None
    k = len(points[0])
    axis = depth % k
    points.sort(key=lambda p: p[axis])
    median = len(points) // 2
    return KDNode(
        point=points[median],
        left=build_kdtree(points[:median], depth + 1),
        right=build_kdtree(points[median + 1:], depth + 1)
    )

Complexity

OperationAverage Time
BuildO(n log n)
Nearest Neighbor SearchO(log n)
Range SearchO(sqrt(n) + m), m = results found

Applications

  • Nearest-neighbor search in machine learning (e.g., k-NN classification).
  • Spatial indexing in GIS and mapping applications.
  • Collision detection in computer graphics and games.
  • Database indexing for multi-dimensional data.

Ready to master Data Structures & Algorithms?

Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.

Explore Course