Commit 46541b82 authored by Margatroid's avatar Margatroid
Browse files

Merge branch 'llrbt' of https://github.com/Enter-tainer/OI-wiki into llrbt

parents 9d5d23e4 97c7a3f3
Loading
Loading
Loading
Loading
+39 −34
Original line number Diff line number Diff line
@@ -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))
@@ -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 *
@@ -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
@@ -128,6 +130,7 @@
      }
      return root;
    }
    ```

    template<class Key, class Compare>
    typename Set<Key, Compare>::Node *
@@ -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);
@@ -187,6 +190,7 @@
        }
      }
      return fix_up(root);
    ```

    }
    ```
@@ -200,6 +204,7 @@
    #include <algorithm>
    #include <memory>
    #include <vector>
    ```

    template<class Key, class Compare = std::less<Key>>
    class Set {
@@ -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