Callback function and function pointer trouble in C++ for a BST
Posted
by
Brendon C.
on Stack Overflow
See other posts from Stack Overflow
or by Brendon C.
Published on 2014-05-28T09:22:56Z
Indexed on
2014/05/28
9:24 UTC
Read the original article
Hit count: 230
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)
© Stack Overflow or respective owner