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 ):
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...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
Any assistance would be appreciated. Thanks...



LinkBack URL
About LinkBacks




The problem is both the definition of your tree object and the call to insertNode. You define the template as having char as the type:
It now comes out exactly in the same order as the inorder traversal... The original setup I used seemed to work OK (with the first letters)... Is this the dependency problem you spoke of?