c ++ k-d树实现崩溃

时间:2013-07-23 17:34:11

标签: c++ tree kdtree

我试图实现一个简单的kd树,但我在内存管理方面做错了,或者我试图访问那些不存在的东西(ei程序编译,但在运行时崩溃)。以下是我认为问题来自的部分:

void tree::addpoint (node* leaf, const my_vec &pointdata, int ref)
{
  my_vec center(2);
  int indicator;

  indicator = findquad (leaf, pointdata);

  if ( leaf->child[indicator] == NULL )
  {
    if ( (indicator%2) > 0)
        center[0] = leaf->center[0] + leaf->boxsize/4;
    else
        center[0] = leaf->center[0] - leaf->boxsize/4;
    if ( indicator > 1 )
        center[1] = leaf->center[1] + leaf->boxsize/4;
    else
        center[1] = leaf->center[1] - leaf->boxsize/4;
    leaf->child[indicator] = new node;
    leaf->child[indicator]->point = pointdata;
    leaf->child[indicator]->ref = ref;
    leaf->child[indicator]->center = center;
    leaf->child[indicator]->boxsize = leaf->boxsize/2;
    leaf->child[indicator]->IsReal = true;
    leaf->child[indicator]->child.resize(4);
    for (int i=1; i<4; i++)
        leaf->child[indicator]->child[i] = NULL;
  }
  else
    addpoint (leaf->child[indicator], pointdata, ref);

  if (leaf->IsReal)
  {
    leaf->IsReal = false;
    addpoint (leaf, pointdata, ref);
  }   
}

int tree::findquad(node *leaf, const my_vec& pointdata)
// For a given node 'node', find the proper octraturequadrature for a point located at   'pointdata'.
{
  bool north, east;
  int indicator = 0, end;

  end = pointdata.size() - 1;

  east = pointdata[0] >= leaf->center[0];
  north = pointdata[end] >= leaf->center[1];

  if (east)
    indicator = indicator + 2;
  if (north)
    indicator = indicator + 1;
  return(indicator);
}

0 个答案:

没有答案