【发布时间】:2020-10-08 20:35:00
【问题描述】:
我现在正在学习 C++。
我想将BST中add函数的返回类型设置为bool,如果树中已经有相同的项,则返回true,添加到树中时返回false。
这是代码,有什么建议可以实现这个目标吗?
节点类(.h文件)中的代码:
node* insert(string content, node* t)
{
if(t == NULL)
{
t = new node;
t->data = content;
t->left = t->right = NULL;
}
else if(content < t->data)
t->left = insert(content, t->left);
else if(content > t->data)
t->right = insert(content, t->right);
return t;
}
.cpp 文件中的代码:
BST :: BST() {
root = NULL;
}
void BST :: add(string content) {
root = insert(content, root);
}
有什么建议吗?此代码也引用自BST Implementation in C++
【问题讨论】:
-
欢迎来到 Stack Overflow。请尽快阅读About 页面并访问描述How to Ask a Question 和How to create a Minimal, Complete, and Verifiable example (MCVE) 的链接。提供必要的详细信息,包括您的 MCVE、编译器警告和相关错误(如果有),将允许这里的每个人帮助您解决您的问题。
-
那些返回值听起来倒退。添加成功返回true,否则返回false。
标签: c++ data-structures binary-search-tree