Read text file into binary search tree

Apr 28, 2012 at 10:18pm
I need to read a file and insert each word into a binary search tree. I'm halfway there but I'm a little stumped. It opens the file just fine but then how to get each separate word into its own node is beyond me. Here is what I have. It's half code and half pseudo code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(inFile.is_open())
         {
           while(getline(inFile,lineFile))//inFile: file //lineFile: line in file
           {
               //convert text in file to lower case
               toLowerCase(lineFile);
               
               //get each word into a node in a bst
               binaryTree.insert(word);
               
           }

            
           }
         else cout << "Unable to open file";
         inFile.close();


Apr 29, 2012 at 2:59am
If the file has words seperated by stpaces or tabs, you can read it like this:
1
2
3
4
5
std::string word;
while (inFile >> word)
{
    // add word to tree
}
Apr 29, 2012 at 5:55am
That works great, thanks. However, as it iterates through several documents, I get a segmentation fault halfway through the second one. I can't figure what is causing it. If you need to see more code than what I have originally posted, let me know.

Thanks
Topic archived. No new replies allowed.