When you are coding a red black tree and debug granpa!= nullptr even though it is true, the entire phrase is skipped. I stopped it in debugging mode and checked it by pressing f11, but I only ran the line with if and else, and ignored all the codes inside. What's wrong with this?
if (granpa != nullptr) {
if (granpa->left == parent) {
granpa->left == n;
}
else if (granpa->right == parent) {
granpa->right == n;
}
}
else {
root = n;
}
If you look at the code given... "granpa!=nullptr
is correct, but granpa->left
and granpa->right
may not be parent
. (I don't know what it is.) It's very normal to do nothing at this time.
Make a else
block under else if
and do something.
© 2024 OneMinuteCode. All rights reserved.