帮助插入第一个BST
help with insert on first BST
编辑有一件小事我错过了!!错误还在
所以我试图学习如何编码我的第一个BST,这是很难....我已经有麻烦,只是几行代码。问题是在插入,但我已经包括了一切,这样我就可以得到一些反馈我的风格/其他错误。我被建议使用指针到指针的实现,但我们还没有学会它,所以我不觉得舒服/知道如何编码它。
error is
[trinhc@cs1 Assignment_3]$ g++ movieList.cpp -o a.out
/tmp/ccLw6nsv.o: In function `main':
movieList.cpp:(.text+0x7a): undefined reference to `Tree::Tree()'
movieList.cpp:(.text+0xa7): undefined reference to `Tree::insert(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
文件
#ifndef TREE_H
#define TREE_H
#include <string>
#include <iostream>
using namespace std;
class Tree
{
public:
Tree();
bool insert(int k, string s);
private:
struct Node
{
int key;
string data;
Node *left;
Node *right;
};
Node* root;
bool insert(Node*& root, int k, string s);
};
#endif
tree.cpp
#include <iostream>
#include "tree.h"
#include <stack>
#include <queue>
#include <string>
using namespace std;
Tree::Tree()
{
root = NULL;
}
bool Tree::insert(int k, string s)
{
return insert(root, k, s);
}
bool Tree::insert(Node*& current_root, int k, string s)
{
if(root == NULL){
current_root = new Node;
current_root->key = k;
current_root->data = s;
current_root->left = NULL;
current_root->right = NULL;
return true;
}
else if (current_root->key == k)
return false;
else if (current_root->key > k)
insert(current_root->left, k, s);
else
insert (current_root->right,k, s);
}
movieList.cpp
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"
using namespace std;
int main()
{
Tree test;
test.insert(100, "blah");
return 0;
}
Tree test();不是如何定义一个Test类的对象,这实际上声明了一个名为Test的函数,它返回Tree。
试
树测试;测试。instert(100年,"废话");返回0;
我复制了一些你的代码,这对我来说很好:
主:#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include "tree.h"
int main()
{
Tree test;
test.insert(100, "blah");
test.insert(50, "fifty");
test.insert(110, "one hundred ten");
return 0;
}
插入功能:bool Tree::insert(Node*& currentRoot, int k, string s)
{
if(currentRoot == NULL){
currentRoot = new Node;
currentRoot->key = k;
currentRoot->data = s;
currentRoot->left = NULL;
currentRoot->right = NULL;
return true;
}
else if (currentRoot->key == k)
return false;
else if (currentRoot->key > k)
insert(currentRoot->left, k, s);
else
insert (currentRoot->right,k, s);
}
除此之外,到处都是语法错误。我也改了名字,因为有人指出有一点命名问题。CurrentRoot是有意义的,因为您在每次递归时将左子树或右子树的根传递给它。
几点说明:
你需要改变你的成员变量root的名字为别的东西-我建议m_root,或my_root,或tree_root,或类似的东西。现在,在任何包含根作为参数的函数中,都有一点名称空间冲突。这还可以让您跟踪所引用的根目录。
bool Tree::insert(Node*& root, int k, string s)
{
if(root == NULL){
root = new Node;
root->key = k;
root->data = s;
root->left = NULL;
root->right = NULL;
return true;
} else
if (root == k) //Comparison between pointer and number.
return false;
else
if (root->key > k)
insert(root->left, k, s);
else
insert (root->right,k, s);
}
您需要将注释行中的root更改为root->key。
除此之外,它看起来是可以工作的。
编辑:还有,另一个人说了什么。将对象声明为TYPE name ()
如果你调用默认构造函数(),那么你的主函数中的代码应该是
Tree test;
test.insert(...)
不应该将tree.cpp
添加到构建命令中吗?
[trinhc@cs1 Assignment_3]$ g++movieList.cpp -o a.out
将成为
[trinhc@cs1 Assignment_3]$ g++tree.cpp movieList.cpp -o .out
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 在我的第一个C++程序中需要一些帮助(简单)
- 为什么我的代码在第一个 if 语句处中断?
- 是否可以从另一个类对象调用一个类函数而不继承第一个类
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 将参数初始化为构造函数,而不是第一个
- 帮助插入第一个BST