(ツリー上に) 再帰関数があり、再帰を使用せずにツリーを暗黙的なデータ構造 (配列) として表現する必要があります。
関数は次のとおりです。
kdnode* kdSearchNN(kdnode* here, punto point, kdnode* best=NULL, int depth=0)
{
if(here == NULL)
return best;
if(best == NULL)
best = here;
if(distance(here, point) < distance(best, point))
best = here;
int axis = depth % 3;
kdnode* near_child = here->left;
kdnode* away_child = here->right;
if(point.xyz[axis] > here->xyz[axis])
{
near_child = here->right;
away_child = here->left;
}
best = kdSearchNN(near_child, point, best, depth + 1);
if(distance(here, point) < distance(best, point))
{
best = kdSearchNN(away_child, point, best, depth + 1);
}
return best;
}
このプロパティを使用して、ツリーを配列として表しています。
root: 0
left: index*2+1
right: index*2+2
これは私がやったことです:
punto* kdSearchNN_array(punto *tree_array, int here, punto point, punto* best=NULL, int depth=0, float dim=0)
{
if (here > dim) {
return best;
}
if(best == NULL)
best = &tree_array[here];
if(distance(&tree_array[here], point) < distance(best, point))
best = &tree_array[here];
int axis = depth % 3;
int near_child = (here*2)+1;
int away_child = (here*2)+2;
if(point.xyz[axis] > tree_array[here].xyz[axis])
{
near_child = (here*2)+2;
away_child = (here*2)+1;
}
best = kdSearchNN_array(tree_array, near_child, point, best, depth + 1, dim);
if(distance(&tree_array[here], point) < distance(best, point))
{
best = kdSearchNN_array(tree_array, away_child, point, best, depth + 1, dim);
}
return best;
}
最後のステップは再帰を取り除くことですが、方法が見つかりません。ヒントはありますか? ありがとう