Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

How can data be loaded to a bst?

This topic is 2699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is the file that gets saved from the bst preorder
Are you a mammal?
Are you bigger than a cat?
Kangaroo
Do you purr?
Cat
Mouse
Do you live underwater?
Trout
Robin

this is what it needs to load back up like
[attachment=2142:bst.jpg]


How can I get it to load from top to bottom left to right

I am using c++

Share this post


Link to post
Share on other sites
Advertisement
Just perform a pre-order traversal and save each node to file. e.g.

[source]

// saving

void Node::Save(std::ofstream& ofs)
{
ofs << this->isLeaf();
ofs << this->data;
if(!this->isLeaf()) {
leftNode->Save(ofs);
rightNode->Save(ofs);
}
}

// loading
(static)
Node* Node::load(std::ifstream& ifs)
{
Node* n = new Node();
bool isLeaf;
ifs >> isLeaf;
ifs >> n->data;
if(isLeaf)
return n;
n->leftNode = load(ifs);
n->rightNode = load(ifs);
}

[/source]

Edit: I noticed you got the order wrong in your list, pre-order is when you visit the root, left sub-tree then right subtree. it should be:

Are you a mammal?
Are you bigger than a cat?
Do you purr?
Cat
Mouse
Kangaroo
Do you live underwater?
Trout
Robin

Share this post


Link to post
Share on other sites

[source]

// loading
(static)
Node* Node::load(std::ifstream& ifs)
{
Node* n = new Node();
bool isLeaf;
ifs >> isLeaf;
ifs >> n->data;
if(isLeaf)
return n;
n->leftNode = load(ifs);
n->rightNode = load(ifs);
}

[/source][c++]


how to I load the entire file? It keeps cutting out early.



void beginning_tree(binary_tree_node<string>*& current_ptr,ifstream &fin)
// Library facilities used: bintree.h, string
{
string question;
bool isLeaf = false;
binary_tree_node<string> *child_ptr = current_ptr;
if(fin.is_open())
{ // if began
while(fin.good())
{
child_ptr = new binary_tree_node<string>;

fin >> isLeaf;
fin >> question;

if(isLeaf)
child_ptr->set_data(question);

beginning_tree(child_ptr->left(),fin);
beginning_tree(child_ptr->right(),fin);

}

print(taxonomy_root_ptr,0);
}

fin.close();
}

Share this post


Link to post
Share on other sites

It keeps cutting out early.
[/quote]
Not enough information. Describe where it "cuts out" - the very start? Somewhere in the middle? Did you re-write your saving code or file format to support the "isLeaf" boolean?

We need more information to help you.
[hr]
You are closing the file in a recursive function - don't do this. You don't need to explicitly close std::fstreams unless you plan on re-using them to open another file (hint: don't do this). std::fstreams close the file in their destructor. Checking and managing the file is open should be done in the calling function. Ideally, the code building the tree wouldn't even need to load from a file, it could load from std::cin or a stringstream or other data source.

Is "taxonomy_root_ptr" the one passed in to the first call to "beginning_tree"?

Don't create the "child_ptr" temporary, the changes you make to it are lost because it is not a reference. Write your code to return the new node instead (like staticVoid suggests), this is easier to understand.

Something like this would be better:

typedef binary_tree_node<string> TaxonomyTree;

TaxonomyTree *load_tree(const string &filename)
{
std::ifstream in(filename.c_str());
if(!in.is_open())
{
// throw exception?
return 0;
}
return beginning_tree(root, in);
}

// TODO: detect and handle eof() correctly.
TaxonomyTree *beginning_tree(istream &in)
{
string question;
bool isLeaf;

// TODO: use std::getline()
if(in >> question >> isLeaf)
{
if(isLeaf)
{
return new TaxonomyTree(question);
}
else
{
TaxonomyTree *left = beginning_tree(in);
TaxonomyTree *right = beginning_tree(in);
if(!(left && right))
{
// Error, some nodes failed to read
// N.B. Cleanup is required!
return 0;
}
return = new TaxonomyTree(question, left, right);
}
return current_ptr;
}
else
{
// Failed to read data?
return 0;
}

print(taxonomy_root_ptr,0);
}

Also note that none of this code will actually work because you questions contain whitespace. You'll need to re-write the code using std::getline() or something. It might be easier if you output the "isLeaf" boolean at the start of each line. Also note that the code above detects malformed data, but doesn't correctly detect the end-of-file condition.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!