C++ 有关BST插入函数的帮助

C++ 有关BST插入函数的帮助,c++,g++,helper,C++,G++,Helper,嘿,所以我试图写一个BST,但我有很多错误,我非常不知所措,不知所措。你们能看一下并指出有什么不对劲的地方吗。老师在解释任何事情上都没有什么用处 .h文件中的头 class Tree { public: bool insert(int k, string s); private: struct Node { int key; string data; Node *left; Node *right;

嘿,所以我试图写一个BST,但我有很多错误,我非常不知所措,不知所措。你们能看一下并指出有什么不对劲的地方吗。老师在解释任何事情上都没有什么用处

.h文件中的头

class Tree
{
public:
    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);
};
.cpp文件

bool Tree::insert(int k, string s)
{
    return insert(root, k, s);
}
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;
    }
    else if (root == k)
        return false;
    else if (root->key < k)
        insert (root ->left, k);
    else
        insert (root -> right, k);
}
bool树::插入(int k,字符串s)
{
返回插入(根、k、s);
}
布尔树::插入(节点*&根,整数k,字符串s)
{
if(root==NULL){
根=新节点;
根->键=k;
根->数据=s;
根->左=空;
root->right=NULL;
}
else if(根==k)
返回false;
else if(根->键左,k);
其他的
插入(根->右,k);
}

首先,代码末尾缺少一个括号

首先,代码末尾缺少一个括号

看来您有很多未经测试的代码。您必须一次一个地让功能正常工作

此外,我已经看到了至少一个重复工作的实例。
前序
后序
方法应该是同一算法的细微变化,但其中一种是基于循环的,另一种是通过函数递归工作的

将这些源文件放在一边,创建一个新项目,然后一次复制粘贴一个要素。在继续下一个功能之前,请进行彻底测试。如果您获得的功能可能已经存在于现有代码中,请不要从旧代码复制粘贴,而是利用现有的测试代码

编辑 这看起来应该行得通。这里有一些风格上的批评,如果你愿意的话:v)

  • 节点
    结构应该有自己的构造函数。始终提供一个构造函数来帮助保证任何东西都不能处于无效状态
  • 同样,当您创建一个新的
    树时,需要将
    root
    设置为
    NULL
  • 更改
    root
    参数的值不是一种好的样式。乍一看,它看起来像是
    left
    right
    从不接收非
    NULL
    值。就我个人而言,我喜欢这里的指针指向指针,但有些人可能会同意您的实现

  • 看起来您有很多未经测试的代码。您必须一次一个地让功能正常工作

    此外,我已经看到了至少一个重复工作的实例。
    前序
    后序
    方法应该是同一算法的细微变化,但其中一种是基于循环的,另一种是通过函数递归工作的

    将这些源文件放在一边,创建一个新项目,然后一次复制粘贴一个要素。在继续下一个功能之前,请进行彻底测试。如果您获得的功能可能已经存在于现有代码中,请不要从旧代码复制粘贴,而是利用现有的测试代码

    编辑 这看起来应该行得通。这里有一些风格上的批评,如果你愿意的话:v)

  • 节点
    结构应该有自己的构造函数。始终提供一个构造函数来帮助保证任何东西都不能处于无效状态
  • 同样,当您创建一个新的
    树时,需要将
    root
    设置为
    NULL
  • 更改
    root
    参数的值不是一种好的样式。乍一看,它看起来像是
    left
    right
    从不接收非
    NULL
    值。就我个人而言,我喜欢这里的指针指向指针,但有些人可能会同意您的实现

  • 不看实际的实现,有很多与接口相关的陷阱。以下是其中一些:

    • 头文件中缺少您
    • 允许在包含的头文件的全局范围中使用“usingnamespace std”,但这是一种非常糟糕的风格
    • Tree::findKey
      方法接受字符串作为非常量引用,为什么
    • Tree::insert
      按值接受字符串,这涉及不必要的复制,为什么
    • preOrder
      levelOrder
      需要指向函数的指针。如果我想调用一个类方法,或者传递其他参数,该怎么办?我想说,您最好使用谓词,或者
      boost::function
    • 不修改状态(类中的数据)的方法不是常量(
      const
      modifier)。例如,
      isEmpty()
      方法
    • makeCopy
      方法不可用
    • 使用带符号整数代替
      size\t

    希望能有所帮助。

    不看实际的实现,有很多与接口相关的陷阱。以下是其中一些:

    • 头文件中缺少您
    • 允许在包含的头文件的全局范围中使用“usingnamespace std”,但这是一种非常糟糕的风格
    • Tree::findKey
      方法接受字符串作为非常量引用,为什么
    • Tree::insert
      按值接受字符串,这涉及不必要的复制,为什么
    • preOrder
      levelOrder
      需要指向函数的指针。如果我想调用一个类方法,或者传递其他参数,该怎么办?我想说,您最好使用谓词,或者
      boost::function
    • 不修改状态(类中的数据)的方法不是常量(
      const
      modifier)。例如,
      isEmpty()
      方法
    • makeCopy
      方法不可用
    • 使用带符号整数代替
      size\t

    希望有帮助。

    写第一个BST可能很难。。。根据Potatoswatter的建议,我首先要确保在去其他任何地方之前有一个非常可靠的insert函数。因此,您可以为BST创建类,包括函数的声明,
    class BST
    {
        public:
            BST();
            bool insert(int k, const string& s);
            bool findKey(int k, string& s);
            int maxKey();
    
            /* ... the rest of your class ...*/
    };
    
    #include "BST.h"
    
    BST::BST()
    {
        /*initialize anything required for your insert function to work*/
    }
    
    //pass in a const reference for your string argument, 
    //or else you could end up doing a lot
    //of extra processing creating a new copy of your string on the stack for
    //each insertion call
    bool BST::insert(int k, const string& s)
    {
        /* write your insertion algorithm implementation */
    }
    
    //make the rest of your function definitions that don't
    //apply to insertion empty so you can compile and test your insertion algorithm
    //without adding more cruft
    bool BST::findKey(int k, string& s) {}
    
    int BST::maxKey() {}
    
    /* continue process for the rest of the class */