Thread: Noob Question about trees

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Noob Question about trees

    Hey,

    I have a small problem where i an not quite sure how to iterate through string data within a file and insert it into a tree list.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <algorithm>
    #include <fstream>
    
    #include "bintree.h"
    #include "binnode.h"
    
    using namespace std;
    
    void fillTree(bintree<string> &treeRoot);
    //void printTreeStats(const bintree<string> &treeRoot);
    
    int main(int argc, char *argv[])
    {
        
       const int MAXDATA = 1000; 
         bintree<string> treeRoot;
    
       /* if (argc != 2) {
          cout << "Syntax : family familyFile\n";
          return 0;
       }*/
     
       fillTree(treeRoot);
     //  printTreeStats(treeRoot);
       treeRoot.rebalance();
    //   printTreeStats(treeRoot);
    
       return 0;
    }
    
    void fillTree(bintree<string> &treeRoot)
    {
       ifstream fin;
       string c;
       
       fin.open("dict.txt");
       if (!fin) {
          cout << "Unable to read from  file " << "\n";
          exit(0);
       }
       
       while (!fin.eof()) {
             c = fin.get();
             treeRoot.insert(c);
          
       }
       fin.close();   
    }
    
    /*void print(const bintree<string> &treeRoot)
    {
       
    }*/
    this is the code i currently worked out but i am stuck at the point where i need to add all the string data ( 1000 words) into the tree list.

    Also just want to know can a tree have a lists within it ? because the program specification had something about lists and im not quite sure I understood it. Like lists which contain strings are nodes of a tree. (idk if that is even more confusin :P)

    Any help is welcome. Thank you.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is not the way to check if open failed on an ifstream:

    Code:
       fin.open("dict.txt");
       if (!fin) {
    "fin" will never be NULL, even if the file does not exist. It will be an ifstream object with the failbit set:

    ifstream :: open - C++ Reference

    Also, ifstream::get() does not work the way you are trying to use it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    There is nothing wrong with the check; look for the conversion operator for "some unknown type that is implicitly convertible to `bool'" in the member list of `std::ios'. (This will likely be the `void *' operator.)

    [Edit]
    Come to think of it, `std::ios' has operator `!', so that is what is in play here.
    [/Edit]

    Soma
    Last edited by phantomotap; 05-15-2011 at 07:16 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3

    lost

    ok you both lost me there lol.. The way we learnt is that how to open a file and check it. It worked previously and i guess we did not go into much depth with that.

    The problem im having is accessing the file and transferring all the data into the tree. I manage to transfer one word but thats about it afterwards it tells me that the data in already in the tree so im guessing that it only adds the first word and tries to add it again but fails. I might be wrong though. Still touching basics

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    There is nothing wrong with the check;
    My apologizes, I wasn't aware of that (it seems a very inappropriate and confusing use for ! tho since the object is still valid...but anyway).

    Looks like I'm also wrong about the use of get(), sigh. Time to find a new C++ reference.

    Which means this problem:

    Quote Originally Posted by theonealone View Post
    The problem im having is accessing the file and transferring all the data into the tree. I manage to transfer one word but thats about it afterwards it tells me that the data in already in the tree so im guessing that it only adds the first word
    Is not because of the fillTree() function -- it's in the code you haven't shown us.
    Last edited by MK27; 05-15-2011 at 07:37 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    3
    Thats all the code i have written so far. The rest is just normal stuff as binnode.h and bintree.h which i think are the c++ classes.

    Anyways i found the problem. I had to put,

    Code:
    while(fin >> c)
       tree.insert(c);
    That put all the data into the tree and now i just have to write the rest of it. Hopefully i will be able to or you shall see me again soon

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Theoritical question in trees structures
    By nomikos in forum C Programming
    Replies: 5
    Last Post: 12-20-2010, 11:00 AM
  2. Binary trees question
    By ozumsafa in forum C Programming
    Replies: 7
    Last Post: 09-24-2007, 11:17 AM
  3. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  4. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM