Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balancing KD Tree

So when balancing a KD tree you're supposed to find the median and then put all the elements that are less on the left subtree and those greater on the right. But what happens if you have multiple elements with the same value as the median? Do they go in the left subtree, the right or do you discard them?

I ask because I've tried doing multiple things and it affects the results of my nearest neighbor search algorithm and there are some cases where all the elements for a given section of the tree will all have the exact same value and so I don't know how to split them up in that case.

like image 813
user1855952 Avatar asked Apr 19 '26 22:04

user1855952


1 Answers

It does not really matter where you put them. Preferably, keep your tree balanced. So place as many on the left as needed to keep the optimal balance!

If your current search radius touches the median, you will have to check the other part, that's all you need to handle tied objects on the other side. This is usually cheaper than some complex handling of attaching multiple elements anywhere.

like image 100
Has QUIT--Anony-Mousse Avatar answered Apr 21 '26 13:04

Has QUIT--Anony-Mousse