Chapter 11: Tries

Algorithms
Prefix treeNode structureInsertionSearch and prefix queries AutocompleteDeletionTime and memoryCompressed and suffix tries
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Structure

Explain how a trie is built from a root, nodes, edges and end-of-word marks; draw the trie of a set of words.

Operations

Implement insert, search, prefix query, autocomplete and delete in Java and trace them step by step.

Analysis

Justify the O(L) time and O(N · M) memory of the operations; compare tries with hash tables and BSTs.

Variants

Explain which problem compressed tries, suffix tries and Aho–Corasick each solve.

Trie Basics

The Trie (Prefix Tree)

  • A tree-like data structure that stores strings for fast lookup.
  • The root is empty; each step down from the root represents one character.
  • Words are stored as paths from the root; common prefixes are stored only once.
  • Applications: autocomplete, spell checking, IP routing.
rootandandtantdaddadodo
The name comes from retrieval (E. Fredkin, 1960). A double circle marks a node where a word ends.

Trie Basics

Why a Trie?

We want every word in a dictionary that starts with "an". How do different structures answer this?

Array / listCompare against every word: O(n · L) for n words. Slows down as the dictionary grows.
Hash tableFinds a whole word in O(L) on average, but cannot answer prefix queries: the hashes of "an" and "and" are unrelated.
TrieFollow branch "a", then "n" (2 steps); every word below that node starts with "an".
Search time in a trie depends on the length (L) of the key, not on the number of words stored.
danoatddrootan

Trie Basics

Basic Components

RootAn empty node; it represents no character and is where every search begins.
NodesThe path from the root to a node is a prefix; each node holds links to its children.
EdgesLinks between nodes; each edge carries one character.
End-of-word markisEnd: a word ends at this node.
rootandandtantdaddadodo

The colored node represents the prefix "an"; "an" is not a word (unmarked), but it is a prefix of "and" and "ant".

Trie Basics

Example: A Seven-Word Trie

Words
ab · aba · abc
ad
ba · bad · bag

7 words, 18 characters in total → only 9 nodes (+ root).

rootababaabacabcdadbabadbadgbag
"ab" is both a word and a prefix of "aba" and "abc". The shape alone cannot tell us this, so the isEnd mark is essential.

Trie Basics

Example: A Trie with Values

KeyValueKeyValue
A15tea3
i11ted4
in5ten12
inn9to7
rootA15i11n5n9tea3d4n12o7
A trie can also serve as a map: the node stores a value instead of isEnd. "t" and "te" carry no value; they are only prefixes. The uppercase "A" needs a wider alphabet (e.g. a HashMap) than a 26-letter array.

Implementation

The Trie Node Class

public class TrieNode {
  TrieNode[] children;  // 'a'..'z'
  boolean isEnd;  // word ends here?

  public TrieNode() {
    isEnd = false;
    children = new TrieNode[26];
  }
}
  • Each node holds 26 child references, all null initially.
  • The character is not stored in the node: it is implied by which index of the parent the node sits at.
  • isEnd: is the path from the root to here a word?
isEndfalseletterindexa0ab1⁄c2⁄d3de4⁄f5⁄g6⁄h7⁄i8⁄j9⁄k10⁄l11⁄m12⁄n13⁄o14⁄p15⁄q16⁄r17⁄s18⁄t19⁄u20⁄v21⁄w22⁄x23⁄y24⁄z25⁄

The root's children array after inserting and, ant, dad, do: only 2 of the 26 cells are used (⁄ = null).

Implementation

From Character to Index: ch − 'a'

chUnicodech − 'a'
'a'970
'd'1003
'n'11013
'o'11114
't'11619
'z'12225
031325ad'a' − 'a' = 0, 'd' − 'a' = 3root.children: other 24 cells null
  • In Java a char is a number; ch - 'a' maps a lowercase letter to 0..25.
  • Accessing a child by array index is O(1): node.children[ch - 'a'].
  • For "and", children[0], children[13], children[3] are used in turn.
'A' - 'a' = −32, 'ç' - 'a' = 134 → ArrayIndexOutOfBoundsException. For uppercase or Turkish letters, normalize the input or use a HashMap.

Insertion

Insertion

public void insert(String word) {
  TrieNode node = root;
  for (char ch : word.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      node.children[i] = new TrieNode();
    node = node.children[i];
  }
  node.isEnd = true;
}
  1. Start at the root.
  2. For each character of the word, look at the matching child: children[ch − 'a'].
  3. If the child is null, create a new node; otherwise (a shared prefix) reuse the existing node.
  4. Move to that child.
  5. When the characters run out, set isEnd = true on the last node.
Time: O(L) (L = word length); at most L new nodes are created. Inserting the same word twice leaves the structure unchanged.

Insertion · Step by step

Insertion Step by Step: and, ant, dad, do

root

Initially the trie is just the root; all 26 of its children are null and isEnd = false.

roota

Inserting "and", character 'a': children[0] null → create a new node and move to it.

rootan

Inserting "and", character 'n': children[13] null → create a new node and move to it.

rootand

Inserting "and", character 'd': children[3] null → create a new node and move to it.

rootandand

Characters exhausted: isEnd = true on the last node (double circle). "and" inserted. The path children[0] → children[13] → children[3] from the root is in place.

rootandand

Inserting "ant", character 'a': children[0] is set → move to the existing "a" node (shared prefix).

rootandand

Inserting "ant", character 'n': children[13] is set → move to the existing "an" node (shared prefix).

rootandandt

Inserting "ant", character 't': children[19] null → create a new node and move to it.

rootandandtant

Characters exhausted: isEnd = true on the last node (double circle). "ant" inserted. Only 1 new node was needed: the prefix "an" is shared.

rootandandtantd

Inserting "dad", character 'd': children[3] null → create a new node and move to it.

rootandandtantda

Inserting "dad", character 'a': children[0] null → create a new node and move to it.

rootandandtantdad

Inserting "dad", character 'd': children[3] null → create a new node and move to it.

rootandandtantdaddad

Characters exhausted: isEnd = true on the last node (double circle). "dad" inserted.

rootandandtantdaddad

Inserting "do", character 'd': children[3] is set → move to the existing "d" node (shared prefix).

rootandandtantdaddado

Inserting "do", character 'o': children[14] null → create a new node and move to it.

rootandandtantdaddadodo

Characters exhausted: isEnd = true on the last node (double circle). "do" inserted. Final state: 4 words, 11 characters, 8 nodes excluding the root.

Insertion · Step through the code

insert("ant"): Step Through the Code

rootandand

The trie holds only "and". node starts at the root.

rootandand

Character 'a' → i = 'a' − 'a' = 0.

rootandand

node.children[0] is not null → no new node needed.

rootandand

node is now the "a" node.

rootandand

Character 'n' → i = 'n' − 'a' = 13.

rootandand

node.children[13] is not null → no new node needed.

rootandand

node is now the "an" node.

rootandand

Character 't' → i = 't' − 'a' = 19.

rootandand

node.children[19] == null → condition true.

rootandandt

A new TrieNode is created and linked at children[19] (isEnd = false).

rootandandt

node is now the "ant" node.

rootandandtant

Loop done: node.isEnd = true. "ant" inserted; only 1 node was created.

public void insert(String word) {
  TrieNode node = root;
  for (char ch : word.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      node.children[i] = new TrieNode();
    node = node.children[i];
  }
  node.isEnd = true;
}

Search

Search

public boolean search(String word) {
  TrieNode node = root;
  for (char ch : word.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      return false;
    node = node.children[i];
  }
  return node.isEnd;
}
  • Start at the root.
  • For each character, follow the matching branch (child).
    • If the branch is missing (null), the word is not in the trie.
  • At the end of the word, check isEnd:
    • true → the word is found;
    • false → not a word; it is only a prefix of a stored word.
Time: O(L). Even with a million words in the trie, "dad" is found in at most 3 steps.

Search · Step through the code

search("dad"): Step Through the Code

rootandandtantdaddadodo

Trie: and, ant, dad, do. node starts at the root.

rootandandtantdaddadodo

Character 'd' → i = 3.

rootandandtantdaddadodo

children[3] is not null → branch exists, continue.

rootandandtantdaddadodo

node = "d".

rootandandtantdaddadodo

Character 'a' → i = 0.

rootandandtantdaddadodo

children[0] is not null → branch exists, continue.

rootandandtantdaddadodo

node = "da".

rootandandtantdaddadodo

Character 'd' → i = 3.

rootandandtantdaddadodo

children[3] is not null → branch exists, continue.

rootandandtantdaddadodo

node = "dad".

rootandandtantdaddadodo

End of word; node.isEnd = true → return true (word found).

public boolean search(String word) {
  TrieNode node = root;
  for (char ch : word.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      return false;
    node = node.children[i];
  }
  return node.isEnd;
}

Search

Three Possible Outcomes of a Search

search("dad") → trueAll characters found and the last node is marked.
rootandtdado
search("da") → falseThe path exists, but node "da" has isEnd = false: "da" is only a prefix.
rootandtdado
search("dog") → falseAt node "do", children[6] ('g') is null: no branch, early exit.
rootandtdadog

Dashed red circle: a node that was searched for but is not in the trie.

Search

Prefix Query: startsWith

public boolean startsWith(String prefix) {
  TrieNode node = root;
  for (char ch : prefix.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      return false;
    node = node.children[i];
  }
  return true;  // search: node.isEnd
}
QuerysearchstartsWith
"dad"truetrue
"da"falsetrue
"an"falsetrue
"dog"falsefalse
The only difference is the last line: a prefix query does not check the end-of-word mark; the path existing is enough. Trie: and, ant, dad, do. Time: O(P), P = prefix length.

Search · Step through the code

startsWith("da") and startsWith("dog")

danoatddroot

The trie holds and, ant, dad, do (ringed node = word end). First startsWith("da"): node is set to the root.

danoatddroot

ch = 'd', i = 3: children[3] is set → node moves down to that child ("d").

danoatddroot

ch = 'a', i = 0: children[0] is set → node moves down to that child ("da").

danoatddroot

The prefix is consumed and the path never broke → true. Node "da" is unmarked, so search("da") would return false; a prefix query ignores the end mark.

danoatddroot

Second call startsWith("dog"): node is reset to the root.

danoatddroot

ch = 'd', i = 3: children[3] is set → node moves down to that child ("d").

danoatddroot

ch = 'o', i = 14: children[14] is set → node moves down to that child ("do").

danoatddrootnullg

ch = 'g', i = 6: children[6] of node "do" is null → false. "do" is a word, but no word starts with "dog".

public boolean startsWith(String prefix) {
  TrieNode node = root;
  for (char ch : prefix.toCharArray()) {
    int i = ch - 'a';
    if (node.children[i] == null)
      return false;
    node = node.children[i];
  }
  return true;  // search: node.isEnd
}

Search

Prefix Counter: countPrefix in O(P)

void insert(String word) { if (search(word)) return;
  TrieNode d = root;
  d.count++;
  for (char ch : word.toCharArray()) {
    int i = ch - 'a';
    if (d.children[i] == null) d.children[i] = new TrieNode();
    d = d.children[i];
    d.count++;          // one more word passed
  }
  d.isEnd = true;
}
int countPrefix(String prefix) {
  TrieNode d = root;
  for (char ch : prefix.toCharArray()) {
    d = d.children[ch - 'a'];
    if (d == null) return 0;
  }
  return d.count;
}
addtndao11221112root
Green numbers: count (4 at the root).
Queryresult
countPrefix("an")2
countPrefix("da")1
countPrefix("x")0
Cost: one int count per node; without it the query walks the subtree, O(P + S · M).

Search

Wildcard Search: "d.d"

boolean wildcard(TrieNode d, String s, int j) {
  if (d == null) return false;
  if (j == s.length()) return d.isEnd;
  char ch = s.charAt(j);
  if (ch != '.')
    return wildcard(d.children[ch - 'a'], s, j + 1);
  for (int i = 0; i < 26; i++)
    if (wildcard(d.children[i], s, j + 1)) return true;
  return false;
}
Patternresultmatch
"d.d"truedad
".n."trueand
".o"truedo
"b.."falseno b branch
"...."falseno 4-letter word
  • Trie: and, ant, dad, do. Call wildcard(root, pattern, 0).
  • A normal letter narrows the search to one path; '.' tries every non-null branch (backtracking). The first match propagates true upward.
  • Worst case 26k branches for k wildcards, but empty branches return at once: the real cost is bounded by the paths that exist in the trie. A hash table can answer this only by scanning every word.

Search

Spell Checking: Edit Distance on a Trie

  • Question: which dictionary words are within edit distance (insert / delete / replace) at most 1 of "dod"?
  • Instead of computing the distance to each word separately, traverse the trie depth-first; each node derives one DP row from its parent's row.
  • A common prefix's row is computed once: the "d" row serves both dad and do.
  • Pruning: if the row's minimum exceeds k, no word in the subtree can be within k; the minimum of row "an" is 2 > 1, so and and ant are never visited.
node"" d o dlastaction
a1 1 2 3—continue
an2 2 2 3—prune (min 2)
d1 0 1 2—continue
da2 1 1 2—continue
dad3 2 2 11suggest
do2 1 0 11suggest

The last cell of a row is the distance between the node's word and "dod".

Suggestions: dad (o → a) and do (delete the final d). O(|query|) work per row; thanks to pruning, most of the dictionary is never visited. The same traversal works with a weighted distance that makes adjacent keyboard letters cheap; as k grows, pruning weakens and the number of visited nodes rises quickly.

Search · Step through the code

Autocomplete: complete("te")

rootaaiininninnteateadtedntenoto

First findNode("te") walks t → e from the root to node "te" (like a prefix query).

rootaaiininninnteateadtedntenoto

Starting at node "te", the subtree is traversed depth-first.

rootaaiininninnteateadtedntenoto

"te" is not a word (isEnd = false) → not added to the list.

rootaaiininninnteateadtedntenoto

i = 0: children[0] ('a') is set → path = "tea".

rootaaiininninnteateadtedntenoto

Recursive call: collect("tea").

rootaaiininninnteateadtedntenoto

"tea" is marked → added to result. No children; the loop passes over 26 empty indices.

rootaaiininninnteateadtedntenoto

The call returns; drop the last letter (path = "te") → backtracking.

rootaaiininninnteateadtedntenoto

i = 3: children[3] ('d') is set → path = "ted".

rootaaiininninnteateadtedntenoto

Recursive call: collect("ted").

rootaaiininninnteateadtedntenoto

"ted" is marked → added to result. No children; the loop passes over 26 empty indices.

rootaaiininninnteateadtedntenoto

The call returns; drop the last letter (path = "te") → backtracking.

rootaaiininninnteateadtedntenoto

i = 13: children[13] ('n') is set → path = "ten".

rootaaiininninnteateadtedntenoto

Recursive call: collect("ten").

rootaaiininninnteateadtedntenoto

"ten" is marked → added to result. No children; the loop passes over 26 empty indices.

rootaaiininninnteateadtedntenoto

The call returns; drop the last letter (path = "te") → backtracking.

rootaaiininninnteateadtedntenoto

Loop done; complete("te") → [tea, ted, ten]. Thanks to index order (a < d < n), the results come out in alphabetical order.

List<String> complete(String prefix) {
  List<String> result = new ArrayList<>();
  TrieNode d = findNode(prefix);
  if (d != null)
    collect(d, new StringBuilder(prefix), result);
  return result;
}
void collect(TrieNode d, StringBuilder path,
           List<String> result) {
  if (d.isEnd) result.add(path.toString());
  for (int i = 0; i < 26; i++)
    if (d.children[i] != null) {
      path.append((char) ('a' + i));
      collect(d.children[i], path, result);
      path.setLength(path.length() - 1);
    }
}

Search · Step through the code

longestCommonPrefix(): Code Trace

kartonrootkkakarkartkartokarton

Trie: kar, kart, karton. Walk down from the root through nodes that have one child and are unmarked.

kartonrootkkakarkartkartokarton

The root is unmarked; count its children: only 'k' is set → count = 1, only = 10.

kartonrootkkakarkartkartokarton

Append 'k', d = "k". This node also has a single child ('a', only = 0).

kartonrootkkakarkartkartokarton

Append 'a', d = "ka"; its single child is 'r' (only = 17).

kartonrootkkakarkartkartokarton

Append 'r', d = "kar". "kar" is a word → the loop ends: even with a single child, the word "kar" does not share a longer prefix.

kartonrootkkakarkartkartokarton

Returns "kar". With only kart and karton in the trie, the walk would continue to "kart" and stop at its end mark: "kart".

String longestCommonPrefix() {
  StringBuilder sb = new StringBuilder();
  TrieNode d = root;
  while (!d.isEnd) {
    int only = -1, count = 0;
    for (int i = 0; i < 26; i++)
      if (d.children[i] != null) { count++; only = i; }
    if (count != 1) break;  // branch or leaf
    sb.append((char) ('a' + only));
    d = d.children[only];
  }
  return sb.toString();
}

Search

Try It: Trie Operations

Lowercase a–z only (at most 10 letters, 14 words). Double circle: word end · orange: last visited node · dashed red: missing branch. Enter = insert.

Deletion

Deletion: Three Cases

1. The word is a prefix of another wordThe last node has children → just set isEnd = false; no node is removed.
androotan: mark removed
2. The word has a tail of its ownWorking back from the end, remove nodes that are childless and unmarked; stop at the shared prefix.
androotremoved
3. The word is not in the trieA branch is missing or the last node is unmarked → nothing changes.
dorootno g
Question: If the trie holds "apple" and "app", how does the structure change when "app" is deleted?
Show answer

Only the isEnd mark of the last 'p' node is cleared. The node cannot be removed, because "apple" continues below it (case 1).

Deletion · Step by step

Deletion Step by Step: app, apple, apt

rootappappleappletapt

Start: app, apple, apt (6 nodes excluding the root). Both scenarios below start from this trie.

rootappappleappletapt

delete("app"): follow a → p → p; node "app" is marked, so the word exists.

rootappleappletapt

isEnd = false. The node has a child ('l'), so it is not removed → case 1. "apple" is still found.

rootappappleappletapt

delete("apple") (again from the original trie): follow a-p-p-l-e; "apple" is found.

rootappappletapt

Node 'e' loses its mark; it has no children → its parent removes it (children[4] = null).

rootappappltapt

Node 'l' is now childless and unmarked → it is removed too.

rootappapptapt

Node "app" is marked (another word) → deletion stops here. Result: app, apt; 4 nodes excluding the root (case 2).

Deletion

Deletion: Recursive Code

// true: the parent may remove d
boolean delete(TrieNode d, String k, int j) {
  if (j == k.length()) {        // end of word
    if (!d.isEnd) return false;
    d.isEnd = false;
    return isEmpty(d);          // childless?
  }
  int i = k.charAt(j) - 'a';
  TrieNode c = d.children[i];
  if (c == null) return false;  // no such word
  if (delete(c, k, j + 1)) {
    d.children[i] = null;       // remove the child
    return !d.isEnd && isEmpty(d);
  }
  return false;
}
  • Call: delete(root, word, 0); the root's return value is ignored.
  • First descend to the end of the word; removal decisions are made on the way back (bottom-up).
  • A node is removed only if it has no children and is unmarked.
  • isEmpty(d): are all 26 children null? (O(M)).
  • Time: O(L · M) with an array; O(L) if a child count is kept.
  • In the counting version, if the word exists (check with search first), do d.count-- at each node on the way down.

Deletion · Step through the code

delete(root, "dad", 0)

danoatddroot

delete(root, "dad", 0): the first character is 'd' → i = 3, c = the root's 'd' child. Removal decisions come on the way back; first we descend.

danoatddroot

delete(d, "dad", 1): 'a' → i = 0, c = node "da". The recursive call goes one level deeper.

danoatddroot

delete(da, "dad", 2): 'd' → i = 3, c = node "dad".

danoatddroot

j = 3 = k.length(): end of word. "dad" is marked, so set isEnd = false.

danoatddroot

isEmpty(dad) = true: the node has no children → return true; the parent may remove it.

danoatdrootremoved

delete(da) returned true → d.children[3] = null: node "dad" is cut off.

danoatdroot

"da" is unmarked and now childless → true: this node can be removed too.

danotdrootremoved

delete(d) returned true → d.children[0] = null: node "da" is cut off.

danotdroot

"d" is unmarked but has the child 'o' for "do" → isEmpty is false → false. Deletion stops here.

danotdroot

At the root, delete(d) returned false → the root removes nothing and returns false (the value is ignored). Remaining words: and, ant, do.

// true: the parent may remove d
boolean delete(TrieNode d, String k,
            int j) {
  if (j == k.length()) {  // done
    if (!d.isEnd) return false;
    d.isEnd = false;
    return isEmpty(d);   // childless?
  }
  int i = k.charAt(j) - 'a';
  TrieNode c = d.children[i];
  if (c == null) return false;
  if (delete(c, k, j + 1)) {
    d.children[i] = null;  // remove child
    return !d.isEnd && isEmpty(d);
  }
  return false;
}

Analysis

Complexity

OperationTimeNotes
insert(word)O(L)One step per character; at most L new nodes
search(word)O(L)Ends earlier if a branch is missing
startsWith(prefix)O(P)P: prefix length
complete(prefix)O(P + S · M)S: number of nodes in the subtree of the prefix node
delete(word)O(L · M)isEmpty scans 26 cells; O(L) with a child count
Memory: O(N · M)N: number of nodes, M: alphabet size (26). N ≤ (total length of all words) + 1.
Independent of nRunning times do not depend on the number of stored words; there is no log n factor as in a BST.

Analysis

Memory: A Worked Example

Trie: and, ant, dad, do

Total characters3 + 3 + 3 + 2 = 11
Number of nodes N1 (root) + 8 = 9
Child references9 × 26 = 234
Used references8 (number of edges)
Empty (null) references226 (≈ 97%)
rootandandtantdaddadodo
Shared prefixes shrink 11 characters to 8 nodes, but 26-element arrays fill most of the memory with null. The bigger the alphabet (Unicode: 65,536+), the worse it gets.

Analysis

Pros and Cons

Pros

  • Fast prefix-based search: autocomplete.
  • Word lookup in O(L) time (L: word length).
  • Overlapping prefixes are stored once: shared memory.
  • Traversal yields words in lexicographic order; no hash collisions.

Cons

  • High memory use, especially with large alphabets.
  • Space complexity O(N · M) (N: number of nodes, M: alphabet size).
  • No gain for sets with few common prefixes (e.g. random keys).
  • Many small objects: not cache-friendly.

Analysis

Ways to Store the Children

RepresentationChild accessMemory per nodeNotes
TrieNode[26] arrayO(1)M referencesSimple, fast; wasteful for sparse nodes
HashMap<Character, TrieNode>O(1) avg.one per childWorks with any alphabet (Turkish, Unicode); no ordering
TreeMap / sorted listO(log M)one per childKeeps alphabetical traversal
Ternary search trie (TST)O(log M) avg.3 referencesLess / equal / greater link at each node
The trie's logic stays the same; only node.children[i] becomes a call such as node.children.get(ch). Choose by alphabet size, memory and the need for ordered output.
array: 26 referencesa and d used, 24 cells wastedHashMap: only 2 entries'a' → node'd' → node

Analysis

Trie, Hash Table or BST?

Operation (n words, length L)TrieHash tableBalanced BST
Exact word lookupO(L)O(L) averageO(L · log n)
Prefix queryO(P)not supportedO(P · log n)
k words with a prefixO(P + S · M)scan the whole tableO(P · log n + k · L)
Sorted listingDFS, O(N · M)needs sortinginorder, O(n)
Worst caseO(L)O(n · L) (collisions)O(L · log n)
Memoryhighlowlow

A hash table also reads all L characters to compute the key's hash, so for exact lookup the two are of the same order. The trie's edge is in prefix and ordered queries. S: number of nodes in the subtree of the prefix node.

Analysis

Lexicographic Order with a Trie: Sorting Strings

  • Input order: dog, ant, do, and, dad. Insert them all into a trie.
  • Traverse the trie in preorder from the root, visiting children in 'a' → 'z' order and emitting a word at each marked node.
  • Output: and, ant, dad, do, dog. A node is emitted before its children, so a prefix comes before its extensions (do < dog).
  • With N total characters and a fixed alphabet, insertion is O(N) and traversal O(N·M) (M = 26 child checks): no need for the O(n log n) comparisons of a comparison sort, each costing O(L).
  • The same idea underlies burst sort and string versions of MSD radix sort.
addtngdao12354root
Green: output order in the preorder traversal.

Applications and Variants

Applications

AutocompleteSearch boxes, IDEs: descend to the typed prefix's node and suggest the words in its subtree (most frequent first).
Spell checkingThe dictionary lives in a trie; for an unknown word, search the trie for nearby words via small edits (insert / delete / replace).
IP routingLongest prefix match with a binary trie (alphabet {0, 1}): address 0110… goes to the longest of 0* / 01* / 011*, namely 011*.
Word games and T9Boggle / word puzzles: abandon a path as soon as it is not a prefix (pruning). Suggesting words from a key sequence.
Longest prefix match: routes 0* → A, 01* → B, 011* → C. For address 0110…, descend 0 → 1 → 1 from the root; there is no '0' branch. The last route seen on the way is C → the packet goes to C.
root0A1B1C0

Applications and Variants · Step through the code

longestMatch(): Routing with a Binary Trie

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

Binary trie (alphabet {0, 1}); some nodes carry a route. addr = "0110": d = root, best = null.

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

k = 0: no route at the root. Bit '0' → d = "0".

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

k = 1: node "0" has A → best = A. Bit '1' → d = "01".

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

k = 2: "01" has B → best = B (the longer prefix wins). d = "011".

011110rootABCDEno 0routes0* → A01* → B011* → C1* → D110* → E

k = 3: "011" has C → best = C. No '0' branch → d = null, return C.

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

New address "1001": k = 0, no route at the root; d = "1".

011110rootABCDEno 0routes0* → A01* → B011* → C1* → D110* → E

k = 1: "1" has D → best = D. No '0' branch → return D. Without a more specific route, the general one applies.

011110rootABCDEroutes0* → A01* → B011* → C1* → D110* → E

"1101": descend 1 → 11 → 110. "11" is an internal node (no route), so best stays D.

011110rootABCDEno 1routes0* → A01* → B011* → C1* → D110* → E

k = 3: "110" has E → best = E; no '1' branch → E. The number of steps is bounded by the address length.

String longestMatch(String addr) {
  BitNode d = root;
  String best = null;
  for (int k = 0; k < addr.length(); k++) {
    if (d.route != null) best = d.route;  // longer prefix
    d = d.children[addr.charAt(k) - '0'];
    if (d == null) return best;
  }
  return d.route != null ? d.route : best;
}

Applications and Variants

The Maximum XOR Pair with a Binary Trie

  • Question: which pair in [3, 10, 5, 25, 2, 8] maximizes a ⊕ b? Brute force tries n² pairs.
  • Insert the numbers into a 5-bit binary trie, starting from the most significant bit.
  • For each x, descend from the root and at every bit take the opposite branch if it exists: that bit of the result becomes 1. A 1 in a higher bit outweighs all lower bits combined, so the greedy choice is correct.
  • For x = 5 = 00101 we want 11010: 1 ✓ (25), 1 ✓, 0 ✓, no 1 → 0, no 0 → 1: we reach 11001 = 25, 5 ⊕ 25 = 28.
nbinary (5 bits)300011100101050010125110012000108010005 ⊕ 2511100 = 28
Methodtimen = 10⁵, B = 30
All pairsO(n²)≈ 5·10⁹ operations
Binary trieO(n·B)≈ 6·10⁶ steps

Applications and Variants

Ternary Search Tries

  • Each node holds one character and three links: left (smaller), mid (equal, go to the next character), right (larger).
  • A node's left/right chain is a small binary search tree over the “sibling” children of a trie.
  • Word ends are marked in the node (ring). Insertion order shapes the tree: the first word inserted becomes the “spine”.
kar, kal, at, ve, kasnodeslinks
Trie with 26-element arrays1010 × 26 = 260
TST99 × 3 = 27
<===<>>=katarlsve
Solid line: mid (=); dashed: left (<) and right (>).
A search for L characters takes L mid steps plus a binary search among siblings at each level: O(L + log n) on average. With a large alphabet (Unicode), a TST keeps memory and speed in balance where arrays cannot (Bentley and Sedgewick, 1997).

Applications and Variants · Step through the code

TST: search("kas") and search("al")

<===<>>=katarlsve

TST: kar, kal, at, ve, kas. search("kas"): d = root ('k'), i = 0.

<===<>>=katarlsve

ch = 'k' = d.c and not the last character → mid link, i = 1. d = 'a'.

<===<>>=katarlsve

ch = 'a' equal → mid, i = 2. d = 'r'.

<===<>>=katarlsve

ch = 's' > 'r' → right link; i is unchanged (same character, searching the siblings). d = 's'.

<===<>>=katarlsve

ch = 's' equal and i = 2 is the last index → return d.end: true. 4 nodes visited for 3 characters.

<===<>>=katarlsve

search("al"): ch = 'a' < 'k' → left, d = 'a' (the branch of the word at).

<===<>>=katarlsve

'a' equal, not the last character → mid, i = 1, d = 't'.

<===<>>=katarlsvenull

'l' < 't' → left: null, false.

boolean search(String s) {
  TstNode d = root;
  int i = 0;
  while (d != null) {
    char ch = s.charAt(i);
    if (ch < d.c) d = d.left;
    else if (ch > d.c) d = d.right;
    else if (i == s.length() - 1) return d.end;
    else { d = d.mid; i++; }
  }
  return false;
}

Applications and Variants

Compressed Tries (Radix / Patricia)

Standard trie: 8 nodes
rootandandtantdaddadodo
Compressed trie: 6 nodes
anddtadorootandantdaddo
  • Chains of single-child, unmarked nodes are merged into one edge, which now carries a substring ("an", "ad").
  • Every internal node has at least 2 children (or an end-of-word mark), so the number of nodes is O(n) (n: number of words).
  • Patricia (Morrison, 1968) is the binary-alphabet version of this idea; it is used in IP routing tables.

Applications and Variants

Suffix Tries

Insert every suffix of the text T = "abab" into a trie:

abab · bab · ab · b

  • P is a substring of T ⇔ P is a prefix of some suffix ⇔ P is a path from the root.
  • Query: startsWith(P) → O(m) (m = |P|), independent of the text length. E.g. "ba" is present, "bb" is not.
  • The price: up to O(n²) nodes for a text of length n.
  • Suffix tree = compressed suffix trie: O(n) nodes, built in O(n) time by Ukkonen's algorithm.
rootabababababbbabbab

Applications and Variants · Step through the code

suffixTrie("abab")

rootta0b1a2b3

t = "abab": its 4 suffixes (abab, bab, ab, b) will be inserted in turn. The trie is just the root.

arootta0b1a2b3

s = 0: start inserting suffix "abab"; d = root. s = 0, j = 0: 'a' branch missing → new node, d moves there ("a").

abrootta0b1a2b3

s = 0, j = 1: 'b' branch missing → new node, d moves there ("ab").

abarootta0b1a2b3

s = 0, j = 2: 'a' branch missing → new node, d moves there ("aba").

ababrootta0b1a2b3

s = 0, j = 3: 'b' branch missing → new node, d moves there ("abab").

ababrootta0b1a2b3

Suffix "abab" done → mark the last node (ring).

abbabrootta0b1a2b3

s = 1: start inserting suffix "bab"; d = root. s = 1, j = 1: 'b' branch missing → new node, d moves there ("b").

abbaabrootta0b1a2b3

s = 1, j = 2: 'a' branch missing → new node, d moves there ("ba").

abbaabbrootta0b1a2b3

s = 1, j = 3: 'b' branch missing → new node, d moves there ("bab").

abbaabbrootta0b1a2b3

Suffix "bab" done → mark the last node (ring).

abbaabbrootta0b1a2b3

s = 2: the path "ab" already exists from inserting abab; descend without creating nodes and mark "ab".

abbaabbrootta0b1a2b3

s = 3: the path "b" exists too; only the mark is added. Shared prefixes are stored once: 8 nodes including the root.

abbaabbrootta0b1a2b3

The suffix trie is ready. "ba" is a path from the root → a substring; there is no path "bb" → not a substring. Query O(m).

TrieNode suffixTrie(String t) {
  TrieNode root = new TrieNode();
  for (int s = 0; s < t.length(); s++) {
    TrieNode d = root;
    for (int j = s; j < t.length(); j++) {
      int i = t.charAt(j) - 'a';
      if (d.children[i] == null)
        d.children[i] = new TrieNode();
      d = d.children[i];
    }
    d.isEnd = true;  // suffix done
  }
  return root;
}

Applications and Variants

Aho–Corasick: Many Patterns in One Pass

  • The patterns (he, she, his, hers) go into a trie; the text is read once, left to right.
  • Failure link (dashed): the longest proper suffix of the node's string that is in the trie. "she" → "he", "sh" → "h", "hers" → "s".
  • When a branch is missing, fall back along the failure link; this is the multi-pattern counterpart of KMP's prefix table.
  • Output sets merge along the failure chain: the "she" node also reports he.
Text "ushers"statefound
u, s, hroot, s, sh—
e (index 3)sheshe, he
rshe → he → her—
s (index 5)hershers
hersisshe{he}{hers}{his}{she, he}

Time O(n + m + z): n text length, m total pattern length, z number of matches (Aho and Corasick, 1975). Used in virus signature scanning and banned-word filters.

Applications and Variants

Which Trie for Which Question?

StructureStoresGood atQuery
Standard triea set of wordsexact word, prefix, autocompleteO(L)
Trie with prefix counts+ words through each node“how many words have this prefix?”O(P)
Compressed triesingle-child chains mergedlong shared prefixes, memoryO(L)
Ternary search trie3 links per nodelarge alphabets, little memoryO(L + log n) avg.
Binary triebit strings (IP, integers)longest prefix match, maximum XORO(B)
Suffix trie / arrayall suffixes (array: O(n) memory)“is P a substring of T?”O(m) / O(m log n)
Aho–Corasickpattern trie + failure linksmany patterns in one passO(n + z)
Same idea throughout: read the key a character (or bit) at a time and share prefixes; variants differ in what a node stores and how children are reached. Choose by question type first (exact, prefix, substring, many patterns), then by alphabet size and memory.

Summary

Summary

TopicKey points
Structureempty root; each edge one character; a word = a path from the root; common prefixes are shared
NodeTrieNode[26] children + boolean isEnd; index = ch − 'a'
Insertcreate a node if missing, else move on; mark the end — O(L)
Searchfalse if a branch is missing; return isEnd at the end — O(L)
Prefixsame path, no mark check — O(P); complete = DFS from the prefix node
Deleteclear the mark; remove childless, unmarked nodes bottom-up
MemoryO(N · M); HashMap / TST / compressed trie for sparse nodes
Variantsradix/Patricia trie, suffix trie → suffix tree, suffix array

Summary

Common Mistakes

Forgetting isEndChecking only that the path exists counts every prefix as a word: "da" appears to be found.
Confusing search with startsWithA prefix query ignores the last node's mark; an exact search must check it.
Characters outside the alphabet'A' − 'a' = −32, 'ş' − 'a' = 254: out of array bounds. Lowercase the input or use a HashMap.
Deleting a shared nodeDeleting the whole path also destroys the other words that share the prefix. Remove only childless, unmarked nodes.
Stating complexity in terms of nTrie operations are not O(log n) or O(n) but O(L): independent of the number of words.
Underestimating memoryEach node holds 26 references; millions of nodes take hundreds of MB. A sparse trie needs a HashMap or compression.

Summary

Sample Problems

Insert the words in order: car, cart, care, cat, dog (trie with 26-element arrays).

  1. Draw the trie. How many nodes, including the root?
  2. What do search("ca") and startsWith("ca") return?
  3. Write the result of complete("car") in order.
  4. How many nodes do delete("car") and (from the original trie) delete("cart") remove?
  5. How many child references are null in total?
  6. How many nodes, including the root, does the compressed trie of the same five words have?
Show answers
  1. c-a-r(•)-t(•), car-e(•), ca-t(•); d-o-g(•) → 9 nodes excluding the root, 10 including it (17 characters).
  2. search("ca") = false ("ca" is unmarked); startsWith("ca") = true.
  3. [car, care, cart] — DFS emits a node before its children, which come in index order ('e' < 't').
  4. delete("car"): 0 nodes (only the mark is cleared); delete("cart"): 1 node ('t'), stopping because "car" is marked.
  5. 10 × 26 = 260 references, 9 of them used (the edges) → 251 null.
  6. 7: root, "ca", r(•), t(•) (cart), e(•) (care), t(•) (cat), "dog"(•); the chains c–a and d–o–g collapse into single nodes.

Summary

Exercises

  1. int countWords(TrieNode d): count the trie's words.
  2. int countPrefix(String prefix): return the number of words with this prefix in O(P).
  3. String longestCommonPrefix(): find the longest common prefix of all words in the trie.
  4. Write a trie with HashMap<Character, TrieNode> children for Turkish letters.
  5. How many nodes, with root, does the trie of A, to, tea, ted, ten, i, in, inn have?
  6. Search "abab"'s suffix trie for "bab" and "bb"; how many comparisons?
Answers / hints
  1. DFS: (d.isEnd ? 1 : 0) + the sum of countWords over all non-null children. O(N · M).
  2. An int count per node, incremented by insert on its path; the prefix node's count is the answer.
  3. From the root, descend while there is one child and isEnd is false; the characters passed are the answer.
  4. children.get(ch) / children.computeIfAbsent(ch, x -> new TrieNode()); no index arithmetic.
  5. A, t, to, te, tea, ted, ten, i, in, inn = 10 nodes + root = 11.
  6. "bab": path b → a → b exists (3 steps). "bb": no 'b' after b → not found (2 steps).

Summary

References

  • Sedgewick and Wayne, Algorithms, 4th ed., 2011 (Section 5.2: Tries).
  • Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., 2022.
  • E. Fredkin, "Trie Memory", Communications of the ACM 3(9), 1960.
  • D. R. Morrison, "PATRICIA — Practical Algorithm To Retrieve Information Coded in Alphanumeric", Journal of the ACM 15(4), 1968.
  • U. Manber and G. Myers, "Suffix Arrays: A New Method for On-Line String Searches", SIAM Journal on Computing 22(5), 1993.
  • D. Gusfield, Algorithms on Strings, Trees, and Sequences, Cambridge University Press, 1997.

The End

Chapter 11: Tries
Previous: Chapter 10: Advanced String Algorithms · Next course: Advanced Algorithms
1 / 1 Sercan KÜLCÜ. All rights reserved.