Skip to content

Implemented KD Tree Data Structure #11532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0d6985c
Implemented KD-Tree Data Structure
Ramy-Badr-Ahmed Aug 28, 2024
6665d23
Implemented KD-Tree Data Structure. updated DIRECTORY.md.
Ramy-Badr-Ahmed Aug 28, 2024
6b3d47e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
4203cda
Create __init__.py
Ramy-Badr-Ahmed Aug 28, 2024
3222bd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
a41ae5b
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed Aug 28, 2024
1668d73
Replaced legacy `np.random.rand` call with `np.random.Generator` in k…
Ramy-Badr-Ahmed Aug 28, 2024
81d6917
added typehints and docstrings
Ramy-Badr-Ahmed Aug 28, 2024
6cddcbd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
8b238d1
docstring for search()
Ramy-Badr-Ahmed Aug 28, 2024
cd1dd9f
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed Aug 28, 2024
ead2838
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
543584c
Added tests. Updated docstrings/typehints
Ramy-Badr-Ahmed Aug 28, 2024
ad31f83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
1322921
updated tests and used | for type annotations
Ramy-Badr-Ahmed Aug 28, 2024
4608a9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 28, 2024
7c1aa7e
E501 for build_kdtree.py, hypercube_points.py, nearest_neighbour_sear…
Ramy-Badr-Ahmed Aug 29, 2024
ba24e75
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed Aug 29, 2024
05975a3
I001 for example_usage.py and test_kdtree.py
Ramy-Badr-Ahmed Aug 29, 2024
31782d1
Update data_structures/kd_tree/build_kdtree.py
Ramy-Badr-Ahmed Sep 3, 2024
6a9b3e1
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed Sep 3, 2024
2fd24d4
Update data_structures/kd_tree/example/hypercube_points.py
Ramy-Badr-Ahmed Sep 3, 2024
2cf9d92
Added new test cases requested in Review. Refactored the test_build_k…
Ramy-Badr-Ahmed Sep 3, 2024
a3803ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
f1f5862
Considered ruff errors
Ramy-Badr-Ahmed Sep 3, 2024
ec6559d
Merge remote-tracking branch 'origin/feature/kd-tree-implementation' …
Ramy-Badr-Ahmed Sep 3, 2024
5c07a1a
Considered ruff errors
Ramy-Badr-Ahmed Sep 3, 2024
3c09ac1
Apply suggestions from code review
cclauss Sep 3, 2024
bab43e7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
a10ff15
Update kd_node.py
cclauss Sep 3, 2024
d77a285
imported annotations from __future__
Ramy-Badr-Ahmed Sep 3, 2024
0426806
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@
* Trie
* [Radix Tree](data_structures/trie/radix_tree.py)
* [Trie](data_structures/trie/trie.py)
* KD Tree
* [KD Tree Node](data_structures/kd_tree/kd_node.py)
* [Build KD Tree](data_structures/kd_tree/build_kdtree.py)
* [Nearest Neighbour Search](data_structures/kd_tree/nearest_neighbour_search.py)
* [Hypercibe Points](data_structures/kd_tree/example/hypercube_points.py)
* [Example Usage](data_structures/kd_tree/example/example_usage.py)

## Digital Image Processing
* [Change Brightness](digital_image_processing/change_brightness.py)
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions data_structures/kd_tree/build_kdtree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from .kd_node import KDNode


def build_kdtree(points, depth=0):
if not points:
return None

k = len(points[0]) # dimensionality of the points
axis = depth % k

# Sort point list and choose median as pivot element
points.sort(key=lambda x: x[axis])
median_idx = len(points) // 2

# Create node and construct subtrees
return KDNode(
point=points[median_idx],
left=build_kdtree(points[:median_idx], depth + 1),
right=build_kdtree(points[median_idx + 1 :], depth + 1),
)
Empty file.
25 changes: 25 additions & 0 deletions data_structures/kd_tree/example/example_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import numpy as np

from hypercube_points import hypercube_points
from data_structures.kd_tree.build_kdtree import build_kdtree
from data_structures.kd_tree.nearest_neighbour_search import nearest_neighbour_search


num_points = 5000

Check failure on line 8 in data_structures/kd_tree/example/example_usage.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

data_structures/kd_tree/example/example_usage.py:1:1: I001 Import block is un-sorted or un-formatted
cube_size = 10
num_dimensions = 10

points = hypercube_points(num_points, cube_size, num_dimensions)
hypercube_kdtree = build_kdtree(points.tolist())

rng = np.random.default_rng()
query_point = rng.random(num_dimensions).tolist()

nearest_point, nearest_dist, nodes_visited = nearest_neighbour_search(
hypercube_kdtree, query_point
)

print(f"Query point: {query_point}")
print(f"Nearest point: {nearest_point}")
print(f"Distance: {nearest_dist:.4f}")
print(f"Nodes visited: {nodes_visited}")
6 changes: 6 additions & 0 deletions data_structures/kd_tree/example/hypercube_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import numpy as np


def hypercube_points(num_points, hypercube_size, num_dimensions):
rng = np.random.default_rng()
return hypercube_size * rng.random((num_points, num_dimensions))
5 changes: 5 additions & 0 deletions data_structures/kd_tree/kd_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class KDNode:
def __init__(self, point, left=None, right=None):
self.point = point
self.left = left
self.right = right
41 changes: 41 additions & 0 deletions data_structures/kd_tree/nearest_neighbour_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def nearest_neighbour_search(root, query_point):
nearest_point = None
nearest_dist = float("inf")
nodes_visited = 0

def search(node, depth=0):
nonlocal nearest_point, nearest_dist, nodes_visited
if node is None:
return

nodes_visited += 1

# Calculate the current distance (squared distance)
current_point = node.point
current_dist = sum((qp - cp) ** 2 for qp, cp in zip(query_point, current_point))

# Update nearest point if the current node is closer
if nearest_point is None or current_dist < nearest_dist:
nearest_point = current_point
nearest_dist = current_dist

# Determine which subtree to search first (based on axis and query point)
k = len(query_point) # dimensionality of points
axis = depth % k

if query_point[axis] <= current_point[axis]:
nearer_subtree = node.left
further_subtree = node.right
else:
nearer_subtree = node.right
further_subtree = node.left

# Search the nearer subtree first
search(nearer_subtree, depth + 1)

# If the further subtree has a closer point
if (query_point[axis] - current_point[axis]) ** 2 < nearest_dist:
search(further_subtree, depth + 1)

search(root, 0)
return nearest_point, nearest_dist, nodes_visited
Loading