Thread: saving a binary tree??

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    saving a binary tree??

    Hiya!
    I've been making a binary tree over the last couple of days, and i've got stuck on saving it! I need to save it and then read it back into memory when i load the program up.

    Obviously doing a pre-order traversal, but it wont work, when using recursion and I/O do you have to change things?

    I've got a feeling i'm missing something really simple! I had a search and coulddnt find anyone with a similar propblem.
    Cheers for any advice!

    snipet of my code:

    void saveIndexINT()
    {
    saveIndex(root);
    }


    void saveIndex(node *currentNode)
    {

    ofstream indexOut;

    indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space



    if(currentNode != NULL)
    {
    indexOut << currentNode->name << "*" << currentNode->recNo << endl;
    saveIndex(currentNode->left);
    saveIndex(currentNode->right);
    }




    indexOut.close();


    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are missing something simple. Code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    ahh, square brackets! thanks for the tip!
    Code:
     void saveIndexINT()
     {
     saveIndex(root);
     }
               
               
     void saveIndex(node *currentNode)
     {
     
         ofstream indexOut;
     
         indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space
     
         
         
     if(currentNode != NULL)
         {
         indexOut << currentNode->name << "*" <<    currentNode->recNo << endl;
         saveIndex(currentNode->left);
         saveIndex(currentNode->right);
         }
         
         
         
         
         indexOut.close();    
             
         
     }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space

    The above line opens indexData.txt each time you call saveIndex(). The problem is each time it opens the file it overwrites the contents. Therefore, I'd open indexData just once back in main() or somewhere before saveIndex() and pass the stream associated with the file to saveIndex() with the node you want saved. That way you keep adding to the file with each call to saveIndex instead of overwriting it each time you call saveIndex.
    You're only born perfect.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like this
    http://cboard.cprogramming.com/showt...888#post433888
    Only following left and right pointers at each node.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    cheers! I understand what you mean, thats what i was thinking myself

    so.........

    Code:
    void saveIndexINT()
    {
    ofstream indexOut;
    indexOut.open("indexData.txt", ios::out);
    saveIndex(root, "indexData.txt");
    indexOut.close();
    }
    		  
    		  
    void saveIndex(node *currentNode, char *filename)
    {
    
    	
    
    	
    //indexOut << numberOfRecords << endl;
    	
    	if(currentNode != NULL)
    	{
     
    	indexOut << currentNode->name << "*" << currentNode->recNo;
    	saveIndex(currentNode->left, "indexData.txt");
    	saveIndex(currentNode->right, "indexData.txt");
    	}
    	
    	
    	
    	
    
     
    		
    	
    }

    I create the "indexData.txt" in the procedure before hand, I then pass it to my current procedure.

    I found the code you showed me abit confusing, fwrite etc, not sure how to use those, can it be done using the read and write functions?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    It works!

    Got it working! cheers.
    As a global i declared:

    ofstream indexOut("indexData.txt", ios:ut);

    then just used it in the procedures. So simple.

    fwrite and co, thats from C isnt it?

    Only just really started with C++ (as you can see) and programing in general. Its fun when you get something working! Just as frustrating when it dosent work tho!

    Cheers again for your help, much appreciated!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > can it be done using the read and write functions?
    Yes, C++ fstreams have read() and write() methods
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Note: the stream doesn't have to be global.
    Code:
    void saveIndex(ofstream & fout, node * currentNode);
     
    int main()
    {
      ofstream fout("indexData.txt");
      //create tree including a root node
      //send root node of tree to saveIndex() to start saving to file
      saveIndex(fout, root);
     
      //etc.
    }
     
    void saveIndex(ofstream & fout, node * currentNode)
    { 
      if(currentNode != NULL)
      {
    	  fout << currentNode->name << "*" << currentNode->recNo;
    	  saveIndex(fout, currentNode->left);
    	  saveIndex(fout, currentNode->right);
      }
    }
    Alternatively, if you wanted to open and close the stream with each call to saveIndex() you could open the stream in ios::app mode instead of the default ios::trunc mode. To me opening and closing stream with each call to saveIndex() seemed overkill is all. I prefer to send the stream to the function by reference as opposed to using a global variable to hold the stream.
    Last edited by elad; 01-27-2005 at 04:32 PM.
    You're only born perfect.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    ahh!

    ah!! Thats what i actually wanted to do, we've been told: avoid global variables, encapsulation etc etc. I just didnt know how to do it!

    Cheers for that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM