所以我试图找到我的二叉树中最大的数字,所以我可以删除它,但它不会运行,插入部分,我正在寻找最大的数字,树工作得很好,没有这部分。二叉树中最大的数字
这里是我得到了现在的代码:
#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);
}
}
void Largest(Node* theRoot, T max)
{
if (theRoot == null)
return -1;
int left = Largest(theRoot->lChildptr);
int right = Largest (theRoot->rChildptr);
if(theRoot->data > left && theRoot->data > right)
return theRoot->data;
else
return max (left, right);
};
public:
BinaryTree()
{
root = NULL;
}
void AddItem(T newData)
{
Insert(newData, root);
}
void PrintTree()
{
PrintTree(root);
}
void Largest()
{
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();
}
使用调试器(例如Linux上的'gdb')来找出发生了什么。 –