Function Pointers (C++)

Bananapie

New Member
I am implementing a binary tree in C++, and I am supposed to write inOrder, preOrder, postOrder etc.


Code:
template <class T> class binTree {

public:
binTree ( ); // default constructor
bool empty ( ) const; // checks if tree empty
int size ( ) const; // returns no of nodes
int height ( ) const; // returns height of tree
virtual void insert ( const T& ); // inserts a node in shortest subtree
void inOrder ( void ( * ) ( T& )); // inorder traversal of tree
void preOrder ( void ( * ) ( T& )); // preorder traversal of tree
void postOrder ( void ( * ) ( T& )); // postorder traversal of tree


protected:
binTreeNode <T>* root; // root of tree


private:
int size ( binTreeNode <T>* ) const; // private version of size
int height ( binTreeNode <T>* ) const; // private version of height
void insert ( binTreeNode <T>*&, const T& ); // private version of insert

void inOrder ( binTreeNode <T>*, void ( * ) ( T& )); // private version of inOrder

void preOrder ( binTreeNode <T>*, void ( * ) ( T& )); // private version of preOrder

void postOrder ( binTreeNode <T>*, void ( * ) ( T& )); // private version of postOrder
};
Code:
2
template <class T> class binTree;

template <class T> class binTreeNode {
friend class binTree <T>;

public:
// default constructor
binTreeNode ( const T& =T ( ), binTreeNode <T>* = 0, binTreeNode <T>* = 0 );

private:
T data; // data value in node
binTreeNode <T> *left, *right; // links to other nodes
};
I write the public functions for inOrder, postOrder, and preOrder as followed:

My code:
Code:
template <class T> void binTree<T>::inOrder ( void ( *order ) ( T& ))
{
inOrder(root, order);
}

template <class T> void binTree<T>::preOrder ( void ( *order ) ( T& ))
{
preOrder(root, order);
}

template <class T> void binTree<T>::postOrder ( void ( *order ) ( T& ))
{
postOrder(root, order);
}
and the private functions as

Code:
//********************
//*****preOrder*******
//********************
template <class T> void binTree<T>::preOrder (binTreeNode <T>* node1, void (*order) (T&))
{//begin preOrder
        if (node1 != NULL)
        {//begin if
                order(node1->data);
                order(node1->left); //prints left subtree
                order(node1->right); //prints right subtree
        }//end if
}//end preOrder


//*********************
//*****postOrder*******
//*********************
template <class T> void binTree<T>::postOrder (binTreeNode <T>* node1, void (*order) (T&))
{//begin postOrder
cout << "h" << endl;
        if (node1 != NULL)
        {//begin if
                order(node1->left); //prints left subtree
                order(node1->right); //prints right subtree
                order(node1->data);
        }//end if
}//end postOrder

//**********************
//*****inOrder**********
//**********************
template <class T> void binTree<T>::inOrder (binTreeNode <T>* node1, void (*order) (T&))
{//begin inOrder
cout << "Heyoooo" << endl;

        if (node1 != NULL)
        {//begin if
                order(node1->left);
                order(node1->data);
                order(node1->right);
        }//end if

}//end inOrder

It is not liking that I am passing root, and the function pointer order in my public version. My driver(main) program where it initiates it is right here:

Code:
#define ORD( x, y, s, t )\
    cout << "The elements of '" << s << "' in " << t << ":\n\t";\
    x.y ( print_val ); cout << endl

template <class T>
void print_val ( T& x ) { cout << x << ' '; }
where x = first(the object after elements are inserted in it
y = .inOrder/.postOrder/.preOrder
s = "first" (string to tell the user what is what)
t = "inorder/postorder/preorder" (string to tell user what is what)



As I mentioned, I get errors in the public function if inOrder(so far) where I pass root and order(right now, I am passing (root, inOrder) but have tried (root, order) as well... and I am unsure why. :confused: I have more info to give if needed, but I believe this is what one might need.

My current error is:
Segmentation fault
 
Last edited:

Bananapie

New Member
Found my problem.

Code:
template <class T> void binTree<T>::preOrder (binTreeNode <T>* node1, void (*order) (T&))
{//begin preOrder
        if (node1 != NULL)
        {//begin if
                order(node1->data);
                [B]preOrder[/B](node1->left[B], order[/B]); //prints left subtree
                [B]preOrder[/B](node1->right[B], order[/B]); //prints right subtree
        }//end if
}//end preOrder
For anyone that is interested in the obviously harder way to do a binary tree, you just switch out in each order... Definitely a rookie mistake, and pretty disappointed I was so close and this is all that was wrong.
 
Top