Create buildKDTree.m
This commit is contained in:
parent
69a30da0a6
commit
2ccae86f34
1 changed files with 23 additions and 0 deletions
23
Task/K-d-tree/Matlab/buildKDTree.m
Normal file
23
Task/K-d-tree/Matlab/buildKDTree.m
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function node = buildKDTree(points, depth)
|
||||
if nargin < 2
|
||||
depth = 0; % default depth to start building tree, otherwise specified in the function call
|
||||
end
|
||||
if isempty(points)
|
||||
node = [];
|
||||
return;
|
||||
end
|
||||
|
||||
k = size(points, 2); % Dimensionality of the points
|
||||
axis = mod(depth, k) + 1;
|
||||
|
||||
% Sort point list and choose median as pivot element
|
||||
|
||||
sortedPoints = sortrows(points, axis);
|
||||
medianIdx = floor(size(sortedPoints, 1) / 2) + 1;
|
||||
|
||||
% Create node and construct subtrees
|
||||
|
||||
node = kdNode(sortedPoints(medianIdx, :), ...
|
||||
buildKDTree(sortedPoints(1 : medianIdx - 1, :), depth + 1), ...
|
||||
buildKDTree(sortedPoints(medianIdx + 1 : end, :), depth + 1));
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue