Archived

This topic is now archived and is closed to further replies.

Diakon_Tristan

Quadtree to file

Recommended Posts

As the topic says I would like to save a quadtree to a file. I have no idea how to do it. I hope you can help. Here is my Quadtree: typedef class QUADTREE { public: QUADTREE(void); //data QUADTREE *node1, *node2, *node3, *node4; }T_QUADTREE; typedef T_QUADTREE *QUADTREE_POINTER; If a node is a leaf of the tree it is marked. I would like to use fopen() etc.

Share this post


Link to post
Share on other sites
Just add methods called SaveTree() and LoadTree() to the node class, and have them each take a FILE* as a parameter.

  
SaveTree(FILE* f)
{
// print all items which arent child pointers to the file

node1->SaveTree(f);
node2->SaveTree(f);
node3->SaveTree(f);
node4->SaveTree(f);
}

LoadTree(FILE* f)
{
// load data from file here

if(!leaf)
{
node1 = new QUADTREE;
node1->LoadTree(f);
node2 = new QUADTREE;
node2->LoadTree(f);
node3 = new QUADTREE;
node3->LoadTree(f);
node4 = new QUADTREE;
node4->LoadTree(f);
}
}



I havent tested this, but I see no reason why it shouldnt work..

Share this post


Link to post
Share on other sites
Well, that example lacks base cases, so it won''t actually DO anything, but it''s the right idea.

Basically, when writing out a tree structure, a preorder listing is the way to go, as long as there is an unambiguous ordering between the children. Given that they are named node1 through node4, that pretty much gives you the ordering you''re looking for. So slightly more useful code would look kind of like this:

  
SaveTree(FILE* f)
{
if (isLeaf)
{
fprintf(f, "L");
fprintf(f, "Put the leaf data here, terminated with a newline");
}
else // is an internal node

{
fprintf(f, "I");
fprintf(f, "Put the internal node data (other than the children) here, terminated with a newline");
node1->SaveTree(f);
node2->SaveTree(f);
node3->SaveTree(f);
node4->SaveTree(f);
}
} // SaveTree(FILE *)



LoadTree(FILE *f)
{
char buff[1024];
fgets(buff, 1024, f);
if (*buff == ''L'')
{
// parse the Leaf data out of the remainder of the

// string stored in buff

}
else // internal node

{
// parse the internal node data out of the remainder

// of the string stored in buff, then:

node1->LoadTree(f);
node2->LoadTree(f);
node3->LoadTree(f);
node4->LoadTree(f);
}
}


Note that just using newlines to separate records is a bit fragile, and parsing out of string format can be a bit hard, although sscanf should do fine with it. Also, 1024 might not be enough characters for a line, which would really screw that function up. But it shows the basic idea of how to use a preorder recursive listing to save and load a tree structure.

The big advantage to using a preorder listing is that it is complete on its own. You don''t need to store any information about the number of nodes in the tree, because the recursive loading is always filling in subtrees from the top down, and when all of the subtrees are full, the load is done. A postorder listing, on the other hand, fills in the tree from the leaves up to the root, which means that it requires knowing how large the tree is, so that it knows when the tree it''s built is done.

Share this post


Link to post
Share on other sites