Another possibility is creating two tries.
The first one (let it be T1
) is for first names, and the second (let it be T2
) for last names.
When you construct the trie, from each word terminator in T1
(usually denoted as $
sign), add a list of pointers to the relevant entries in T2
, and vise versa.
I.E. if John Doe is an entree:
T1:
J
|
O
|
H
|
N
|
$1
T2:
D
|
O
|
E
|
$2
$1 will hold a list, containing pointer to $2, and $2 will hold a list, containing $1.
each prefix search will search on both tries, getting you the auto completion, and then use the pointers to get the full name (the partial search gave you only first/last name, you get the second using the pointers).
Searching for full name is done by searching in both tries (look for the first name in T1
and for the last name in T2
, and get the relevant $1
and $2
respectively), then you need to check if the pointers match (the list l1
in $1
contains $2
and the list l2
in $2
contains $1
). If they do - the name is in the dictionary.
Note that once you have a pointer to the $
node, one can simply go back on the trie, until you get to the root to get the word this $
sign is representing. (requires pointer to parent from each node)
Also note: I explained about simple tries, but there is really no reason why not to use patricia tries instead, using the same approach.