Kd-Tree问题

时间:2011-06-06 12:15:09

标签: algorithm data-structures kdtree

我正在尝试实施并理解KdTree,以下是我找到的链接。 http://ldots.org/kdtree/#buildingAkDTree 但我无法理解以下算法

tuple function build_kd_tree(int depth, set points):
    if points contains only one point:
        return that point as a leaf.

    if depth is even:
        Calculate the median x-value.
        Create a set of points (pointsLeft) that have x-values less than
            the median.
        Create a set of points (pointsRight) that have x-values greater
            than or equal to the median.
    else:
        Calculate the median y-value.
        Create a set of points (pointsLeft) that have y-values less than
            the median.
        Create a set of points (pointsRight) that have y-values greater
            than or equal to the median.

    treeLeft = build_kd_tree(depth + 1, pointsLeft)
    treeRight = build_kd_tree(depth + 1, pointsRight)

    return(median, treeLeft, treeRight)

我不明白是什么意思 Calculate the median x-value.

1 个答案:

答案 0 :(得分:0)

您的pointsxy个值。可以通过x排序,然后获取中间元素的x(针对奇数x)或平均值来获取median points个值两个中间points'x(偶数points)。

或者,使用快速selection algorithm,例如中位数中位数。