Difference between revisions of "Thread:User talk:Rednaxela/kD-Tree/Unit tests for KdTree/reply (7)"
(Reply to Unit tests for KdTree) |
(additional notes) |
||
Line 6: | Line 6: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
− | points[ | + | points[i] = points[size-1]; |
− | data[ | + | data[i] = data[size-1]; |
points[size-1] = null; | points[size-1] = null; | ||
data[size-1] = null; | data[size-1] = null; | ||
size--; | size--; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | (Oh, and when removing elements, be careful with how you're looping over them at the same time. You need to decrement "i" when the remove occurs, because what points[i] refers to changes when you remove the element) |
Latest revision as of 05:23, 29 May 2012
Well, to be clear the bounds affect the correctness if they are *too small*, but the bounds being too big (as is the case after removing) has no impact.
Really, you don't have to "merge siblings" when all points are removed from a node even, and it could (for sake of tree balance) be worth merging sibling nodes before they're completely empty (i.e. if both nodes had half their points removed)
With regards to the removing from the array, actually you can do it in O(1) time due to the fact that 1) unused array elements are expected, and 2) the array of points in a leaf node are unordered. You just need to overwrite the removed element with the last element, and then remove the last element:
points[i] = points[size-1];
data[i] = data[size-1];
points[size-1] = null;
data[size-1] = null;
size--;
(Oh, and when removing elements, be careful with how you're looping over them at the same time. You need to decrement "i" when the remove occurs, because what points[i] refers to changes when you remove the element)