Thread: binary tree token problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User OldSchool's Avatar
    Join Date
    Apr 2006
    Location
    Virginia
    Posts
    18

    binary tree token problem

    Hi there... I've got a question about tokenizing strings...

    I'm attempting to write a program that inputs a sentence from the user, tokenizes it and then hands the words to a binary search tree. I am using 'strtok' to to tokenize the sentence so the original is in a char format. I can print the individual tokens using a pointer and everything works great until I hand the tokens to the tree insert function. It appears that the program only hands off the first character of each word instead of the whole token. I get warnings that the individual first letters are dups (which is a check in the insertNode function template) so I think the function call to insertNode might be the culprit although I can't figure out a way to hand the tokens off any other way (I tried several).

    Here is the pertinent code (I omitted the header files because I suspect the problem is in the function call ):

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    
    #include <cstring>  // prototype for strtok
    
    #include "tree.h"  // Tree class definition
    
    int main()
    {
       Tree< char > charTree;  // create Tree
        char sentence[150];
        char *tokenPtr;
    	
        cout << "Enter a sentence (150 characters max):\n";
    
       cin.getline( sentence, 150, '\n' );
    
    
        cout << "The string to be tokenized is:\n" << sentence
            << "\n\nThe tokens are:\n\n";
    
        // begin tokenization of sentence
        tokenPtr = strtok( sentence, " " );
    
        // continue tokenizing sentence until tokenPtr becomes NULL
        while ( tokenPtr != NULL ) {
    	charTree.insertNode( *tokenPtr );
    	cout << tokenPtr << '\n';
            tokenPtr = strtok( NULL, " " );  // get next token 
     
        } // end while
    
        cout << "\nPreorder traversal\n";
        charTree.preOrderTraversal();
    
        cout << "\nInorder traversal\n";
        charTree.inOrderTraversal();
    
        cout << "\nPostorder traversal\n";
        charTree.postOrderTraversal();
    
        cout << endl;
    
        return 0;
    
    } // end main
    The assignment suggested to use the strtok function so I assumed that this would be the easiest way to accomplish the tokenization of the sentence. I checked many of the previous binary tree threads here but could not find anything that seemed similar to this particular problem. I'm probably missing something obvious but I can seem to figure out why the whole word is not getting passed to the function...

    Any assistance would be appreciated. Thanks...
    Last edited by OldSchool; 05-27-2006 at 09:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree scope? problem
    By tms43 in forum C++ Programming
    Replies: 5
    Last Post: 11-01-2006, 10:13 PM
  2. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  3. problem in storing data in a binary search tree
    By alavardi in forum C Programming
    Replies: 5
    Last Post: 02-13-2005, 03:20 PM
  4. Problem with binary search tree :(
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 05-01-2002, 10:31 PM
  5. problem in binary expression tree
    By hanij in forum C Programming
    Replies: 6
    Last Post: 04-28-2002, 08:31 AM