Here is the start of the code I have been working on implementing a binary tree. It is just a beginning, and preludes post about binary trees makes me want to change the implementation a bit. I am getting a simple syntax error that has me baffeld also any comments on this beginning code are welcome.
binarytree.cc:69: error: syntax error before `{' tokenCode://binarytree.cc uses struct, simple function to seperate // sentence into words in a binary tree // uses inordertraversal to display sorted words #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; struct tree_node { string word; int count; tree_node* left; tree_node* right; }; // takes a sentence a builds a binary tree constructed from the words void buildtree(tree_node*,string); void insert_node(tree_node**, string); int main(int argc, char * argv[]) { string sentence; tree_node* root; // top of binary tree cout << "Enter a sentence press enter when done." << endl; getline(cin,sentence); buildtree(root,sentence); }; void buildtree(tree_node * base, string wordlist) { //delims holds charachters used to seperate words froms sentence const string delims(" \t"); int count = 0;// word count string::size_type begIdx, endIdx;// indices of single word //search for beginning of first word begIdx = wordlist.find_first_not_of(delims); //while beginning of word found loop while (begIdx != string::npos){ //search for end of actual word endIdx = wordlist.find_first_of(delims, begIdx); if (endIdx == string::npos){ //end of word is end of line endIdx = wordlist.length(); } insert_node(&base,wordlist.substr(begIdx,endIdx)); } } void insert_node(tree_node **ptrnode, string word) { // test to see if node has been initialized or is null if (*ptrnode==0) *ptrnode = new tree_node{word,1,0,0};// syntax error here before { else //has been initialized so //insert into left branch if less than parent value if (word<((*ptrnode)->word)) insert_node( &((*ptrnode)->left),word,count); else //values in right branches are greater than values in parent node if (word>((*ptrnode)->word)) insert_node( &((*ptrnode)->right),word,count); else //duplicate word so just increase count ++((*ptrnode)->count); }



LinkBack URL
About LinkBacks



anyhow here is the working code so far thanks for the help!