完成这些准备工作之后,KD树的建设正式开始。为了简单起见,子树的每一层都除以
x和
y顺序和周期性。然后,第一个分区是通过处理的元素
x索引数组依次从上到下。元素2、4和3小于中位数,所以他们属于左子树,应该留在的上部域被当前子树(即占领。BN数组中,相应的值应该是0 0)元素是中值,所以它应该位于当前子树(即中间。,BN[0] should be 3). Moreover, as this point will no longer belong to any subtree after being an intermediate node of the KD tree, SS[3] should be set to 0. Elements 5, 6, and 1 are greater than the median, so they belong to the right subtree and should be left in the lower part of the domain occupied by the current subtree (i.e., their corresponding value in the BN array should be 4). In the process of traversing the subtree starting from 0, the value of CUR[0] increases continuously, which is used to determine whether the current element belongs to the left subtree or the right subtree. After all the elements in the current subtree have been visited, CUR[0] is reset to 0. At the same time, the number of elements in the left and right subtrees will also be reset; that is, SS[0] and SS[4] will be set to 3. After the first partition, the values of these three arrays are shown in Figure
1在“后第一次分裂。”可以看出,坏蛋数组是一个辅助数组,它只影响当前的分区。当前分区完成后,它将返回0。因此,其状态将不再显示在随后的分区的过程。第二个分区将根据完成
y索引数组。的第一个元素
y索引数组是0,(即当前元素的开始位置。BN[0])是3。党卫军[3]是零,这意味着当前元素不需要处理。第二个元素是2,2(即元素的起始位置。,BN[2]) is 0, and the number of elements in the current subtree is 3. Therefore, element 2 belongs to the left subtree of the current subtree, and BN[2] should be 0. The third element is 6, starting at 4 and belonging to the left subtree of the current subtree, so BN[6] is 4. The fourth element is 1, and the starting position is 4, which should be the median of the current subtree. Therefore, BN[4] is set to 5, and SS[5] is set to 0. The remaining elements can be processed similarly. The results are shown in Figure
1在“后第二次分裂。“当所有的元素都选了KD树中的一个节点,党卫军数组中的所有元素为零,如图所示在“后第三分裂”图
1。然后,最后的索引数组(即。,F array in the right side of Figure
1)用于构造KD树可以很容易地从BN获得数组(这可以通过将完成
我在BN的位置
我F的数组)。它可以看到从上面的过程提出了构建KD树时,新方法并不管理准备一个完整的为每个分区索引数组,但只有保持下次的分区数据集。这个想法使新方法不再需要每次更新K索引数组。因此,它降低了时间成本。
建立一个KD树的详细描述基于新方法如图
2。第一步是预分类和初始化,在那之后,选择分裂维度和处理BN, SS,反复和坏蛋数组,直到党卫军数组中的每个元素为零。随后,最后根据BN数组索引数组构造F(事实上,F的结构数组也可以完成在分区没有BN数组)的帮助。最后,KD树正是构建递归地根据F数组。处理的主要流程BN, SS,坏蛋数组大框所示。D索引数组中的元素(即。,the splitting dimension) will be visited in turn from the beginning to the end. Suppose that the current element is tmpi. If the current element has already been selected (i.e., tmpsize is 0), it no longer needs to be processed. Otherwise, it is whether it belongs to the left subtree or the right subtree or is the median will be judged according to CUR[tmpBN]. If it belongs to the left subtree or the right subtree, BN[tmpi] will point to the starting position of the corresponding left or right subtree. Otherwise, BN[tmpi] will point to the middle of the segment occupied by the current subtree, and its corresponding value in the SS array will be set to zero. When all the elements in the current subtree have been processed, the corresponding elements in SS and CUR arrays will be reset as described in the foregoing subsection.