Back to Course

0% Complete
0/0 Steps

Lesson 20 of 48
In Progress

# KD Trees in C++

##### Yasin Cakal

KD Trees are one of the most popular data structures used in computer science and programming today. A KD Tree, or K-dimensional Tree, is a type of space-partitioning data structure that is used to organize data points in a k-dimensional space. KD Trees are very useful in a number of applications, such as search algorithms, nearest neighbor search, and compression. In this article, we’ll be exploring how KD Trees work, as well as their time and space complexity. We’ll also provide code examples to illustrate how to use KD Trees in C++.

## What is a KD Tree?

A KD Tree is a type of binary tree that is used to organize points in a k-dimensional space. It is a space-partitioning data structure that splits a k-dimensional space into two halves at each level. For example, a 3D KD Tree will split a 3D space into two halves at each level. The tree is constructed by recursively splitting the space into two halves using a hyperplane. The hyperplane is determined by the position of the data points in the space.

The tree is constructed in a top-down fashion and each node of the tree holds a single data point. The root of the tree is the data point that is farthest from the origin. The two children of the root are the two data points that are closest to the origin. The two children of the root are then split into two halves and the process is repeated until all of the data points have been assigned to a node.

## Time Complexity

The time complexity of access, search, insertion, and deletion in a KD Tree is O(log n), where n is the number of data points. This is because the tree is constructed in a top-down fashion and the data points are assigned to nodes in a recursive manner. Thus, the time complexity is proportional to the length of the tree, which is proportional to the logarithm of the number of data points.

The average time complexity for search, insertion, and deletion is O(log n). This is because the data points are organized in a way that allows for efficient searching, insertion, and deletion. The worst-case time complexity is O(n), which occurs when all of the data points are located at the same level of the tree.

## Space Complexity

The space complexity of a KD Tree is O(n), where n is the number of data points. This is because each node of the tree holds a single data point. Thus, the space complexity is proportional to the number of data points.

## Implementing a KD Tree in C++

Now that we’ve discussed the basics of KD Trees, let’s take a look at how to implement one in C++. We’ll start by defining a Node class that will store the data points in the tree. The Node class will have two members: a data point and two pointers to its children.

class Node {
public:
int data;
Node *left;
Node *right;
};

Next, we’ll define a KDTree class that will be used to construct and manage the tree. The KDTree class will have two members: a root pointer and a function to construct the tree.

class KDTree {
private:
Node *root;

public:
KDTree();
void buildTree(vector<int> &points);
};

The buildTree() function will take in an array of data points and construct the tree. The function will take the data points and recursively split the space into two halves using a hyperplane. The hyperplane is determined by the position of the data points in the space.

void KDTree::buildTree(vector<int> &points) {
if (points.size() == 0) {
root = nullptr;
return;
}

root = new Node();
root->data = points[0];

// Split the points into two halves
vector<int> leftPoints, rightPoints;
for (int i = 1; i < points.size(); i++) {
if (points[i] < root->data) {
leftPoints.push_back(points[i]);
} else {
rightPoints.push_back(points[i]);
}
}

// Recursively build the left and right subtrees
buildTree(leftPoints, root->left);
buildTree(rightPoints, root->right);
}

Now that we have the KDTree class defined, we can use it to construct a KD Tree in C++. The following code example shows how to create a 3D KD Tree with 10 data points.

int main() {
vector<int> points = {3, 7, 4, 9, 5, 2, 8, 1, 6, 0};
KDTree tree;
tree.buildTree(points);

return 0;
}

## Conclusion

KD Trees are a powerful data structure that can be used to organize data points in a k-dimensional space. The time complexity of accessing, searching, inserting, and deleting data points in a KD Tree is O(log n). The space complexity is O(n). In this article, we’ve explored how to implement a KD Tree in C++.

## Exercises

#### Given an array of integers, write a function to construct a KD Tree.

#include <vector>

class Node {
public:
int data;
Node *left;
Node *right;
};

class KDTree {
private:
Node *root;

public:
KDTree();
void buildTree(vector<int> &points);
};

void KDTree::buildTree(vector<int> &points) {
if (points.size() == 0) {
root = nullptr;
return;
}

root = new Node();
root->data = points[0];

// Split the points into two halves
vector<int> leftPoints, rightPoints;
for (int i = 1; i < points.size(); i++) {
if (points[i] < root->data) {
leftPoints.push_back(points[i]);
} else {
rightPoints.push_back(points[i]);
}
}

// Recursively build the left and right subtrees
buildTree(leftPoints, root->left);
buildTree(rightPoints, root->right);
}

#### Given a KD Tree and a target data point, write a function to search for the data point in the tree.

#include <vector>

bool searchTree(Node *root, int target) {
if (root == nullptr) {
return false;
}

if (root->data == target) {
return true;
}

if (target < root->data) {
return searchTree(root->left, target);
} else {
return searchTree(root->right, target);
}
}

#### Given a KD Tree and a data point, write a function to insert the data point into the tree.

#include <vector>

void insertNode(Node *root, int data) {
if (root == nullptr) {
root = new Node();
root->data = data;
return;
}

if (data < root->data) {
insertNode(root->left, data);
} else {
insertNode(root->right, data);
}
}

#### Given a KD Tree and a data point, write a function to delete the data point from the tree.

#include <vector>

void deleteNode(Node *root, int data) {
if (root == nullptr) {
return;
}

if (root->data == data) {
// Code to delete the node
}

if (data < root->data) {
deleteNode(root->left, data);
} else {
deleteNode(root->right, data);
}
}

#### Given a KD Tree, write a function to traverse the tree in pre-order.

#include <vector>

void preOrderTraversal(Node *root) {
if (root == nullptr) {
return;
}

cout << root->data << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}