97% rbtree_rebalance

This commit is contained in:
Henrique Gemignani Passos Lima 2022-10-26 01:55:04 +03:00
parent cb11fd2320
commit 1bb8002205
No known key found for this signature in database
GPG Key ID: E224F951761145F8
1 changed files with 13 additions and 14 deletions

View File

@ -70,40 +70,39 @@ void rbtree_rotate_right(void* header_void, void* node_void) {
} }
void rbtree_rebalance(void* header_void, void* node_void) { void rbtree_rebalance(void* header_void, void* node_void) {
_header* header = static_cast< _header* >(header_void);
_node* node = static_cast< _node* >(node_void); _node* node = static_cast< _node* >(node_void);
_node* it; _header* header = static_cast< _header* >(header_void);
while ((it = node->mParent) != nullptr && it->mColor == kNC_Black) { while (node->mParent != nullptr && node->mParent->mColor == kNC_Black) {
_node* p = it->mParent->mLeft; _node* p = node->mParent->mParent->mLeft;
if (it == p) { if (node->mParent == p) {
p = it->mParent->mRight; p = node->mParent->mParent->mRight;
if ((p != nullptr && p->mColor == kNC_Black)) { if ((p != nullptr && p->mColor == kNC_Black)) {
it->mColor = kNC_Red; node->mParent->mColor = kNC_Red;
p->mColor = kNC_Red; p->mColor = kNC_Red;
node->mParent->mParent->mColor = kNC_Black; node->mParent->mParent->mColor = kNC_Black;
node = node->mParent->mParent; node = node->mParent->mParent;
} else { } else {
if (node == it->mRight) { if (node == node->mParent->mRight) {
rbtree_rotate_left(header, it); rbtree_rotate_left(header, node->mParent);
} }
node = it; // node = node->mParent;
node->mParent->mColor = kNC_Red; node->mParent->mColor = kNC_Red;
node->mParent->mParent->mColor = kNC_Black; node->mParent->mParent->mColor = kNC_Black;
rbtree_rotate_right(header, node->mParent->mParent); rbtree_rotate_right(header, node->mParent->mParent);
} }
} else if (p != nullptr && p->mColor == kNC_Black) { } else if (p != nullptr && p->mColor == kNC_Black) {
it->mColor = kNC_Red; node->mParent->mColor = kNC_Red;
p->mColor = kNC_Red; p->mColor = kNC_Red;
node->mParent->mParent->mColor = kNC_Black; node->mParent->mParent->mColor = kNC_Black;
node = node->mParent->mParent; node = node->mParent->mParent;
} else { } else {
if (node == it->mLeft) { if (node == node->mParent->mLeft) {
rbtree_rotate_right(header, it); rbtree_rotate_right(header, node->mParent);
} }
node = it; // node = node->mParent;
node->mParent->mColor = kNC_Red; node->mParent->mColor = kNC_Red;
node->mParent->mParent->mColor = kNC_Black; node->mParent->mParent->mColor = kNC_Black;
rbtree_rotate_left(header, node->mParent->mParent); rbtree_rotate_left(header, node->mParent->mParent);