mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 09:37:44 +00:00
AK: Fix RedBlackTree::find_smallest_not_below_iterator
Before this was incorrectly assuming that if the current node `n` was at least the key and the left child of `n` was below the key that `n` was always correct. However, the right child(ren) of the left child of `n` could still be at least the key. Also added some tests which produced the wrong results before this.
This commit is contained in:
parent
41dae9b3c7
commit
2bddf157b1
2 changed files with 49 additions and 4 deletions
|
@ -133,16 +133,19 @@ protected:
|
|||
|
||||
static Node* find_smallest_not_below(Node* node, K key)
|
||||
{
|
||||
Node* candidate = nullptr;
|
||||
while (node) {
|
||||
if (node->key >= key && (!node->left_child || node->left_child->key < key))
|
||||
if (node->key == key)
|
||||
return node;
|
||||
|
||||
if (node->key <= key)
|
||||
if (node->key <= key) {
|
||||
node = node->right_child;
|
||||
else
|
||||
} else {
|
||||
candidate = node;
|
||||
node = node->left_child;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
void insert(Node* node)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue