Heya!

I've got a kd tree which stores the x,y value as struct in every node.

The issue I have is the following: in addition to my regular nodes, my tree creates a bunch of nodes with 0,0!

The only thing I can think of being responsible is my struct constructor:

Code:

struct Coordinate{
double x,y;
Coordinate() : x(0), y(0) {}
Coordinate(double paramx, double paramy) : x(paramx), y(paramy) {}
};
struct TreeNode{
Coordinate coord;
struct TreeNode* left;
struct TreeNode* right;
};

The thing is, I don't need the Coordinate() constructor, only added it to get rid of error message. I'm only using Coordinate(double x, double y).

Anyways, I add a node like this in my Tree class:

Code:

TreeNode *node = new TreeNode();
node->coord.x = incCoords[0].x;
node->coord.y = incCoords[0].y;
return node;

The incCoords[0].x and incCoords[0].y value is 100% not 0,0 at this point.

I will post the rest of my method in case the issue happens to be something else^^

Code:

TreeNode* Tree::buildTree(int depth, std::vector<Coordinate>incCoords){
std::vector<Coordinate>coordSet1;
std::vector<Coordinate>coordSet2;
coordSet1.clear();
coordSet2.clear();
if (incCoords.size() == 1)
{
TreeNode *node = new TreeNode();
node->coord.x = incCoords[0].x;
node->coord.y = incCoords[0].y;
std::cout << "New root created, coords: " << incCoords[0].x << " x " << incCoords[0].y << std::endl;
return node;
} else if (depth % 2 == 0) {
std::sort(incCoords.begin(), incCoords.end(), sortByX);
} else if (depth % 2 == 1) {
std::sort(incCoords.begin(), incCoords.end(), sortByY);
}
for (unsigned int i=0; i<incCoords.size()/2; i++)
{
coordSet1.push_back(incCoords[i]);
}
for (unsigned int i=incCoords.size()/2; i<incCoords.size(); i++)
{
coordSet2.push_back(incCoords[i]);
}
TreeNode *node = new TreeNode();
node->left = buildTree(depth++, coordSet1);
node->right = buildTree(depth++, coordSet2);
return node;
}

Any suggestion for a workaround is welcome, thx in advance =)