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;
}