I have a recursive function which runs through a quadtree and generates for each node a key. But this only works when I save the variable myKey as myKey1. If I do not do that, the resulting keys are wrong. But why? That is not clear to me.
Here is the function:
void keygen(tree *tn, long unsigned int myKey)
{
long unsigned int myKey1 = myKey; // Why do I need this line?
for(int q=0; q<4; q++){
// 1) Check if child exists
if(tn->child[q] != NULL){
// Make key
myKey1 = (myKey<<DIM)|q;
// Save key
tn->child[q]->key = myKey1;
// Call again if leaf
if(tn -> child[q] -> isLeaf == 0){
keygen(tn->child[q], myKey1);
}
}
}
}
myKey1 = (myKey<<DIM)|q;? If you were originally using myKey on the left and right, you'd be accumulating shifts on every iteration of the loop.