Is kd-tree always balanced?

后端 未结 3 1255
感动是毒
感动是毒 2021-02-14 08:59

I have used kd-tree algoritham and make tree.

But i found that tree is not balanced so my question is if we used kd-tree algoritham then that tree is always balanced if

3条回答
  •  我寻月下人不归
    2021-02-14 09:26

    In order to make your kd-tree balanced use median value.
    (14,31), (15,32), (17,42), (16,44), (18,52), (16,62)
    In the root choose median of x-cordinates [14,15,16,16,17,18] which is 16,
    So all the elements less than 16 goes to left part of the tree and
    greater than or equal to goes to right side of tree.
    as of now,
    left part tree consists of [14,31],[15,32] ,now for y-axis find the median for [31,32] so that the tree is balanced

提交回复
热议问题