0

有人可以验证并告诉我以下代码是否有效吗?我觉得第 160-162 行可能是错误的。
我有评论指出行号。
完整的代码取自这里C++ Binary Search tree

class BinarySearchTree
{
    private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           int data;
        };
        tree_node* root;

    public:
        BinarySearchTree()
        {
           root = NULL;
        }

        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void print_preorder();
        void preorder(tree_node*);
        void print_postorder();
        void postorder(tree_node*);
        void insert(int);
        void remove(int);
};


void BinarySearchTree::remove(int d)
{
    //Locate the element
    bool found = false;
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }

    tree_node* curr;
    tree_node* parent;
    curr = root;

    while(curr != NULL)
    {
         if(curr->data == d)
         {
            found = true;
            break;
         }
         else
         {
             parent = curr;
             if(d>curr->data) curr = curr->right;
             else curr = curr->left;
         }
    }
    if(!found)
         {
        cout<<" Data not found! "<<endl;
        return;
    }


         // 3 cases :
    // 1. We're removing a leaf node
    // 2. We're removing a node with a single child
    // 3. we're removing a node with 2 children

    // Node with single child
    if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
&& curr->right == NULL))
    {
       if(curr->left == NULL && curr->right != NULL)
       {
           if(parent->left == curr)
           {
             parent->left = curr->right;
             delete curr;
           }
           else
           {
             parent->right = curr->right;
             delete curr;
           }
       }
       else // left child present, no right child
       {
          if(parent->left == curr)
           {
             parent->left = curr->left;
             delete curr;
           }
           else
           {
             parent->right = curr->left;
             delete curr;
           }
       }
     return;
    }

         //We're looking at a leaf node
         if( curr->left == NULL && curr->right == NULL)
    {
        if(parent->left == curr) parent->left = NULL;
        else parent->right = NULL;
                 delete curr;
                 return;
    }


    //Node with 2 children
    // replace node with smallest value in right subtree
    if (curr->left != NULL && curr->right != NULL)
    {
        tree_node* chkr;
        chkr = curr->right;
        if((chkr->left == NULL) && (chkr->right == NULL))
        {
            curr = chkr;  // <----------- line 160
            delete chkr;
            curr->right = NULL; // <------------------ line 162
        }
        else // right child has children
        {
            //if the node's right child has a left child
            // Move all the way down left to locate smallest element

            if((curr->right)->left != NULL)
            {
                tree_node* lcurr;
                tree_node* lcurrp;
                lcurrp = curr->right;
                lcurr = (curr->right)->left;
                while(lcurr->left != NULL)
                {
                   lcurrp = lcurr;
                   lcurr = lcurr->left;
                }
        curr->data = lcurr->data;
                delete lcurr;
                lcurrp->left = NULL;
           }
           else
           {
               tree_node* tmp;
               tmp = curr->right;
               curr->data = tmp->data;
           curr->right = tmp->right;
               delete tmp;
           }

        }
         return;
    }

}

currchkr指向同一个位置。通过删除chkr , curr仍然可以访问相同的位置吗?或者这没关系,因为它们实际上都没有使用 new 语句分配任何内存。

代码确实有些狡猾。我觉得也有内存泄漏。我是一名在职专业人士,希望更新我的 C++ 基础知识。感谢您的任何帮助。

4

2 回答 2

1

我看了一眼你提到的区域周围的代码。我相信你是正确的,因为这是一个错误。

void BinarySearchTree::remove(int d)
{

...

    tree_node* curr;
    tree_node* parent;
    curr = root;

...    

    //Node with 2 children
    // replace node with smallest value in right subtree
    if (curr->left != NULL && curr->right != NULL)
    {
        tree_node* chkr;
        chkr = curr->right;
        if((chkr->left == NULL) && (chkr->right == NULL))
        {
            curr = chkr;
            delete chkr;
            curr->right = NULL;
        }

在此代码部分中,curr 和 chkr 都被声明为指向 tree_node 实例的指针。在运行curr = chkr时,chkr 的指针值被写入 curr,因此将 curr 指向 chkr 指向的实例。通过delete chkr,实例被销毁并被垃圾收集。因此 curr 现在指向一个不存在的对象,该对象已不存在。如果我没记错的话,根据定义,这是一个悬空指针。

如果我在上面都是正确的并且对该特定块的理解是正确的,那么以下是解决此问题的方法:

curr->data = chkr->data;

取代

       curr = chkr;

关于内存泄漏。抱歉,我没有阅读整个代码。看来 msram 的代码应该仅用于显示正确的逻辑。不过,为此目的,这是更多的额外信息。

于 2014-01-03T15:22:56.667 回答
0

是的,第 160-162 行包含您所描述的错误。他正在写他已经释放的内存delete

于 2014-01-03T15:23:13.683 回答