帮助插入第一个BST

help with insert on first BST

本文关键字:BST 第一个 插入 帮助      更新时间:2023-10-16

编辑有一件小事我错过了!!错误还在

所以我试图学习如何编码我的第一个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