繁体   English   中英

c ++值未保存到指针(二进制搜索树)

[英]c++ Value not being saved to pointer (Binary Search Tree)

对于类,我们正在构建一个二进制搜索树。 我相信我应该能够自己完成项目的大部分工作(isPresent,评估等),但是在创建树本身时遇到了问题。 给定来自main的输入:

int main()
{
    BST *bst = new BST();
    bst->add(20, "*");
    bst->add(15, "-");
    bst->add(6, "+");
    bst->add(30, "/");
    bst->add(8, "2");
    bst->add(2, "4");
    bst->add(25, "14");
    bst->add(36, "7");
    bst->add(18, "3");

    cout << ((bst->isPresent(20)) ? "20 - Yes" : "20 - No") << endl;
    cout << ((bst->isPresent(8)) ? "8 - Yes" : "8 - No") << endl;
    cout << ((bst->isPresent(200)) ? "200 - Yes" : "200 - No") << endl;

然后,我们将使用他提供的BST.h,但是我们可以向其中添加信息(我已经添加了很多信息):

class BST
{
public:

    BST();
    void add(int key, string oper);
    bool isPresent(int key) const;

private:


    class Node
    {
    public:
        friend class BST;
        Node()
        {
            mLeft = mRight = nullptr;
            mKey = NULL;
            mOper = "NULL";
            mToken = 0;
        }

        Node(int key, string oper)
        {
            mLeft = mRight = nullptr;
            mKey = key;
            mOper = oper;
            mToken = 0;
        }
        bool find(int x);  // Delete if you don't want it.
        void insert(int key, string oper);

    private:
        Node *mLeft, *mRight;
        int mKey = 0;
        string mOper = "";
        double mToken = 0;
        Node *mRoot;
        Node *helper;
    };
};

在BST.cpp(可以以任何方式更改)中,

BST::BST()
{
    int key = 0;
    string oper = "NULL";
}


void BST::add(int key, string oper)
{
    Node useThis = Node();
    useThis.insert(key, oper);

}

bool BST::isPresent(int key) const
{
    BST useThis = BST();
    return useThis.find(key);
}

void BST::Node::insert(int key, string oper)
{
    if (mRoot == NULL)
    {
        mRoot = new Node();
        mRoot->mKey = key;
        mRoot->mOper = oper;
        mRoot->mToken;
        mRoot->mLeft = mRoot->mRight = NULL;
        cout << "Finished setting mRoot";
    }


    else
    {
        helper = new Node();
        helper->mKey = key;
        helper->mOper = oper;
        helper->mToken;
        helper->mLeft = mRoot->mRight = NULL;


        if (helper->mToken < mRoot->mToken)
        {
            if (mRoot->mLeft == NULL)
            {
                mRoot->mLeft = helper;
            }
            else
            {
                mRoot->insert(mRoot->mLeft->mKey, mRoot->mLeft->mOper);
            }
        }
        else
        {
            if (mRoot->mRight == NULL)
            {
                mRoot->mRight = helper;
            }
            else
            {
                mRoot->insert(mRoot->mRight->mKey, mRoot->mRight->mOper);
            }
        }
    }
}

仅输入了相关代码,我注意到每次添加时,似乎mRoot已被重置,并且代码继续尝试将mRoot设置为我每次发送的值(命中了if(mRoot == NULL)多次。 希望找出为什么我进入find方法时未保存为mRoot输入的值

在此功能

void BST::add(int key, string oper)
{
    Node useThis = Node();
    useThis.insert(key, oper);

}

每次创建新节点并在其上调用insert时。 在添加功能结束后,您所做的一切都会丢失。

考虑到您只想拥有一个根点,最好的选择是只创建一次该节点并记住它(或者,如果您确实需要拆分Node和BST,请再考虑一次)。 只需将其作为BST的私有参数,然后在其上调用insert。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM