Tries

COMP2521 20T2 ♢ Tries [0/15]
❖ Tries


A trie


Note: generally assume "string" = character string; could be bit-string


Note: Trie comes from retrieval ;  but pronounced as "try" not "tree"

COMP2521 20T2 ♢ Tries [1/15]
❖ ... Tries


Each node in a trie …

A "finishing" node marks the end of one key

Depth d of trie = length of longest key value

COMP2521 20T2 ♢ Tries [2/15]
❖ ... Tries

Trie example:

[Diagram:Pics/trie-example.png]

COMP2521 20T2 ♢ Tries [3/15]
❖ ... Tries

Possible trie representation:

#define ALPHABET_SIZE 26

typedef struct Node *Trie;

typedef struct Node {
   char onechar;     // current char in key
   Trie child[ALPHABET_SIZE];
   bool finish;      // last char in key?
   Item data;        // no Item if !finish
} Node;

typedef char *Key;   // just lower-case letters

COMP2521 20T2 ♢ Tries [4/15]
❖ ... Tries


Above representation is space inefficient

If we allowed all ascii chars in alphabet, 128 children

Could reduce branching factor by reducing "alphabet"

COMP2521 20T2 ♢ Tries [5/15]
❖ ... Tries

Note: Can also use BST-like nodes (cf. red-black trees) ...

[Diagram:Pics/trie-as-bst.png]

COMP2521 20T2 ♢ Tries [6/15]
❖ Searching in Tries

Search requires traversing a path, char-by-char from Key:

find(trie,key):
|  Input  trie, key
|  Output pointer to element in trie if key found
|         NULL otherwise
|
|  node=trie
|  for each char c in key do
|  |  if node.child[c] exists then
|  |     node=node.child[c]  // move down one level
|  |  else
|  |     return NULL
|  |  end if
|  end for
|  if node.finish then  // "finishing" node reached?
|     return node
|  else
|     return NULL
|  end if

COMP2521 20T2 ♢ Tries [7/15]
❖ ... Searching in Tries

[Diagram:Pics/trie-search.png]

COMP2521 20T2 ♢ Tries [8/15]
❖ ... Searching in Tries

[Diagram:Pics/trie-search2.png]

COMP2521 20T2 ♢ Tries [9/15]
❖ ... Searching in Tries

[Diagram:Pics/trie-search3.png]

COMP2521 20T2 ♢ Tries [10/15]
❖ Insertion into Tries

Insertion into a Trie ...

Trie insert(trie,item,key):
|  Input  trie, item with key of length m
|  Output trie with item inserted
|
|  if trie is empty then
|  |  t=new trie node
|  end if
|  if m=0 then  // end of key
|  |  t.finish=true, t.data=item
|  else
|  |  first=key[0],  rest=key[1..m-1]
|  |  t.child[first]=insert(t.child[first],item,rest)
|  end if
|  return t

COMP2521 20T2 ♢ Tries [11/15]
❖ Cost Analysis


Analysis of standard trie:

where
COMP2521 20T2 ♢ Tries [12/15]
❖ Example Trie

Example text and corresponding trie of searchable words:

[Diagram:Pics/text-trie-example.png]

Note: trie has no prefixes  ⇒  all finishing nodes are leaves

COMP2521 20T2 ♢ Tries [13/15]
❖ Compressed Tries

Compressed tries

Example:

[Diagram:Pics/compressed-trie.png]

COMP2521 20T2 ♢ Tries [14/15]
❖ ... Compressed Tries

Compact representation of compressed trie to encode array S  of strings:

Example:

[Diagram:Pics/compact-trie.png]

COMP2521 20T2 ♢ Tries [15/15]


Produced: 11 Jul 2020