Skip to content

Commit

Permalink
Update kd_tree.cr
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 12, 2024
1 parent 70a59eb commit d2fe835
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/kd_tree.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ module Kd
points.sort_by!(&.[axis]) # Sort points by the current axis
median = points.size // 2 # Find the median index

right_subtree = build_tree(points[median + 1..], depth + 1)
left_subtree = build_tree(points[...median], depth + 1)

# Create a new Node with the median point as pivot, and recursively build the left and right subtrees.
Node(T).new(
points[median],
axis,
build_tree(points[0...median], depth + 1),
build_tree(points[median + 1..], depth + 1)
left_subtree,
right_subtree,
)
end

Expand Down

0 comments on commit d2fe835

Please sign in to comment.