I have made a Quadtree which is initially a uniform level 2 quadtree.
struct qnode {
int level;
double xy[2];
struct qnode *child[4];
};
typedef struct qnode Node;
int main( int argc, char **argv ) {
Node *head;
// make the head node
head = makeNode( 0.0,0.0, 0 );
// make a tree
//full level 2 tree
makeChildren( head );
makeChildren( head->child[0] );
makeChildren( head->child[1] );
makeChildren( head->child[2] );
makeChildren( head->child[3] );
}
// make a node at given location (x,y) and level
Node *makeNode( double x, double y, int level ) {
int i;
Node *node = (Node *)malloc(sizeof(Node));
node->level = level;
node->xy[0] = x;
node->xy[1] = y;
for( i=0;i<4;++i )
node->child[i] = NULL;
return node;
}
//split a leaf nodes into 4 children
void makeChildren( Node *parent ) {
double x = parent->xy[0];
double y = parent->xy[1];
int level = parent->level;
double hChild = pow(2.0,-(level+1));
parent->child[0] = makeNode( x,y, level+1 );
parent->child[1] = makeNode( x+hChild,y, level+1 );
parent->child[2] = makeNode( x+hChild,y+hChild, level+1 );
parent->child[3] = makeNode( x,y+hChild, level+1 );
return;
}
How do I traverse each node and grow the tree uniformly at each node?
A recursive approach seems right. Something like this:
void growTree(Node * root) {
for(int i=0; i<4; i++) {
if(root->child[i] == NULL) root->child[i] = makeNode(0,0,root->level+1);
else growTree(root->child[i]);
}
}
I would also suggest redesigning makeNode
. Something like this:
Node *makeNode(double x, double y, Node * node) {
int level = node->level;
// copy old body of makeNode here
}
Also, change to this:
typedef struct Node {
int level;
double xy[2];
struct Node *child[4];
} Node;
There's no reason to have different names for struct Node and Node. If you want to hide the definition of struct Node, use this:
struct Node {
int level;
double xy[2];
struct Node *child[4];
};
typedef struct Node Node;