Loading docs/ds/llrbt.md +39 −34 Original line number Diff line number Diff line Loading @@ -60,11 +60,12 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::fix_up(Set::Node *root) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::fix_up( Set::Node *root) const { if (is_red(root->rc) && !is_red(root->lc)) // fix right leaned red link root = rotate_left(root); if (is_red(root->lc) && is_red(root->lc->lc)) // fix doubly linked left leaned red link if (is_red(root->lc) && is_red(root->lc->lc)) // fix doubly linked left leaned red link // if (root->lc == nullptr), then the second expr won't be evaluated root = rotate_right(root); if (is_red(root->lc) && is_red(root->rc)) Loading @@ -73,6 +74,7 @@ root->size = size(root->lc) + size(root->rc) + 1; return root; } ``` template<class Key, class Compare> typename Set<Key, Compare>::Node * Loading Loading @@ -117,8 +119,8 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::move_red_left(Set::Node *root) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::move_red_left( Set::Node *root) const { color_flip(root); if (is_red(root->rc->lc)) { // assume that root->rc != nullptr when calling this function Loading @@ -128,6 +130,7 @@ } return root; } ``` template<class Key, class Compare> typename Set<Key, Compare>::Node * Loading Loading @@ -160,25 +163,25 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::delete_arbitrary(Set::Node *root, Key key) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::delete_arbitrary( Set::Node *root, Key key) const { if (cmp_(key, root->key)) { // key < root->key if (!is_red(root->lc) && !(is_red(root->lc->lc))) root = move_red_left(root); // ensure the invariant: either root->lc or root->lc->lc (or root and root->lc after dive into the function) is red, // to ensure we will eventually delete a red node. therefore we will not break the black height balance // ensure the invariant: either root->lc or root->lc->lc (or root and // root->lc after dive into the function) is red, to ensure we will // eventually delete a red node. therefore we will not break the black // height balance root->lc = delete_arbitrary(root->lc, key); } else { // key >= root->key if (is_red(root->lc)) root = rotate_right(root); if (is_red(root->lc)) root = rotate_right(root); if (key == root->key && root->rc == nullptr) { delete root; return nullptr; } if (!is_red(root->rc) && !is_red(root->rc->lc)) root = move_red_right(root); if (!is_red(root->rc) && !is_red(root->rc->lc)) root = move_red_right(root); if (key == root->key) { root->key = get_min(root->rc); root->rc = delete_min(root->rc); Loading @@ -187,6 +190,7 @@ } } return fix_up(root); ``` } ``` Loading @@ -200,6 +204,7 @@ #include <algorithm> #include <memory> #include <vector> ``` template<class Key, class Compare = std::less<Key>> class Set { Loading Loading @@ -532,4 +537,4 @@ ## 参考资料与拓展阅读 - [Left-Leaning Red-Black Trees](https://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf) - Robert Sedgewick Princeton University - [Balanced Search Trees]( https://algs4.cs.princeton.edu/lectures/33BalancedSearchTrees-2x2.pdf ) - *Algorithms* Robert Sedgewick | Kevin Wayne - [Balanced Search Trees](https://algs4.cs.princeton.edu/lectures/33BalancedSearchTrees-2x2.pdf) -_Algorithms_Robert Sedgewick | Kevin Wayne Loading
docs/ds/llrbt.md +39 −34 Original line number Diff line number Diff line Loading @@ -60,11 +60,12 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::fix_up(Set::Node *root) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::fix_up( Set::Node *root) const { if (is_red(root->rc) && !is_red(root->lc)) // fix right leaned red link root = rotate_left(root); if (is_red(root->lc) && is_red(root->lc->lc)) // fix doubly linked left leaned red link if (is_red(root->lc) && is_red(root->lc->lc)) // fix doubly linked left leaned red link // if (root->lc == nullptr), then the second expr won't be evaluated root = rotate_right(root); if (is_red(root->lc) && is_red(root->rc)) Loading @@ -73,6 +74,7 @@ root->size = size(root->lc) + size(root->rc) + 1; return root; } ``` template<class Key, class Compare> typename Set<Key, Compare>::Node * Loading Loading @@ -117,8 +119,8 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::move_red_left(Set::Node *root) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::move_red_left( Set::Node *root) const { color_flip(root); if (is_red(root->rc->lc)) { // assume that root->rc != nullptr when calling this function Loading @@ -128,6 +130,7 @@ } return root; } ``` template<class Key, class Compare> typename Set<Key, Compare>::Node * Loading Loading @@ -160,25 +163,25 @@ ??? note "参考代码(部分)" ```cpp template <class Key, class Compare> typename Set<Key, Compare>::Node * Set<Key, Compare>::delete_arbitrary(Set::Node *root, Key key) const { typename Set<Key, Compare>::Node *Set<Key, Compare>::delete_arbitrary( Set::Node *root, Key key) const { if (cmp_(key, root->key)) { // key < root->key if (!is_red(root->lc) && !(is_red(root->lc->lc))) root = move_red_left(root); // ensure the invariant: either root->lc or root->lc->lc (or root and root->lc after dive into the function) is red, // to ensure we will eventually delete a red node. therefore we will not break the black height balance // ensure the invariant: either root->lc or root->lc->lc (or root and // root->lc after dive into the function) is red, to ensure we will // eventually delete a red node. therefore we will not break the black // height balance root->lc = delete_arbitrary(root->lc, key); } else { // key >= root->key if (is_red(root->lc)) root = rotate_right(root); if (is_red(root->lc)) root = rotate_right(root); if (key == root->key && root->rc == nullptr) { delete root; return nullptr; } if (!is_red(root->rc) && !is_red(root->rc->lc)) root = move_red_right(root); if (!is_red(root->rc) && !is_red(root->rc->lc)) root = move_red_right(root); if (key == root->key) { root->key = get_min(root->rc); root->rc = delete_min(root->rc); Loading @@ -187,6 +190,7 @@ } } return fix_up(root); ``` } ``` Loading @@ -200,6 +204,7 @@ #include <algorithm> #include <memory> #include <vector> ``` template<class Key, class Compare = std::less<Key>> class Set { Loading Loading @@ -532,4 +537,4 @@ ## 参考资料与拓展阅读 - [Left-Leaning Red-Black Trees](https://www.cs.princeton.edu/~rs/talks/LLRB/RedBlack.pdf) - Robert Sedgewick Princeton University - [Balanced Search Trees]( https://algs4.cs.princeton.edu/lectures/33BalancedSearchTrees-2x2.pdf ) - *Algorithms* Robert Sedgewick | Kevin Wayne - [Balanced Search Trees](https://algs4.cs.princeton.edu/lectures/33BalancedSearchTrees-2x2.pdf) -_Algorithms_Robert Sedgewick | Kevin Wayne