8000 BST deletion bug · Issue #4 · alrightchiu/SecondRound · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

BST deletion bug #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
phlinhng opened this issue Nov 1, 2018 · 0 comments
Open

BST deletion bug #4

phlinhng opened this issue Nov 1, 2018 · 0 comments

Comments

@phlinhng
Copy link
phlinhng commented Nov 1, 2018

您好,我正在學習Data Structure。您的blog文章對我幫助很大,獲益良多。
當參考您的代碼實現Binary Search Tree的時候,發現這段代碼有一點小bug。
[Code](SecondRound/content/Algorithms and Data Structures/Tree series/ExampleCode/BST_Delete.cpp)

當BST中只剩下root的時候 x = y->rightchild;會導致x指向NIL
這時this->root = x;就會出錯,造成後續操作找不到root

解決辦法是將這段代碼

 if (y->parent == NULL){                // 若刪除的是原先的root, 就把x當成新的root 
        this->root = x;
    }

改為

if (y->parent == NULL){                // 若刪除的是原先的root, 就把x當成新的root 
        if(x) this->root = x;
        else this->root=0;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0