views:

206

answers:

1

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?

+2  A: 

It looks like the call to inOrderPtr(this->data) is passing just the data member of the tree node to the print_vals function. If you would like to access the left and right elements, use inOrderPtr(*this). You will have to change various declarations in order for this to compile, such as the declarations for inOrderPtr and print_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:

this->left->inOrder(inOrderPtr);

I am making assumptions about your implementation, though.

Greg Hewgill
http://rafb.net/p/aBmQZx94.html this is actually the driver program. I can't change any of that so I have to kind of follow the guide.
Doug
Ok this actually worked, and on top of the fact that did not put in BT<T>:: in front of void which was my dumb mistake. Thanks for the help! :) and I apologize for making it kind of messy, just heard about this site.
Doug
Glad to help. Don't forget to click the "accept answer" link if this was helpful! :)
Greg Hewgill