仍然找不到二叉树中的最大数字
Still can't find largest number in binary tree
我仍然找不到我的树中最大的数字,但现在我真的没有使它复杂化,我认为它应该工作,没有错误,但它只是不会在控制台中显示我。有人知道吗?我真的很沮丧。
下面是我的全部代码:
#include <iostream>
#include <string>
#include <cstdlib>
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)
{
if ( root == NULL ){
cout<<"There is no tree";
return -1;
}
if (theRoot->rChildptr != NULL)
return Largest(theRoot->rChildptr);
else
return theRoot->data;
cout<<theRoot->data;
};
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(15);
myBT->PrintTree();
myBT->Largest();
}
,这里是应该找到最大数字的部分(最右边的子元素):
T Largest( Node* theRoot)
{
if ( root == NULL ){
cout<<"There is no tree";
return -1;
}
if (theRoot->rChildptr != NULL)
return Largest(theRoot->rChildptr);
else
return theRoot->data;
cout<<theRoot->data;
};
在Largest()
的代码中有两个问题:
-
看起来你想在else子句中执行两条语句,但是你没有使用大括号。
-
您想在返回后执行计数打印,这是不可能的。
所以你应该把你的代码片段替换为:
else {
cout << theRoot->data;
return theRoot->data;
}
顺便说一句,不要让双分号(;;
)留在你的代码中。在大多数情况下,它是无害的,但它总是坏的风格。
下面是一个修复的、稍微清理过的版本:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
template<class T>
class BinaryTree
{
struct Node
{
T data;
Node* lChildptr;
Node* rChildptr;
Node(T dataNew)
{
data = dataNew;
lChildptr = 0;
rChildptr = 0;
}
};
private:
Node* root;
void Insert(T newData, Node** theRoot)
{
if (*theRoot == 0)
{
*theRoot = new Node(newData);
}
else if (newData < (*theRoot)->data)
Insert(newData, &((*theRoot)->lChildptr));
else
Insert(newData, &((*theRoot)->rChildptr));
}
void PrintTree(Node* theRoot)
{
if (theRoot != 0)
{
PrintTree(theRoot->lChildptr);
cout << theRoot->data << " " << endl;
PrintTree(theRoot->rChildptr);
}
}
T Largest(Node* theRoot)
{
if (root == 0)
{
throw "There is no tree";
}
if (theRoot->rChildptr != 0)
return Largest(theRoot->rChildptr);
else
return theRoot->data;
}
public:
BinaryTree()
{
root = 0;
}
void AddItem(T newData)
{
Insert(newData, &root);
}
void PrintTree()
{
PrintTree(root);
}
T Largest()
{
return Largest(root);
}
};
int main()
{
BinaryTree<int> *myBT = new BinaryTree<int>();
cout << "The tree is empty. Trying to find the largest element." << endl;
try
{
cout << "Largest element: " << myBT->Largest() << endl;
}
catch (const char* e)
{
cout << "Exception caught: " << e << endl;
}
myBT->AddItem(15);
myBT->AddItem(2);
myBT->AddItem(5);
myBT->AddItem(1);
myBT->AddItem(10);
cout << "Tree:" << endl;
myBT->PrintTree();
cout << "Largest element: " << myBT->Largest() << endl;
}
输出:The tree is empty. Trying to find the largest element.
Exception caught: There is no tree
Tree:
1
2
5
10
15
Largest element: 15
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 找不到数字文字运算符“运算符”“+BLA”
- 错误:找不到数字文字运算符"运算符 " " U"?
- C 搜索数组以获取一个数字,但如果找不到该数组,则不会触发其他语句
- 正则表达式 找不到数字序列
- 比较 unicode 和 wchar_t C++:"找不到数字文字运算符"运算符" " F""
- 仍然找不到二叉树中的最大数字