繁体   English   中英

二进制搜索树段错误

[英]Binary Search Tree segfaults

我已经为此苦了一段时间。 我有一个BST,旨在在插入时遇到重复项时增加计数器。 这完全按设计工作。

树将丢弃此元素,并且树将保持不变。 但是,我想删除树中的原始元素,以便第三次插入重复元素不会增加计数器。 运行程序时出现段错误。 参见下面的代码。 GDB说segfault发生在InsertInto()方法中,ValGrind抱怨一堆我找不到的内存泄漏。

任何帮助将不胜感激。

struct BSTreeNode
{
    int mData;
    BSTreeNode *mLeft, *mRight;
    BSTreeNode(int i)
    {
        mData = i;
        mLeft = NULL;
        mRight = NULL;
    }
    ~BSTreeNode()
    {
        delete mLeft;
        delete mRight;
    }
};

class BSTree {
    private:
        BSTreeNode * mRoot;
        bool mWithDeletion;
    public:
        int mDupCounter;
        BSTree():mRoot(NULL),mDupCounter(0),mWithDeletion(false){}
        BSTree(bool _withDelete):mRoot(NULL),mDupCounter(0),mWithDeletion(_withDelete){}
        ~BSTree()
        {
            delete mRoot;
        }

        BSTreeNode * FindSmallest(BSTreeNode *& _node)
        {
            if (!_node->mLeft)
                return _node;
            else
                return FindSmallest(_node->mLeft);      
        }

        void DeleteNode(BSTreeNode *& _node)
        {
            if (!_node->mLeft && !_node->mRight) // no subtrees
            {
                delete _node;
                _node = NULL;
            }
            else if (_node->mLeft && !(_node->mRight)) // only left subtree
            {
                _node = _node->mLeft;
            }
            else if (_node->mRight && !(_node->mLeft)) // only right subtree
            {
                _node = _node->mRight;
            }
            else // both subtrees exist.
            {
                BSTreeNode * temp = FindSmallest(_node->mRight);
                _node->mData = temp->mData;                 
                DeleteNode(temp);
            }

        }

        void InsertInto(BSTreeNode *& _node, int _valueToInsert)
        {   
            if (!_node)
            {
                _node = new BSTreeNode(_valueToInsert);
            }
            else if (_valueToInsert < _node->mData)
            {
                InsertInto(_node->mLeft, _valueToInsert);           
            }
            else if (_valueToInsert > _node->mData)
            {
                InsertInto(_node->mRight, _valueToInsert);
            }
            else
            {
                cout << "DUPLICATE FOUND!" << endl;
                mDupCounter++;              
                // delete the node already in the tree if we must
                if (mWithDeletion)
                    DeleteNode(_node);              
            }
            cout << endl;
        }

        void InsertIntoRoot(int _valueToInsert)
        {
            InsertInto(mRoot, _valueToInsert);
        }
};

您确定要通过删除节点来避免计算第三个重复项吗? 即使正确设置了删除代码,您仍然可以算出第4,第6等重复项。 您会模糊地隐藏在没有节点的情况下发生重复的事实。

如果在每个BSTreeNode中显式存储一个计数器,则对您有所帮助。 与树中重复的非平凡的删除/插入相比,我确信开销可以忽略不计。

    else
    {
      cout << "DUPLICATE FOUND!" << endl;
        _node.DupCounter++;                         
    }

如果不是为了性能,我什至会忽略全局mDupCounter并遍历树以分析结果。 并且,如果需求发生变化,您可以有更多选择来计算不同的数字,例如频率的直方图或其他任何形式。

暂无
暂无

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

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