I've been trying to create a functionality in C++ that can implement a BST from an array of strings, but it doesn't do anything at all and I don't understand what might be the problem. It should work recursively with for loop to iterate through all strings in an array.
void insertNode(node* root, std::string name)
{
if (root == nullptr)
{
node* n = new node;
n->name_ = name;
root = n;
}
else if (root->name_.compare(name) <= 0)
{
insertNode(root->right_, name);
}
else
{
insertNode(root->left_, name);
}
}
void CreateBST(node_t *& root, const std::vector<std::string> & array)
{
for (int i = 0; i < array.size(); i++)
{
insertNode(root, array[i]);
}
}
I've tried to wirte recursive function to create a BST, while comparing strings from an array to set them as nodes.
rootto have an effect outsideinsertNodeit needs to benode**.std::stringinstead of thecompare()method. IMHO, easier to read using the comparison operators.last compare is obsoleteI meant condition is obsolete since is covered by earlierif(you are inelsesection of it), code below is still needed.