I want to employ std::function to help me run a generic traverse function that traverses a BST and calls the parametrized function.
My difficulty is that the parametrized function varies in its own parameters.
So, for example, I was to generalize the following three functions (all different in their parameters).
//populates an array with the values in the BST by traversing the BST
void LinkedBST<T>::populate(T const * const data, size_t & I, Node *x)
{
data[i++] = x->val;
}
//insert unique values of another BST into "this" BST: traverses the other BST and inserts every value
void LinkedBST<T>::insert(Node *x)
{
insert(x->val);
}
Instead of writing a separate traverse function for each one of the aforementioned functions, I want to be able to pass them into one generic traverse function, something like:
void LinkedBST<T>::traverse(Node *x, auto func)
{
if(x == nullptr)
return;
traverse(x->left, func);
func( <parameters> );
traverse(x->right, func);
}
Is there any way to do this? If there is, can you help me do it?
Thank you :)