难以插入二叉搜索树

difficulty inserting into binary search tree

我目前正在学习C++,并且一直在研究BST。我在工作簿中承担了一项任务,但一直在努力完成实施。

我不能更改头文件或任何函数定义,但可以添加助手。

我尝试添加一些助手,但是每当我 运行 测试文件时,没有任何单词被插入到树中。

我希望有人能够帮助我或指导我完成实施。

如能对其他功能提供任何帮助,我们将不胜感激!

我的头文件:

#pragma once

#include <iostream>
#include <string>

// Node of the tree
struct Node
{
    std::string data;

    Node *left = nullptr;

    Node *right = nullptr;
};

class BST
{
private:
    Node *root = nullptr;
public:

    BST();

    BST(std::string word);

    BST(const BST &rhs);

    ~BST();

    void insert(std::string word);

    void remove(std::string word);

    bool exists(std::string word) const;

    std::string inorder() const;

    std::string preorder() const;

    std::string postorder() const;

    bool operator==(const BST &other) const;

    bool operator!=(const BST &other) const;
};

我的 BST 文件:

#include "bst.h"
#include <iostream>
#include <string>


using namespace std;

string HelperInsert(Node **root, std::string word)
{
    if (*root == nullptr)
    {
        // Create new node
        *root = new Node;
        // Set new value
        (*root)->data = word;
        // Set branches to nullptr
        (*root)->left = nullptr;
        (*root)->right = nullptr;
    }
    else
        if (word < (*root)->data)
        {
            HelperInsert(&(*root)->left, word);

        }
        else
        {
            if (word > (*root)->data)
            {
                HelperInsert(&(*root)->right, word);
            }
        }
}

void HelperExists(Node *root, string word)
{
    Node *temp = new Node;
    temp = root;
    // Run the loop untill temp points to a NULL pointer.
    while(temp != NULL)
    {
        if(temp->data == word)
        {
            cout<<"True "<<endl;
        }
        // Shift pointer to left child.
        else if(temp->data > word)
            temp = temp->left;
        // Shift pointer to right child.
        else
            temp = temp->right;
    }
    cout<<"\n False";
    return;
}

string Helpwr(Node *root)
{
    if(root == nullptr)
    {
        return "";
    }
    else
    {
        return inorderHelper(root->left)
        + root->data + " "
        + inorderHelper(root->right);
    }

}

void HelperPreOrder(Node *root)
{
    if ( root !=nullptr)
    {
        cout << root->data << " ";
        HelperPreOrder(root->left);
        HelperPreOrder(root->right);
    }
}
void HelperPostorder(Node *root)
{
    if (root!=nullptr)
    {
        HelperPostorder(root->left);
        HelperPostorder(root->right);
        cout << root->data<< endl;
        return;
    }
}


BST::BST()
{
    root= nullptr;
}

// Creates a binary tree by copying an existing tree
BST::BST(const BST &rhs)
{

}


void BST::insert(string word)
{
    HelperInsert(*root, word);
}


void BST::remove(string word)
{

}


bool BST::exists(string word) const
{
    HelperExists(root, word);
    return true;
}


std::string BST::inorder() const
{
    string  res = inorderHelper(root);
    int len = res.length();
    if(len >= 1 && res[len -1] == ' ')
    {
        res.pop_back();
    }
    return res;
}


std::string BST::preorder() const
{
    HelperPreOrder(root);
    return std::string("");
}


std::string BST::postorder() const
{
    HelperPostorder(root);
    return std::string("");
}


bool BST::operator==(const BST &other) const
{
    return true;
}


bool BST::operator!=(const BST &other) const
{
    return true;
}

我的测试文件:

 tree = new BinarySearchTree();
    // Test 3 - insertion check
    tree->insert("fish");
    tree->insert("aardvark");
    tree->insert("zeebra");
    str = tree->inorder();

    if (str != string("aardvark fish zeebra"))
        cerr << "test 3 failed" << endl;
    else
        cout << "Test 3 passed" << endl;

    // Test 4 - exists check

    if (tree->exists("zeebra") && tree->exists("fish") && !tree->exists("bird") && !tree->exists("snake"))
        cout << "Test 4 passed" << endl;
    else
        cerr << "test 4 failed" << endl;

似乎是字符串单词存储不正确的问题:

你的插入方法如下:

void insert(std::string word);

这意味着传递的是字符串的值,而不是引用。 为了存储字符串,您必须创建字符串的副本并将其存储在内存中:

string HelperInsert(Node **root, std::string word)
{
    if (*root == nullptr)
    {
        // Create new node
        *root = new Node;
        // Set new value
        (*root).data = new std:string(word); // note the setting of a string pointer here!
        // Set branches to nullptr
        (*root)->left = nullptr;
        (*root)->right = nullptr;
    }
    else
        if (word < (*root)->data)
        {
            HelperInsert(&(*root)->left, word);

        }
        else
        {
            if (word > (*root)->data)
            {
                HelperInsert(&(*root)->right, word);
            }
        }
}

删除节点时不要忘记删除字符串,以防止内存泄漏。 祝你好运!