diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-02-09 16:33:44 +0330 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-09 20:57:41 +0000 |
commit | e7dea10381578628e492fa2d6dc6b77e9821f306 (patch) | |
tree | ac02112bc357c97270044ec10b5eda47022170a7 /AK/RedBlackTree.h | |
parent | 9ff22ac7e04b40b5cfdd7920d798e9c953c1d2c8 (diff) | |
download | serenity-e7dea10381578628e492fa2d6dc6b77e9821f306.zip |
AK: Add RBTree::find_smallest_above_iterator(Key)
Diffstat (limited to 'AK/RedBlackTree.h')
-rw-r--r-- | AK/RedBlackTree.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/AK/RedBlackTree.h b/AK/RedBlackTree.h index 248b257df4..acdc2b9ed4 100644 --- a/AK/RedBlackTree.h +++ b/AK/RedBlackTree.h @@ -131,6 +131,20 @@ protected: return candidate; } + static Node* find_smallest_not_below(Node* node, K key) + { + while (node) { + if (node->key >= key && (!node->left_child || node->left_child->key < key)) + return node; + + if (node->key <= key) + node = node->right_child; + else + node = node->left_child; + } + return node; + } + void insert(Node* node) { VERIFY(node); @@ -493,6 +507,14 @@ public: return ConstIterator(node, static_cast<Node*>(BaseTree::predecessor(node))); } + ConstIterator find_smallest_not_below_iterator(K key) const + { + auto node = static_cast<Node*>(BaseTree::find_smallest_not_below(this->m_root, key)); + if (!node) + return end(); + return ConstIterator(node, static_cast<Node*>(BaseTree::predecessor(node))); + } + V unsafe_remove(K key) { auto* node = BaseTree::find(this->m_root, key); |