Search code examples
c++binary-search-treepointer-to-member

Callback function and function pointer trouble in C++ for a BST


I have to create a binary search tree which is templated and can deal with any data types, including abstract data types like objects. Since it is unknown what types of data an object might have and which data is going to be compared, the client side must create a comparison function and also a print function (because also not sure which data has to be printed).

I have edited some C code which I was directed to and tried to template, but I cannot figure out how to configure the client display function. I suspect variable 'tree_node' of class BinarySearchTree has to be passed in, but I am not sure how to do this.

For this program I'm creating an integer binary search tree and reading data from a file. Any help on the code or the problem would be greatly appreciated :)

Main.cpp

#include "BinarySearchTreeTemplated.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

/*Comparison function*/
int cmp_fn(void *data1, void *data2)
{
    if (*((int*)data1) > *((int*)data2))
        return 1;
    else if (*((int*)data1) < *((int*)data2))
        return -1;
    else
        return 0;
}

static void displayNode()  //<--------NEED HELP HERE
{
    if (node)
        cout << " " << *((int)node->data)
}

int main()
{
    ifstream infile("rinput.txt");
    BinarySearchTree<int> tree;

    while (true) {
        int tmp1;
        infile >> tmp1;
        if (infile.eof()) break;
        tree.insertRoot(tmp1);
    }
        return 0;
}

BinarySearchTree.h (a bit too big to format here)

http://pastebin.com/4kSVrPhm


Solution

  • I think the server side shall provide the comparison and print functions. This can be done as a template as well.

    template <class T>
    int cmp_fct( const T& rhs, const T& lhs ) {
        if( rhs < lhs) return -1;
        else if (rhs > lhs) return 1;
        return 0;
    }
    

    Same procedure for the displayNode.

    Now the client has to provide some operators:

    struct TestObject {
        int i;
    
        friend bool operator<( const TestObject& rhs, const TestObject& lhs ) {
            return rhs.i < lhs.i;
        }
    
        friend bool operator>( const TestObject& rhs, const TestObject& lhs ) {
            return lhs < rhs;
        }
    
        // and stream operators 
        friend std::ostream& operator<<( std::ostream& output, const TestObject & ob)
        {
            output << ob.i;
            return output;
        }
    };
    

    Furthermore, i would regard using c++ methods for your BinarySearchTree, i.e. use smart pointers instead of raw pointers.

    Your BinarySearchTree creates objects but never deletes them.

    Avoid using void-pointers, use Interfaces (e.g. INode), than, the client has to derive from the interface ...

    Use constructor initialization lists, otherwise, objects are created by the default constructor and then reassigned within the constructor.