I am making a word ladder and have gotten some headway on creating a tree design but for some reason in my insertion function I am getting an error saying that this is the "first use of this function" This function being my vector<node*>"children". (I have the area marked with ****) Needless to say, I have no idea what the error could possibly be stemming from. Just hoping for some help. Thanks a lot!

struct node 
{
       node* parent;
       vector<node*> children(1, NULL);
       string word;
};

class tree
{
    public:
           tree(string firstWord, string end);
           bool insert(string newWord);
           bool checker();
           
    private:
            node * root;
            string searchWord;
            bool insert(string newWord, node * ptr);
            
    
};


bool tree::insert(string newWord)
{
     return insert(newWord, root);
}

bool tree::checker()
{
   if(root->word == searchWord)
   {
      cout<<root->word<<endl;
      return true;
   }
   if(root->word.length() != searchWord.length())
   {
      cout<<"No Solution."<<endl;
      return true;
   }
}


tree::tree(string firstWord, string end)
{
     searchWord=end;
     root=new node;
     root->word=firstWord;
     root->parent=NULL;
     root->children.push_back(NULL);
}


bool tree::insert(string newWord, node * ptr)
{
   bool which=false;
   bool inserted=false;
   int difference=0;

   for (int x=0;x<ptr->word.length();x++)
   {if(ptr->word[x]!=newWord[x])difference++;}
   if(difference==1)
   {
       if(ptr->children.empty())
       {
          ptr->children.push_back(new node);
//**************************************************Right here!
          //ptr->children[children.size()-1]->parent=ptr;
          ptr->word=newWord;
          inserted=true;
       }
       else
       {
          if(!ptr->children.empty())
          {
             for(int y=0;y<ptr->children.size();y++)
             {if(ptr->children[y]->word==newWord)which=true;}
             if(!which)
             {
                ptr->children.push_back(new node);
                //ptr->children[children.size()-1]->parent=&ptr;
                ptr->word=newWord;
                inserted=true;
             }
             for(int z=0;z<ptr->children.size()-1;z++)
               inserted=(inserted || insert(newWord,ptr->children[z]));                         
          }
       }
   }
   if(difference>1)
   {
      if(!ptr->children.empty())
      {
          for(int w=0;w<ptr->children.size()-1;w++)
            inserted=(inserted || insert(newWord,ptr->children[w]));
          
      }
   } 
   return inserted;              
}

Recommended Answers

All 2 Replies

Aside from what you pointed out, I'm not sure what you are trying to do here in this node struct. What does children represent? Is there a children data type? Is that a constructor? Is there a function called children?

struct node 
{
       node* parent;
       vector<node*> children(1, NULL);
       string word;
};

oops, sorry about that, that was just an experiment. The line was supposed to read

vector<node*> children;

children is just the variable name of the vector of node*. It represents the pointers to all the child nodes branching off on a tree. The parent is the parent node on a tree.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.