简体   繁体   中英

How to use Functional Pointers in C++ for binary search Tree?

this is BinarySearchTree.h file

I am creating a binary search tree template class, and want to use function pointers for the inorder traversal function, i am okay with function pointers but for some reason am lost on how to use the inorder once the code is in place. not much other stuff online has helped me so any feed back would be great.

template <class T>
struct nodeType  
{
    // your write the code
        T data;
        nodeType * left;
        nodeType * right;
        nodeType(T key) :data(key), left(nullptr), right(nullptr) {}

};

template<class T>
typedef void (*f1Typ)(T &);  // you are not restricted to a single parameter. You 
                                // decide on how many parameters and provide rationale

template <class T>
class Bst // answer why class encapsulation is used - rationale
{

public:

            // you write whatever else is needed. You already have some from Lab 9.
            // insert is needed.

    void inOrderTraversal(f1Typ f1 ) const;
    
    void preOrderTraversal() const; // parameter f1 in here too. left as an exercise
    void postOrderTraversal() const; // parameter f1 in here too. left as an exercise

private:
    
    
    nodeType<T> *root;

            // you write the rest

    void inOrder(f1Typ f1, nodeType<T> *p) const;
    // you write whatever else is needed.


};

// you write the rest. You already have some from Lab 9.

template<class T>
inline void Bst<T>::inorder( f1Typ f1,nodeType<T> * root) const
{
    if (root->left != nullptr)
    {
        inorder(f1,root->left);
    }

    f1(root->data);

    if (root->right != nullptr)
    {
        inorder(f1,root->right);
    }
}

template<class T>
inline void Bst<T>::inorder(f1Typ f1) const
{   
    inorder(f1,this->root);
}

This is main Method // Using function pointer

int test1()
{
    BinarySearchTree<int>  intTree; 

    for (int i =0; i< 20; i++)
    {
        intTree.insertElement(i);
    }

    intTree.inOrderTraversal(f1t);  // similar to the approach used in the text - just prints, so not very useful

    return 0;

}

void f1t(int &data)
{
    cout << data << "  ";
}
 **These are the errors** 
*BinarySearchTree.h:22:1: error: template declaration of 'typedef'
   22 | typedef void (*f1Typ)(T &);  // you are not restricted to a single parameter. You
      | ^~~~~~~
BinarySearchTree.h:71:29: error: 'f1Typ' was not declared in this scope*

this is BinarySearchTree.h file

I am creating a binary search tree template class, and want to use function pointers for the inorder traversal function, i am okay with function pointers but for some reason am lost on how to use the inorder once the code is in place. not much other stuff online has helped me so any feed back would be great.

template <class T>
struct nodeType  
{
    // your write the code
        T data;
        nodeType * left;
        nodeType * right;
        nodeType(T key) :data(key), left(nullptr), right(nullptr) {}

};

template<class T>
typedef void (*f1Typ)(T &);  // you are not restricted to a single parameter. You 
                                // decide on how many parameters and provide rationale

template <class T>
class Bst // answer why class encapsulation is used - rationale
{

public:

            // you write whatever else is needed. You already have some from Lab 9.
            // insert is needed.

    void inOrderTraversal(f1Typ f1 ) const;
    
    void preOrderTraversal() const; // parameter f1 in here too. left as an exercise
    void postOrderTraversal() const; // parameter f1 in here too. left as an exercise

private:
    
    
    nodeType<T> *root;

            // you write the rest

    void inOrder(f1Typ f1, nodeType<T> *p) const;
    // you write whatever else is needed.


};

// you write the rest. You already have some from Lab 9.

template<class T>
inline void Bst<T>::inorder( f1Typ f1,nodeType<T> * root) const
{
    if (root->left != nullptr)
    {
        inorder(f1,root->left);
    }

    f1(root->data);

    if (root->right != nullptr)
    {
        inorder(f1,root->right);
    }
}

template<class T>
inline void Bst<T>::inorder(f1Typ f1) const
{   
    inorder(f1,this->root);
}

This is main Method // Using function pointer

int test1()
{
    BinarySearchTree<int>  intTree; 

    for (int i =0; i< 20; i++)
    {
        intTree.insertElement(i);
    }

    intTree.inOrderTraversal(f1t);  // similar to the approach used in the text - just prints, so not very useful

    return 0;

}

void f1t(int &data)
{
    cout << data << "  ";
}
 **These are the errors** 
*BinarySearchTree.h:22:1: error: template declaration of 'typedef'
   22 | typedef void (*f1Typ)(T &);  // you are not restricted to a single parameter. You
      | ^~~~~~~
BinarySearchTree.h:71:29: error: 'f1Typ' was not declared in this scope*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM