14   	
 




2011   7   8
OK



                    (^^)b




2011   7   8
14
               14.1              O(n)




                           (i   ) O(lgn)


               14.2

                    14.1


               14.3




2011   7   8
3                                                              size
               1


                            14.1 Dynamic order statistics                                                     303
                                Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shade
                                and darkened nodes are black. In addition to its usual fields, each node x has a field
                                                                    26
                                the number of nodes in the subtree rooted at x.
                                                                    20
                                                17                                               41
                                                12                                               7
                                 14                            21                    30                       47
                       10
                                  7 A data structure that can support fast5 order-statistic operations is
                                          16        19
                                                               4
                                                                      21       28                38
                                                                                                                 1

                       4        ure 14.1. An order-statistic tree T is simply a 3red-black tree with ad
                                           2         2                 1        1
                   7        12        14                    20    key                        35     39
                   2         1 mation stored in each node. Besides the usual red-black tree fields key
                                       1                     1                                1      1
               3                                                  size
               1                p[x], left[x], and right[x] in a node x, we have another field size[x]. T
                                tains the number of (internal) nodes in the subtree rooted at x (inclu
                            Figure 14.1 An order-statistic the which is an augmented red-blackthe sentinel’s are red,to be 0,
                                that is, the size of tree, subtree. If we define tree. Shaded nodes size
                            and darkened nodes ]] to beIn addition towe have the identity a field size[x], which is
                                size[nil[T are black. 0, then its usual fields, each node x has
                            the number of nodes in the subtree rooted at x.

                                size[x] = size[left[x]] + size[right[x]] + 1 .
                               A data structure that can support fast order-statistic operations is shown in Fig-
                            ure 14.1. An order-statistic tree T is to be distinct in anwith additional infor-
                                    We do not require keys simply a red-black tree order-statistic tree. (For
                            mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
                            p[x], left[x],Figure 14.1 has twowe havewith value size[x]. This field con-with val
                                tree in and right[x] in a node x, keys another field 14 and two keys
                            tains the number ofequal keys, the the subtree rooted of xrank is not itself), defined
                                presence of (internal) nodes in above notion at (including x well
                            thatthis ambiguity for an order-statistic tree by defining the we set of an e
                                 is, the size of the subtree. If we define the sentinel’s size to be 0, that is, rank
                            size[nil[T ]] to be 0, then we have the identity
                                position at which it would be printed in an inorder walk of the tree. I
               O(n)              O(lgn)
                            size[x] = size[left[x]] + size[right[x]] + 1 .in a black node has rank 5, and the ke
                                for example, the key 14 stored
2011   7   8
                               Werednot require keys to be distinct in an order-statistic tree. (For example, the
                                a do node has rank 6.
i

                           17
                                14.1 Dynamic order statistics                                                                     303
                                                                                      17
                                                                                      26
                                                                                      20
                                                    17                                                            41
                                                     12                                                            7
                                     14                               21                           30                            47
                                      7                               4                            5                              1
                           10                  16         19                     21        28                38
                           4                    2         2                       1        1                 3
                       7        12        14                     20        key                          35        39
                       2         1         1                      1                                     1          1
                   3                                                       size
                   1




                                Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                the number of nodes in the subtree rooted at x.


                                  A data structure that can support fast order-statistic operations is shown in Fig-
                                ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                    mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17
                                                                                       26
                                                     4                                 20
                                                         17                                                        41
                                                         12                                                         7
                                      14                               21                           30                            47
                                       7                                4                           5                              1
                            10                  16            19                  21        28                38
                            4                    2            2                    1        1                 3
                        7        12        14                      20       key                          35        39
                        2         1         1                      1                                     1          1
                   3                                                        size
                   1




                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17                      4
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                   4
                                                         17                                                        41
                                                         12                                                         7
                                      14                               21                           30                            47
                                       7                                4                           5                              1
                            10                  16            19                  21        28                38
                            4                    2            2                    1        1                 3
                        7        12        14                      20       key                          35        39
                        2         1         1                      1                                     1          1
                   3                                                        size
                   1




                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17                      4
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                   4
                                                         17                                                        41
                                                         12                                                         7
                                      14                               21                           30                            47
                                       7                                4                           5                              1
                            10                  16            19                  21        28                38
                            4                    2            2                    1        1                 3
                        7        12        14                      20       key                          35        39
                        2         1         1                      1                                     1          1
                   3                                                        size
                   1




                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17                      4
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                    4
                                                         17                                        4                41
                                                         12                                                         7
                                      14                               21                           30                            47
                                       7                                4                             5                            1
                            10                  16            19                  21        28                 38
                            4                    2            2                    1        1                  3
                        7        12        14                      20       key                           35        39
                        2         1         1                      1                                      1         1
                   3                                                        size
                   1
                                                                                                  4

                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17                      4
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                    4
                                                         17                                        4                41
                                                         12                                                         7
                                      14
                                       7
                                                                       21
                                                                        4
                                                                                                    30
                                                                                                      5
                                                                                                               2                  47
                                                                                                                                   1
                            10                  16            19                  21        28                 38
                            4                    2            2                    1        1                  3
                        7        12        14                      20       key                           35        39
                        2         1         1                      1                                      1         1
                   3                                                        size
                   1
                                                                                                  4

                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
i

                            17
                                 14.1 Dynamic order statistics                                                                     303

                   12                                                                  17                      4
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                    4
                                                         17                                        4                41
                                                         12                                                         7
                                      14
                                       7
                                                                       21
                                                                        4
                                                                                                    30
                                                                                                      5
                                                                                                               2                  47
                                                                                                                                   1
                            10                  16            19                  21        28                 38
                            4                    2            2                    1        1                  3
                        7        12        14                      20       key                           35        39
                        2         1         1                      1                                      1         1
                   3                                                        size
                   1
                                                                                                  4

                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                                 and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.


                                   A data structure that can support fast order-statistic operations is shown in Fig-
                                 ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
14.1 Dynamic order statistics                                                                     303

                   12                                                                  17
                                                     4
                                                                                       26
                                                                                       20
                                                                                                                   4
                                                         17                                        4               41
                                                         12                                                         7
                                      14
                                       7
                                                                     21
                                                                        4
                                                                                                    30
                                                                                                    5
                                                                                                              2                   47
                                                                                                                                   1
                            10                  16            19                  21        28                38
                            4                    2            2                   1         1                 3
                        7        12        14                      20       key                          35        39
               Chapter 14 Augmenting Data Structures
                   2    1   1         1
                                          size
                                                                                                         1          1
                   3
                   1                                                                              4

                                 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
               OS-S ELECT (x, i) and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                                 the number of nodes in the subtree rooted at x.
               1 r ← size[left[x]]+1support fast order-statistic operations is shown in Fig-
                          A data structure that can
               2 if i = r An order-statistic tree T is simply a red-black tree with additional infor-
                       ure 14.1.
                       mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
               3     thenleft[x], and right[x] in a node x, we have another field size[x]. This field con-
                       p[x], return x
                       tains the number of (internal) nodes in the subtree rooted at x (including x itself),
               4 elseif iis,< size of the subtree. If we define the sentinel’s size to be 0, that is, we set
                       that the r
                       size[nil[T ]] to be 0, then we have the identity
               5     then return OS-S ELECT (left[x], i)
                       size[x] = size[left[x]] + size[right[x]] + 1 .
               6 else return require keysELECT (right[x], i tree. (For example, the
                          We do not
                                       OS-S to be distinct in an order-statistic − r)
2011   7   8
:38
                             14.1 Dynamic order statistics                                                                     303


                                                                                   26
                                                                                   20
                                                 17                                                            41
                                                  12                                                            7
                                  14                               21                           30                            47
                                   7                               4                            5                              1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303


                                                                                   26
                                                                                   20
                                                 17                                                            41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303


                                                                                   26
                                                                                   20
                                                 17                                                            41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303


                                                                                   26
                                                                                   20
                                                 17                                            4               41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303
                                                                                                                    =
                                                                                   26
                                                                                   20
                                                                                                               4
                                                 17                                            4               41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303
                                                                                   17                               =
                                                                                   26
                                                                                   20
                                                                                                               4
                                                 17                                            4               41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
:38
                             14.1 Dynamic order statistics                                                                     303
                                                                                   17                               =
                                                                                   26
                                                                                   20
                                                                                                               4
                                                 17                                            4               41
                                                  12                                                            7
                                  14
                                   7
                                                                   21
                                                                   4
                                                                                                30
                                                                                                5
                                                                                                          2                   47
                                                                                                                               1
                        10                  16         19                     21        28                38
                         4                   2         2                       1        1                 3
                    7        12        14                     20        key                          35        39
                    2         1         1                      1                                     1          1
               3                                                        size
               1




                             Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                             and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
                             the number of nodes in the subtree rooted at x.


                               A data structure that can support fast order-statistic operations is shown in Fig-
                             ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
2011   7   8                 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
14.1 Dynamic order statistics                                                                 303
                                                                                  17                             =
                                                                                  26
                                                                                  20
                                                                                                             4
                                                  17                                         4               41
                                                  12                                                         7
                                   14
                                   7
                                                                  21
                                                                   4
                                                                                              30
                                                                                              5
                                                                                                        2                  47
                                                                                                                            1
                         10                  16        19                    21        28               38
                         4                   2         2                      1        1                3
                     7        12        14                    20       key                         35        39
                     2         1        1                     1                                    1         1
                 3                                                     size
                 1
               14.1 Dynamic order statistics

                          Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red,
                          and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is
               OS-R      (T, x)
                         ANKnumber of nodes in the subtree rooted at x.
                          the

               1 r ← size[left[x]] that 1 support fast order-statistic operations is shown in Fig-
                        A data structure + can
               2 y ←ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor-
                       x
                     mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
               3 while y = root[T ] in a node x, we have another field size[x]. This field con-
                      p[x], left[x], and right[x]
               4     do if y = right[ p[y]] in the subtree rooted at x (including x itself),
                     tains the number of (internal) nodes
                     that is, the size of the subtree. If we define the sentinel’s size to be 0, that is, we set
               5               then r then we have the identity
                     size[nil[T ]] to be 0,← r + size[left[ p[y]]] + 1
               6           y ← p[y]
                     size[x] = size[left[x]] + size[right[x]] + 1 .
               7 returnWe do not require keys to be distinct in an order-statistic tree. (For example, the
                          r
2011   7   8
naught. We shall now show that subtree sizes can be maintained
                        and deletion without affecting the asymptotic running time of eit
                            We noted in Section 13.3 that insertion into a red-black tre
                        phases. The first phase goes down the tree from the root, inser
                        as a child of an existing node. The second phase  ( goes up the tree
                 )      and ultimately performing rotations to maintain the red-black pr
                            To maintain the subtree sizes in the first phase, we simply inc
           Chapter 14 Augmenting Data Structures traversed from the root down toward th
                        each node x on the path
                        node added gets a size of 1. Since there are O(lg n) nodes on t
                        the additional cost of maintaining the size fields is O(lg n).
                                          LEFT-ROTATE(T, x)
                     42 In the second phase, the only structural changes to the underly
                                                                             93
                     19
                          x                                                  19
                                                                                y
                        are caused by rotations, of which there are at most two. Moreo
                               93                                   42
                                  y                                      x
                        local operation: RIGHT-ROTATE(T, y) have their size fields invalidated
                               12         only two nodes             11
            6                                                                         7
                        which the rotation is performed is incident on these two nodes
                        code for L7EFT-ROTATE (T, x) in Section 13.2,4 we add the follow
                            4                                    6


                         13 size[y] ← size[x]
           Figure 14.2   14 size[x] ← size[left[x]] + size[right[x]] + which the rotation is
                         Updating subtree sizes during rotations. The link around 1
           formed is incident on the two nodes whose size fields need to be updated. The updates are lo
           requiring only the size information stored in x, y, and the roots of the subtrees shown as triangle
                                                                        O(1)
                         Figure 14.2 illustrates how the fields are updated. The change t
2011   7   8             is symmetric.
14
               14.1              O(n)




                           (i   ) O(lgn)


               14.2

                    14.1


               14.3




2011   7   8
2011   7   8
size   (   )




               size



                          /
2011   7   8
14
               14.1              O(n)




                           (i   ) O(lgn)


               14.2

                    14.1


               14.3




2011   7   8
Chapter 14 Augmenting Data Structures


                           i                     i                       i                        i
                           i′                    i′                      i′                       i′


                                                            (a)


                       i                  i′                                     i′                i


                                 (b)                                                    (c)


               Figure 14.3 The interval trichotomy for two closed intervals i and i . (a) If i and i overlap, there
               are four situations; in each, low[i] ≤ high[i ] and low[i ] ≤ high[i]. (b) The intervals do not overlap,
               and high[i] < low[i ]. (c) The intervals do not overlap, and high[i ] < low[i].

2011   7   8
16                  21
                                                                        15                          23
                                                      8       9
                                                  6               10
                                              5           8
                              0         3
                                 0            5               10          15             20               25        30
                                                                          (a)                  (               ) int[x]

                                                                        [16,21]
                                                                          30

                                                                                                                    int
                                            [8,9]                                                       [25,30]
                                              23                                                          30        max

                           [5,8]                              [15,23]             [17,19]                           [26,26]
                            10                                     23               20                                    26


                [0,3]                [6,10]                                                   [19,20]
                  3                    10                                                          20
                                                                          (b)


               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
               (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted
               order by left endpoint.


               Step 3: Maintaining the information
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8
16                  21
                                                                        15                          23
                                                      8       9
                                                  6               10
                                              5           8
                              0         3
                                 0            5               10          15             20               25        30
                                                                          (a)                  (               ) int[x]

                                                                        [16,21]
                                                                          30

                                                                                                                    int
                                            [8,9]                                                       [25,30]
                                              23                                                          30        max

                           [5,8]                              [15,23]             [17,19]                           [26,26]
                            10                                     23               20                                    26


                [0,3]                [6,10]                                                   [19,20]
                  3                    10                                                          20
                                                                          (b)


               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
               (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted
               order by left endpoint.


               Step 3: Maintaining the information
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8
16                  21
                                                                        15                          23
                                                      8       9
                                                  6               10
                                              5           8
                              0         3
                                 0            5               10          15             20               25        30
                                                                          (a)                  (               ) int[x]

                                                                        [16,21]
                                                                          30

                                                                                                                    int
                                            [8,9]                                                       [25,30]
                                              23                                                          30        max

                           [5,8]                              [15,23]             [17,19]                           [26,26]
                            10                                     23               20                                    26


                [0,3]                [6,10]                                                   [19,20]
                  3                    10                                                          20
                                                                          (b)


               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
               (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted
               order by left endpoint.


               Step 3: Maintaining the information
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8
16                  21
                                                                        15                          23
                                                      8       9
                                                  6               10
                                              5           8
                              0         3
                                 0            5               10          15             20               25        30
                                                                          (a)                  (               ) int[x]

                                                                        [16,21]
                                                                          30

                                                                                                                    int
                                            [8,9]                                                       [25,30]
                                              23                                                          30        max

                           [5,8]                              [15,23]             [17,19]                           [26,26]
                            10                                     23               20                                    26


                [0,3]                [6,10]                                                   [19,20]
                  3                    10                                                          20
                                                                          (b)


               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
               (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted
               order by left endpoint.


               Step 3: Maintaining the information
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8
O(lgn)



               14.1            14.2-4 14.3-1




2011   7   8
19     20
                                                                              17        19
                                                                        16                   21
                                                                      15                           23
                                                     8       9
                                                 6               10
                                            5            8
                              0         3
                               0             5               10         15              20               25      30
                                                                        (a)

                                                                                    :          [22 25]
                                                                      [16,21]
  314             Chapter 14 Augmenting Data Structures
                                                      30

                                                                                                                 int
                                            [8,9]                                                      [25,30]
                  Step 4: Developing new operations
                                  23                                                                     30      max


                 The only new operation we need is I NTERVAL -S EARCH (T,[26,26]
                         [5,8]                [15,23]        [17,19]             i), which finds a node
                          10                    23              20                26
                 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in
                [0,3] tree, a pointer to the sentinel nil[T ] is returned.
                 the            [6,10]                               [19,20]
                  3                   10                                                          20
                                                                        (b)
                  I NTERVAL -S EARCH (T, i)
                  1 x ← root[T ]
               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
                   The while x that represents them. does not overlap int[x]
               (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted
                               do if
               order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i]
                  3
                  4                    then x ← left[x]
               Step 3: Maintaining the information
                  5                    else x ← right[x]
                  6 return x
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8   max values of node x’s children: that overlaps i starts with x at the root
                  The search for an interval                                                                           of the tree and
19      20
                                                                              17         19
                                                                        16                    21
                                                                      15                            23
                                                     8       9
                                                 6               10
                                            5            8
                              0         3
                               0             5               10         15               20               25      30
                                                                        (a)

                                                                                     :          [22 25]
                                                                                   [22 25]
                                                                      [16,21]
  314             Chapter 14 Augmenting Data Structures
                                                      30

                                                                                                                  int
                                            [8,9]                                                       [25,30]
                  Step 4: Developing new operations
                                  23                                                                      30      max


                 The only new operation we need is I NTERVAL -S EARCH (T,[26,26]
                         [5,8]                [15,23]        [17,19]             i), which finds a node
                          10                    23              20                26
                 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in
                [0,3] tree, a pointer to the sentinel nil[T ] is returned.
                 the            [6,10]                               [19,20]
                  3                   10                                                           20
                                                                        (b)
                  I NTERVAL -S EARCH (T, i)
                  1 x ← root[T ]
               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
                   The while x that represents them. does not overlap int[x]
               (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted
                               do if
               order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i]
                  3
                  4                    then x ← left[x]
               Step 3: Maintaining the information
                  5                    else x ← right[x]
                  6 return x
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8   max values of node x’s children: that overlaps i starts with x at the root
                  The search for an interval                                                                            of the tree and
19      20
                                                                              17         19
                                                                        16                    21
                                                                      15                            23
                                                     8       9
                                                 6               10
                                            5            8
                              0         3
                               0             5               10         15               20               25      30
                                                                        (a)

                                                                                     :          [22 25]
                                                                                   [22 25]
                                                                      [16,21]
  314             Chapter 14 Augmenting Data Structures
                                                      30

                                                     [22 25]                                                      int
                                            [8,9]                                                       [25,30]
                  Step 4: Developing new operations
                                  23                                                                      30      max


                 The only new operation we need is I NTERVAL -S EARCH (T,[26,26]
                         [5,8]                [15,23]        [17,19]             i), which finds a node
                          10                    23              20                26
                 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in
                [0,3] tree, a pointer to the sentinel nil[T ] is returned.
                 the            [6,10]                               [19,20]
                  3                   10                                                           20
                                                                        (b)
                  I NTERVAL -S EARCH (T, i)
                  1 x ← root[T ]
               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
                   The while x that represents them. does not overlap int[x]
               (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted
                               do if
               order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i]
                  3
                  4                    then x ← left[x]
               Step 3: Maintaining the information
                  5                    else x ← right[x]
                  6 return x
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8   max values of node x’s children: that overlaps i starts with x at the root
                  The search for an interval                                                                            of the tree and
19      20
                                                                              17         19
                                                                        16                    21
                                                                      15                            23
                                                     8       9
                                                 6               10
                                            5            8
                              0         3
                               0             5               10         15               20               25      30
                                                                        (a)

                                                                                     :          [22 25]
                                                                                   [22 25]
                                                                      [16,21]
  314             Chapter 14 Augmenting Data Structures
                                                      30

                                                     [22 25]                                                      int
                                            [8,9]                                                       [25,30]
                  Step 4: Developing new operations
                                  23                                                                      30      max


                 The only new operation we need is I NTERVAL -S EARCH (T,[26,26]
                         [5,8]                [15,23]        [17,19]             i), which finds a node
                          10                    23              20                26
                 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in
                [0,3] tree, a pointer to the sentinel nil[T ] is returned.
                 the            [6,10]                               [19,20]
                  3                   10                                                           20
                                                                        (b)
                  I NTERVAL -S EARCH (T, i)
                  1 x ← root[T ]
               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
                   The while x that represents them. does not overlap int[x]
               (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted
                               do if
               order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i]
                  3
                  4                    then x ← left[x]
               Step 3: Maintaining the information
                  5                    else x ← right[x]
                  6 return x
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8   max values of node x’s children: that overlaps i starts with x at the root
                  The search for an interval                                                                            of the tree and
19      20
                                                                               17         19
                                                                          16                   21
                                                                        15                           23
                                                     8       9
                                                 6               10
                                            5            8
                              0         3
                               0             5               10          15               20               25      30
                                                                         (a)

                                                                                      :          [22 25]
                                                                                    [22 25]
                                                                       [16,21]
  314             Chapter 14 Augmenting Data Structures
                                                      30

                                                     [22 25]                                                       int
                                            [8,9]                                                        [25,30]
                  Step 4: Developing new operations
                                  23                                                                       30      max
                                                                      [22 25]
                 The only new operation we need is I NTERVAL -S EARCH (T,[26,26]
                         [5,8]                [15,23]        [17,19]             i), which finds a node
                          10                    23              20                26
                 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in
                [0,3] tree, a pointer to the sentinel nil[T ] is returned.
                 the            [6,10]                               [19,20]
                  3                   10                                                            20
                                                                         (b)
                  I NTERVAL -S EARCH (T, i)
                  1 x ← root[T ]
               Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint.
                   The while x that represents them. does not overlap int[x]
               (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted
                               do if
               order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i]
                  3
                  4                    then x ← left[x]
               Step 3: Maintaining the information
                  5                    else x ← right[x]
                  6 return x
               We must verify that insertion and deletion can be performed in O(lg n) time on an
               interval tree of n nodes. We can determine max[x] given interval int[x] and the
2011   7   8   max values of node x’s children: that overlaps i starts with x at the root
                  The search for an interval                                                                             of the tree and
b




2011   7   8
Thank you




2011   7   8
Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to
               (b) The interval tree that represents them. An inorder tree walk of the tree list
               order by left endpoint.


               Step 3: Maintaining the information
               We must verify that insertion and deletion can be performed in O
                              O(lgn)
               interval tree of n nodes. We can determine max[x] given interv
               max values of node x’s children:
               max[x] = max(high[int[x]], max[left[x]], max[right[x]]) .
               Thus, by Theorem 14.1, insertion and deletion run in O(lg n) tim
               ing the max fields after a rotation can be accomplished in O(1)
               in Exercises 14.2-4 and 14.3-1.




2011   7   8

アルゴリズムイントロダクション 14章

  • 1.
    14 2011 7 8
  • 2.
    OK (^^)b 2011 7 8
  • 3.
    14 14.1 O(n) (i ) O(lgn) 14.2 14.1 14.3 2011 7 8
  • 4.
    3 size 1 14.1 Dynamic order statistics 303 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shade and darkened nodes are black. In addition to its usual fields, each node x has a field 26 the number of nodes in the subtree rooted at x. 20 17 41 12 7 14 21 30 47 10 7 A data structure that can support fast5 order-statistic operations is 16 19 4 21 28 38 1 4 ure 14.1. An order-statistic tree T is simply a 3red-black tree with ad 2 2 1 1 7 12 14 20 key 35 39 2 1 mation stored in each node. Besides the usual red-black tree fields key 1 1 1 1 3 size 1 p[x], left[x], and right[x] in a node x, we have another field size[x]. T tains the number of (internal) nodes in the subtree rooted at x (inclu Figure 14.1 An order-statistic the which is an augmented red-blackthe sentinel’s are red,to be 0, that is, the size of tree, subtree. If we define tree. Shaded nodes size and darkened nodes ]] to beIn addition towe have the identity a field size[x], which is size[nil[T are black. 0, then its usual fields, each node x has the number of nodes in the subtree rooted at x. size[x] = size[left[x]] + size[right[x]] + 1 . A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is to be distinct in anwith additional infor- We do not require keys simply a red-black tree order-statistic tree. (For mation stored in each node. Besides the usual red-black tree fields key[x], color[x], p[x], left[x],Figure 14.1 has twowe havewith value size[x]. This field con-with val tree in and right[x] in a node x, keys another field 14 and two keys tains the number ofequal keys, the the subtree rooted of xrank is not itself), defined presence of (internal) nodes in above notion at (including x well thatthis ambiguity for an order-statistic tree by defining the we set of an e is, the size of the subtree. If we define the sentinel’s size to be 0, that is, rank size[nil[T ]] to be 0, then we have the identity position at which it would be printed in an inorder walk of the tree. I O(n) O(lgn) size[x] = size[left[x]] + size[right[x]] + 1 .in a black node has rank 5, and the ke for example, the key 14 stored 2011 7 8 Werednot require keys to be distinct in an order-statistic tree. (For example, the a do node has rank 6.
  • 5.
    i 17 14.1 Dynamic order statistics 303 17 26 20 17 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 6.
    i 17 14.1 Dynamic order statistics 303 12 17 26 4 20 17 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 7.
    i 17 14.1 Dynamic order statistics 303 12 17 4 4 26 20 4 17 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 8.
    i 17 14.1 Dynamic order statistics 303 12 17 4 4 26 20 4 17 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 9.
    i 17 14.1 Dynamic order statistics 303 12 17 4 4 26 20 4 17 4 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 4 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 10.
    i 17 14.1 Dynamic order statistics 303 12 17 4 4 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 4 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 11.
    i 17 14.1 Dynamic order statistics 303 12 17 4 4 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 4 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 12.
    14.1 Dynamic orderstatistics 303 12 17 4 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 Chapter 14 Augmenting Data Structures 2 1 1 1 size 1 1 3 1 4 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, OS-S ELECT (x, i) and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. 1 r ← size[left[x]]+1support fast order-statistic operations is shown in Fig- A data structure that can 2 if i = r An order-statistic tree T is simply a red-black tree with additional infor- ure 14.1. mation stored in each node. Besides the usual red-black tree fields key[x], color[x], 3 thenleft[x], and right[x] in a node x, we have another field size[x]. This field con- p[x], return x tains the number of (internal) nodes in the subtree rooted at x (including x itself), 4 elseif iis,< size of the subtree. If we define the sentinel’s size to be 0, that is, we set that the r size[nil[T ]] to be 0, then we have the identity 5 then return OS-S ELECT (left[x], i) size[x] = size[left[x]] + size[right[x]] + 1 . 6 else return require keysELECT (right[x], i tree. (For example, the We do not OS-S to be distinct in an order-statistic − r) 2011 7 8
  • 13.
    :38 14.1 Dynamic order statistics 303 26 20 17 41 12 7 14 21 30 47 7 4 5 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 14.
    :38 14.1 Dynamic order statistics 303 26 20 17 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 15.
    :38 14.1 Dynamic order statistics 303 26 20 17 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 16.
    :38 14.1 Dynamic order statistics 303 26 20 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 17.
    :38 14.1 Dynamic order statistics 303 = 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 18.
    :38 14.1 Dynamic order statistics 303 17 = 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 19.
    :38 14.1 Dynamic order statistics 303 17 = 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is the number of nodes in the subtree rooted at x. A data structure that can support fast order-statistic operations is shown in Fig- ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- 2011 7 8 mation stored in each node. Besides the usual red-black tree fields key[x], color[x],
  • 20.
    14.1 Dynamic orderstatistics 303 17 = 26 20 4 17 4 41 12 7 14 7 21 4 30 5 2 47 1 10 16 19 21 28 38 4 2 2 1 1 3 7 12 14 20 key 35 39 2 1 1 1 1 1 3 size 1 14.1 Dynamic order statistics Figure 14.1 An order-statistic tree, which is an augmented red-black tree. Shaded nodes are red, and darkened nodes are black. In addition to its usual fields, each node x has a field size[x], which is OS-R (T, x) ANKnumber of nodes in the subtree rooted at x. the 1 r ← size[left[x]] that 1 support fast order-statistic operations is shown in Fig- A data structure + can 2 y ←ure 14.1. An order-statistic tree T is simply a red-black tree with additional infor- x mation stored in each node. Besides the usual red-black tree fields key[x], color[x], 3 while y = root[T ] in a node x, we have another field size[x]. This field con- p[x], left[x], and right[x] 4 do if y = right[ p[y]] in the subtree rooted at x (including x itself), tains the number of (internal) nodes that is, the size of the subtree. If we define the sentinel’s size to be 0, that is, we set 5 then r then we have the identity size[nil[T ]] to be 0,← r + size[left[ p[y]]] + 1 6 y ← p[y] size[x] = size[left[x]] + size[right[x]] + 1 . 7 returnWe do not require keys to be distinct in an order-statistic tree. (For example, the r 2011 7 8
  • 21.
    naught. We shallnow show that subtree sizes can be maintained and deletion without affecting the asymptotic running time of eit We noted in Section 13.3 that insertion into a red-black tre phases. The first phase goes down the tree from the root, inser as a child of an existing node. The second phase ( goes up the tree ) and ultimately performing rotations to maintain the red-black pr To maintain the subtree sizes in the first phase, we simply inc Chapter 14 Augmenting Data Structures traversed from the root down toward th each node x on the path node added gets a size of 1. Since there are O(lg n) nodes on t the additional cost of maintaining the size fields is O(lg n). LEFT-ROTATE(T, x) 42 In the second phase, the only structural changes to the underly 93 19 x 19 y are caused by rotations, of which there are at most two. Moreo 93 42 y x local operation: RIGHT-ROTATE(T, y) have their size fields invalidated 12 only two nodes 11 6 7 which the rotation is performed is incident on these two nodes code for L7EFT-ROTATE (T, x) in Section 13.2,4 we add the follow 4 6 13 size[y] ← size[x] Figure 14.2 14 size[x] ← size[left[x]] + size[right[x]] + which the rotation is Updating subtree sizes during rotations. The link around 1 formed is incident on the two nodes whose size fields need to be updated. The updates are lo requiring only the size information stored in x, y, and the roots of the subtrees shown as triangle O(1) Figure 14.2 illustrates how the fields are updated. The change t 2011 7 8 is symmetric.
  • 22.
    14 14.1 O(n) (i ) O(lgn) 14.2 14.1 14.3 2011 7 8
  • 23.
    2011 7 8
  • 24.
    size ( ) size / 2011 7 8
  • 25.
    14 14.1 O(n) (i ) O(lgn) 14.2 14.1 14.3 2011 7 8
  • 26.
    Chapter 14 AugmentingData Structures i i i i i′ i′ i′ i′ (a) i i′ i′ i (b) (c) Figure 14.3 The interval trichotomy for two closed intervals i and i . (a) If i and i overlap, there are four situations; in each, low[i] ≤ high[i ] and low[i ] ≤ high[i]. (b) The intervals do not overlap, and high[i] < low[i ]. (c) The intervals do not overlap, and high[i ] < low[i]. 2011 7 8
  • 27.
    16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) ( ) int[x] [16,21] 30 int [8,9] [25,30] 23 30 max [5,8] [15,23] [17,19] [26,26] 10 23 20 26 [0,3] [6,10] [19,20] 3 10 20 (b) Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted order by left endpoint. Step 3: Maintaining the information We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8
  • 28.
    16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) ( ) int[x] [16,21] 30 int [8,9] [25,30] 23 30 max [5,8] [15,23] [17,19] [26,26] 10 23 20 26 [0,3] [6,10] [19,20] 3 10 20 (b) Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted order by left endpoint. Step 3: Maintaining the information We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8
  • 29.
    16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) ( ) int[x] [16,21] 30 int [8,9] [25,30] 23 30 max [5,8] [15,23] [17,19] [26,26] 10 23 20 26 [0,3] [6,10] [19,20] 3 10 20 (b) Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted order by left endpoint. Step 3: Maintaining the information We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8
  • 30.
    16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) ( ) int[x] [16,21] 30 int [8,9] [25,30] 23 30 max [5,8] [15,23] [17,19] [26,26] 10 23 20 26 [0,3] [6,10] [19,20] 3 10 20 (b) Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. (b) The interval tree that represents them. An inorder tree walk of the tree lists the nodes in sorted order by left endpoint. Step 3: Maintaining the information We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8
  • 31.
    O(lgn) 14.1 14.2-4 14.3-1 2011 7 8
  • 32.
    19 20 17 19 16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) : [22 25] [16,21] 314 Chapter 14 Augmenting Data Structures 30 int [8,9] [25,30] Step 4: Developing new operations 23 30 max The only new operation we need is I NTERVAL -S EARCH (T,[26,26] [5,8] [15,23] [17,19] i), which finds a node 10 23 20 26 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in [0,3] tree, a pointer to the sentinel nil[T ] is returned. the [6,10] [19,20] 3 10 20 (b) I NTERVAL -S EARCH (T, i) 1 x ← root[T ] Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. The while x that represents them. does not overlap int[x] (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted do if order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i] 3 4 then x ← left[x] Step 3: Maintaining the information 5 else x ← right[x] 6 return x We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8 max values of node x’s children: that overlaps i starts with x at the root The search for an interval of the tree and
  • 33.
    19 20 17 19 16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) : [22 25] [22 25] [16,21] 314 Chapter 14 Augmenting Data Structures 30 int [8,9] [25,30] Step 4: Developing new operations 23 30 max The only new operation we need is I NTERVAL -S EARCH (T,[26,26] [5,8] [15,23] [17,19] i), which finds a node 10 23 20 26 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in [0,3] tree, a pointer to the sentinel nil[T ] is returned. the [6,10] [19,20] 3 10 20 (b) I NTERVAL -S EARCH (T, i) 1 x ← root[T ] Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. The while x that represents them. does not overlap int[x] (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted do if order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i] 3 4 then x ← left[x] Step 3: Maintaining the information 5 else x ← right[x] 6 return x We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8 max values of node x’s children: that overlaps i starts with x at the root The search for an interval of the tree and
  • 34.
    19 20 17 19 16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) : [22 25] [22 25] [16,21] 314 Chapter 14 Augmenting Data Structures 30 [22 25] int [8,9] [25,30] Step 4: Developing new operations 23 30 max The only new operation we need is I NTERVAL -S EARCH (T,[26,26] [5,8] [15,23] [17,19] i), which finds a node 10 23 20 26 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in [0,3] tree, a pointer to the sentinel nil[T ] is returned. the [6,10] [19,20] 3 10 20 (b) I NTERVAL -S EARCH (T, i) 1 x ← root[T ] Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. The while x that represents them. does not overlap int[x] (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted do if order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i] 3 4 then x ← left[x] Step 3: Maintaining the information 5 else x ← right[x] 6 return x We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8 max values of node x’s children: that overlaps i starts with x at the root The search for an interval of the tree and
  • 35.
    19 20 17 19 16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) : [22 25] [22 25] [16,21] 314 Chapter 14 Augmenting Data Structures 30 [22 25] int [8,9] [25,30] Step 4: Developing new operations 23 30 max The only new operation we need is I NTERVAL -S EARCH (T,[26,26] [5,8] [15,23] [17,19] i), which finds a node 10 23 20 26 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in [0,3] tree, a pointer to the sentinel nil[T ] is returned. the [6,10] [19,20] 3 10 20 (b) I NTERVAL -S EARCH (T, i) 1 x ← root[T ] Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. The while x that represents them. does not overlap int[x] (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted do if order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i] 3 4 then x ← left[x] Step 3: Maintaining the information 5 else x ← right[x] 6 return x We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8 max values of node x’s children: that overlaps i starts with x at the root The search for an interval of the tree and
  • 36.
    19 20 17 19 16 21 15 23 8 9 6 10 5 8 0 3 0 5 10 15 20 25 30 (a) : [22 25] [22 25] [16,21] 314 Chapter 14 Augmenting Data Structures 30 [22 25] int [8,9] [25,30] Step 4: Developing new operations 23 30 max [22 25] The only new operation we need is I NTERVAL -S EARCH (T,[26,26] [5,8] [15,23] [17,19] i), which finds a node 10 23 20 26 in tree T whose interval overlaps interval i. If there is no interval that overlaps i in [0,3] tree, a pointer to the sentinel nil[T ] is returned. the [6,10] [19,20] 3 10 20 (b) I NTERVAL -S EARCH (T, i) 1 x ← root[T ] Figure 14.4 An interval tree. (a) A set of 10 intervals, shown sorted bottom to top by left endpoint. The while x that represents them. does not overlap int[x] (b)2 interval tree = nil[T ] and i An inorder tree walk of the tree lists the nodes in sorted do if order by left endpoint. left[x] = nil[T ] and max[left[x]] ≥ low[i] 3 4 then x ← left[x] Step 3: Maintaining the information 5 else x ← right[x] 6 return x We must verify that insertion and deletion can be performed in O(lg n) time on an interval tree of n nodes. We can determine max[x] given interval int[x] and the 2011 7 8 max values of node x’s children: that overlaps i starts with x at the root The search for an interval of the tree and
  • 37.
  • 38.
  • 39.
    Figure 14.4 Aninterval tree. (a) A set of 10 intervals, shown sorted bottom to (b) The interval tree that represents them. An inorder tree walk of the tree list order by left endpoint. Step 3: Maintaining the information We must verify that insertion and deletion can be performed in O O(lgn) interval tree of n nodes. We can determine max[x] given interv max values of node x’s children: max[x] = max(high[int[x]], max[left[x]], max[right[x]]) . Thus, by Theorem 14.1, insertion and deletion run in O(lg n) tim ing the max fields after a rotation can be accomplished in O(1) in Exercises 14.2-4 and 14.3-1. 2011 7 8