12-2 Radix trees

Given two strings a=a0a1apa = a_0a_1 \ldots a_p and b=b0b1bqb = b_0b_1 \ldots b_q, where each aia_i and each bjb_j is in some ordered set of characters, we say that string aa is lexicographically less than string bb if either

  1. there exists an integer jj, where 0jmin(p,q)0 \le j \le \min(p, q), such that ai=bia_i = b_i for all i=0,1,j1i = 0, 1, \ldots j - 1 and aj<bja_j < b_j, or
  2. p<qp < q and ai=bia_i = b_i for all i=0,1,,pi = 0, 1, \ldots, p.

For example, if aa and bb are bit strings, then 10100<1011010100 < 10110 by rule 1 (letting j=3j = 3) and 10100<10100010100 < 101000 by rule 2. This ordering is similar to that used in English-language dictionaries.

The radix tree data structure shown in Figure 12.5 stores the bit strings 1011,10,011,1001011, 10, 011, 100, and 00. When searching for a key a=a0a1apa = a_0a_1 \ldots a_p, we go left at a node of depth ii if ai=0a_i = 0 and right if ai=1a_i = 1. Let SS be a set of distinct bit strings whose lengths sum to nn. Show how to use a radix tree to sort SS lexicographically in Θ(n)\Theta(n) time. For the example in Figure 12.5, the output of the sort should be the sequence 0,011,10,100,10110, 011, 10, 100, 1011.

(Removed)