我正在研究四叉树的复制构造函数。这是我到目前为止所拥有的:
//Copy Constructor
Quadtree :: Quadtree(const Quadtree & other)
{
root = copy(other.root);
resolution = other.resolution;
}
//Copy Constructor helper function
Quadtree::QuadtreeNode *Quadtree :: copy (const QuadtreeNode* newRoot)
{
if (newRoot != NULL)
{
QuadtreeNode *node = new QuadtreeNode(newRoot->element);
node->nwChild = copy(newRoot->nwChild);
node->neChild = copy(newRoot->neChild);
node->swChild = copy(newRoot->swChild);
node->seChild = copy(newRoot->seChild);
return node;
}
else
return NULL;
}
我不确定我哪里出错了,但我收到内存泄漏,Valgrind 指出我有未初始化的值。请帮忙?
附加的是我的 buildTree 函数 - 我实际创建树的地方。我可能在这里做错了什么?
void Quadtree :: buildTree (PNG const & source, int theResolution)
{
buildTreeHelp (root, 0, 0, theResolution, source);
}
void Quadtree :: buildTreeHelp (QuadtreeNode * & newRoot, int xCoord, int yCoord, int d, PNG const & image)
{
if (d == 1)
{
RGBAPixel pixel = *image(xCoord, yCoord);
newRoot = new QuadtreeNode(pixel);
return;
}
newRoot = new QuadtreeNode ();
newRoot = NULL;
buildTreeHelp(newRoot->nwChild, xCoord, yCoord, d/2, image);
buildTreeHelp(newRoot->neChild, xCoord + d/2, yCoord, d/2, image);
buildTreeHelp(newRoot->swChild, d/2, yCoord + d/2, d/2, image);
buildTreeHelp(newRoot->seChild, d/2 + xCoord, d/2 + yCoord, d/2, image);
}