Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Quadtree to file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Diakon_Tristan   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 August 2001 - 11:07 PM

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.

Sponsor:

#2 Sandman   Moderators   -  Reputation: 2187

Like
Likes
Like

Posted 09 August 2001 - 11:57 PM

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..

#3 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 August 2001 - 05:11 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS