Data Structures19 sections · 729 units
Open in Course

Array vs Map Children

Implementation trade-offs

Two common ways to store children in a trie node: Array (fixed alphabet):

children: array of 26 (for lowercase letters)
# Access: children[ord(c) - ord('a')]

Hash map (flexible):

children: map from char to TrieNode
# Access: children.get(c)

Trade-offs:

  • Array: O(1)O(1) access, wastes space for sparse tries
  • Map: Slight overhead, memory-efficient for large alphabets

For lowercase English letters, array is typically faster.

For Unicode or variable alphabets, map is necessary. Choose based on your alphabet size and sparsity. Time: O(L)O(L). Space: O(L)O(L).