Chapter 6
Selection Trees

A selection tree is a complete binary tree in which the leaf nodes hold a set of keys, and each internal node holds the “winner” key among its children.

6.1 Modifying a Key

It takes O(log n) time to modify a selection tree in response to a change of a key in a leaf.

        4o
   |---------|
   o         o
   2        4|
 |----|   |----|
 o    o   o    o
1    2    3    4         4o
   |---------|

   2o        4o
 |----|   |----|

1o   2o   3oo5    4o         4o
   |---------|

   2o        45oo
 |----|   |----|

1o   2o   3oo5    4o         oo
        45
   |---------|
   2o        45oo
   |         |
 |----|   |----|
1o   2o   3oo5    4o

6.2 Initialization

The construction of a selection tree from scratch takes O(n) time by traversing it level-wise from bottom up.

        o
        |
   |---------|
   o         o
   |         |
 |----|   |----|
1o   2o   3o    4o         o
        |
   |---------|
   o         o
   |        4|
 |----|   |----|
 o    o   o    o
1    2    3    4         o
   |---------|

   2o        4o
 |----|   |----|
 o    o   o    o
1    2    3    4         4o
   |---------|

   2o        4o
 |----|   |----|

1o   2o   3o    4o

6.3 Application: External Sort

Given a set of n values 16 9 10 8 6 11 12 1 4 7 14 13 2 15 5 3 n = 16
divide it into M chuncks, 16 9 10 8 6 11 12 14 7 14 132 15 5 3 M = 4
internally sort each chunk, 8 9 10 16 1 6 11 124 7 13 142 3 5 15
construct complete binary tree of M leaves with the chunks attached to the leaves.                    o
         |--------------------|
         o                    o
         |                    |
    |---------|          |---------|
    o         o          o         o
    |         |          |         |
    |         |          |         |
8-9 10-16 1-6-11 12 -4 7-13-14 2-3 5-15
--------- --------- ---------  --------
Convert the tree into a selection tree with the keys being fed to the leaves from the chunks                    0o
         |--------------------|
         o                    o
         1                   0|
    |---------|          |---------|
    o         o          o         o
   8|         1         0|        2|
    |         |          |         |
--9 10-16 --6-11 12 --7-13-14| --3 5-15
--------- --------- ---------  --------
Remove the winner from the tree                    o
         |--------------------|
         o                    o
         1                    |
    |---------|          |---------|
    o         o          o         o
   8|         1          |        2|
    |         |          |         |
--9 10-16 --6-11 12 --7-13-14| --3 5-15
--------- --------- ---------  --------
Feed to the empty leaf the next value from its corresponding chunk                    o
         |--------------------|
         o                    o
         1                    |
    |---------|          |---------|
    o         o          o         o
   8|         1         7|        2|
    |         |          |         |
--9 10-16 --6-11 12 ----13-14| --3 5-15
--------- --------- ---------  --------
Adjust the selection tree to the change in the leaf                    1o
         |--------------------|
         o                    o
         1                   2|
    |---------|          |---------|
    o         o          o         o
   8|         1         7|        2|
    |         |          |         |
--9 10-16 --6-11 12 ----13-14| --3 5-15
--------- --------- ---------  --------
Repeat the deletion subprocess until all the values are consumed.