Please Help I don't what's wrong with my preorder for my vector

REMOVED ISSUE SOLVED
Last edited on
preorder(tree->left, preorder);

This appears to be... an attempt to call a function named preorder, that accepts itself as a parameter? What are you trying to do with this line?
preorder = preorder(tree->left, preorder);
Last edited on
I was trying to implement the basic rules so I can get my preorder to work with the vector to hold words from the text file.
preorder = preorder(tree->left, preorder); should be preorder = value(tree->left, preorder); and the same with the call to tree->right.

Notice that you're passing preorder by value. That means it gets copied each time you call value, and that will take a whole lot of time as the tree gets larger. Instead, you should pass it by reference and return void:
1
2
3
4
5
6
7
void value(Bst*tree, vector<string> &preorder)
{
    if (tree == nullptr) return;
    preorder.push_back(tree->value);
    value(tree->left, preorder);
    value(tree->right(preorder);
}

Notice that I've changed the nullptr check to check tree instead of tree->right and tree->next. This makes the code shorter at the expense of extra calls. More importantly though, it fixes a bug: now you call value on an empty tree.

You can do a similar trick to greatly reduce the code in insert_tree(). Once you have the code working, save a copy and then see if you can shrink insert_tree().
Topic archived. No new replies allowed.