Creating Traversals for Binary Search Tree with Recursion.
void inOrder(void (*inOrderPtr)(T&)) { if(this->left != NULL) inOrder((*inOrderPtr)(this->left)); inOrderPtr(this->data); if(this->right != NULL) inOrder((*inOrderPtr)(this->right)); }
Here is the function. Now this is obviously wrong. This function is called like this:
first.inOrder(print_vals);
first is the object, and print vals is simply a function that prints what is the data in the object. There are three values for each object, data, left, and right. How do I actually access those items with the function?
It looks like the call to
inOrderPtr(this->data)is passing just thedatamember of the tree node to theprint_valsfunction. If you would like to access theleftandrightelements, useinOrderPtr(*this). You will have to change various declarations in order for this to compile, such as the declarations forinOrderPtrandprint_vals. Without seeing the rest of your code it’s hard to say what you need to change them to.On another note, it seems to me that you might want to write the recursive calls more like this:
I am making assumptions about your implementation, though.