#include <iostream>
#include <string>
using namespace std;
template<class T>
class BinaryTree
{
struct Node
{
T data;
Node* lChildptr;
Node* rChildptr;
Node(T dataNew)
{
data = dataNew;
lChildptr = NULL;
rChildptr = NULL;
}
};
private:
Node* root;
void Insert(T newData, Node* &theRoot)
{
if(theRoot == NULL)
{
theRoot = new Node(newData);
return;
}
if(newData < theRoot->data)
Insert(newData, theRoot->lChildptr);
else
Insert(newData, theRoot->rChildptr);;
}
void PrintTree(Node* theRoot)
{
if(theRoot != NULL)
{
PrintTree(theRoot->lChildptr);
cout<< theRoot->data<<" \n";;
PrintTree(theRoot->rChildptr);
}
}
T Largest( Node* theRoot)//<-- Notice returning of the template, not void
{
if ( theRoot == null )
return -1;
T left = Largest(theRoot->lChildptr);
T right = Largest ( theRoot->rChildptr);
if( theRoot->data > left && theRoot->data > right )
return theRoot->data;
else if (left < right)//<-- max was not a function, manual compare
return right;
else
return left;
}//<--You were missing a "}" here too
//...
public:
BinaryTree()
{
root = NULL;
}
void AddItem(T newData)
{
Insert(newData, root);
}
void PrintTree()
{
PrintTree(root);
}
T Largest()
{
return Largest(root);
}
};
int main()
{
BinaryTree<int> *myBT = new BinaryTree<int>();
myBT->AddItem(2);
myBT->AddItem(5);
myBT->AddItem(1);
myBT->AddItem(10);
myBT->AddItem(8);
myBT->PrintTree();
myBT->Largest();
}