home / experts / javascript / column57 |
|
Storing Data ElementsWe want to index our eight presidents by their first name + last name, as well as their last name + first name. Adhering to the principles of the trie, here is how we build the trie for the eight presidents. First we allocate memory for the top level 10-element array, called
Then we allocate memory for another 10-element array:
which we assign
Now we have a 10-element array at the second element of the top level
Let's examine what's happening at the digit 3. The key 3 is associated with the letters D, E, and F. Quick scanning of the president list, we find that only Ford is indexed by the digit 3. Hence, we can store him at the top level
The digit 4 (associated with G, H, and I) is more crowded. First we create two more levels of hierarchies, one at
Two names crowd the digit 4:
The digit 5 is associated with J, K, and L, and is loaded with five names. The first three are Jimmy Carter, Lyndon Johnson, and Kennedy (John). Allocating one array at
The interesting pair is John Kennedy and Johnson (Lyndon). As you can see you need five level or hierarchies before you can distinguish between them:
Nixon (Richard) is alone on digit 6, associated with M, N, and O:
Finally, we need one level of hierarchy at digit 7, associated with P, R, and S, for Reagan (Ronald), Richard Nixon, and Ronald Reagan:
|
Produced by Yehuda Shiran and Tomer Shiran
Created: February 14, 2000
Revised: February 14, 2000
URL: http://www.webreference.com/js/column57/3.html