|
|
|
|
|
|
Example:
|
|
Example:
bear = 'b' 'e' 'a' 'r'
= 01100010 01100101 01100001 01110010
|
|
|
p = root; // Start at the root
for ( each character c in the keyword ) do
{
if ( c is already stored of the sub-trie )
traverse to that subtrie
else
create a new node for c
}
insert value into leaf node
|
Example:
|
Value put( Key k, Value v )
{
int i;
TrieNode p;
p = root;
for ( i = 0; i < k.length(); i++ )
{
nextChar = k[i];
if ( nextChar ∈ p.char[] )
{
nextLink = link associated with the char entry;
p = nextLink; // Traverse
}
else
{
p = new TrieNode(); // Create new node
insert "nextChar" into p;
}
}
/* ------------------------------------------------------
When the while loop ends, we have found or created
the external node for the key "k"
------------------------------------------------------ */
insert v into node p;
}
|
|
|
Example: at and ate
|
|
Example:
|
|
and modified it to print the internal structure...
Caveat:
|
How to run the program:
|
(Edited) Output:
(`*',0) (`b',-1) (`e',-1) (`a',-1) (`r',-1) (`*',36) "bear"
========================================================
(`*',0) (`b',-1) (`e',-1) (`a',-1) (`r',-1) (`*',36) "bear"
(`l',-1) (`l',-1) (`*',14) "bell"
========================================================
(`*',0) (`b',-1) (`e',-1) (`a',-1) (`r',-1) (`*',36) "bear"
(`l',-1) (`l',-1) (`*',14) "bell"
(`*',-1) (`*',4) "be"
========================================================
(`*',0) (`b',-1) (`e',-1) (`a',-1) (`r',-1) (`*',36) "bear"
(`l',-1) (`l',-1) (`*',14) "bell"
(`*',-1) (`*',4) "be"
(`u',-1) (`l',-1) (`l',-1) (`*',40) "bull"
========================================================
(`*',0) (`b',-1) (`e',-1) (`a',-1) (`r',-1) (`*',36) "bear"
(`l',-1) (`l',-1) (`*',14) "bell"
(`*',-1) (`*',4) "be"
(`u',-1) (`l',-1) (`l',-1) (`*',40) "bull"
(`y',-1) (`*',40) "buy"
|
typedef struct sTRIE_NODE
{
char is_final; // 1 if the node is an external node
// ==> void *data will contain data stored
// 0 if node is an internal node
// ==> use nxt[charCode] to search further
void *data; // user data that is stored with this node
// Only use this field if is_final == 1
unsigned char childs; // number of childs. useful for fast deletion
// (If childs becomes 0, we delete this node)
struct sTRIE_NODE *nxt[256]; // child pointers for every ASCII char !
} TRIE_NODE;
|
/* ====================================================================
trie_search_nodes( node, str, pos )
Search for the string str[pos...]
in sub-trie rooted at node "node"
==================================================================== */
int trie_search_nodes(TRIE_NODE *node, char *str, int pos)
{
unsigned char ch = (unsigned char)str[pos]; // Use this character to search
// in node "node"
if( ch == '\0' ) // string is ended
{
if( node->is_final == 1 )
{
// We reached an external node ==> the string is found !
crt_search_data = node->data; // Get data assoc. with keyword
return 1; // Return FOUND indication
}
else
{
return 0; // Return NOT FOUND indication
}
}
else
{
if( node->nxt[ch] != NULL ) // More characters in string
{
return trie_search_nodes(node->nxt[ch], str, pos + 1);
// Search further - using next character position
}
else
{
return 0; // not found
}
}
}
|