0
0
DSA Javascriptprogramming

Why Trie Exists and What Hash Map Cannot Do for Strings in DSA Javascript - Why This Pattern

Choose your learning style9 modes available
Mental Model
A trie stores words by their letters step-by-step, letting us find all words with the same start easily. Hash maps store whole words as keys, so they can't quickly find words sharing beginnings.
Analogy: Imagine a tree of roads where each letter is a turn. To find all houses on a street starting with 'cat', you just follow the road 'c' -> 'a' -> 't' and see all houses there. A hash map is like a list of full addresses; you can't find all houses on 'cat' street without checking every address.
root
 ↓
 c -> a -> t -> [end]
           ↓
           s -> [end]

Hash Map:
{
  "cat": true,
  "cats": true
}
Dry Run Walkthrough
Input: words: ["cat", "cats", "car"], find all words starting with "ca"
Goal: Show how trie quickly finds all words starting with "ca" while hash map cannot do this efficiently
Step 1: Insert "cat" into trie letter by letter
root -> c -> a -> t -> [end]
Why: We build the path for the word "cat" step by step
Step 2: Insert "cats" by extending from "cat" with letter 's'
root -> c -> a -> t -> s -> [end]
Why: Words sharing prefix reuse existing nodes, saving space
Step 3: Insert "car" branching from 'a' with letter 'r'
root -> c -> a -> t -> s -> [end]
               ↓
               r -> [end]
Why: Trie branches to represent different words sharing prefix
Step 4: Search prefix "ca" by following nodes 'c' then 'a'
root -> c -> a ↑
Why: We reach the node representing prefix "ca" to find all words starting with it
Step 5: Collect all words below node 'a': "cat", "cats", "car"
root -> c -> a -> t -> s -> [end]
               ↓
               r -> [end]
Why: Trie structure allows easy collection of all words sharing prefix
Result:
root -> c -> a -> t -> s -> [end]
               ↓
               r -> [end]
Words starting with "ca": "cat", "cats", "car"
Annotated Code
DSA Javascript
class TrieNode {
  constructor() {
    this.children = new Map();
    this.isEnd = false;
  }
}

class Trie {
  constructor() {
    this.root = new TrieNode();
  }

  insert(word) {
    let node = this.root;
    for (const char of word) {
      if (!node.children.has(char)) {
        node.children.set(char, new TrieNode());
      }
      node = node.children.get(char); // advance node to child
    }
    node.isEnd = true; // mark end of word
  }

  startsWith(prefix) {
    let node = this.root;
    for (const char of prefix) {
      if (!node.children.has(char)) {
        return [];
      }
      node = node.children.get(char); // advance node to prefix end
    }
    return this._collectAllWords(node, prefix);
  }

  _collectAllWords(node, prefix) {
    const words = [];
    if (node.isEnd) words.push(prefix); // add word if end reached
    for (const [char, childNode] of node.children) {
      words.push(...this._collectAllWords(childNode, prefix + char)); // recurse children
    }
    return words;
  }
}

// Driver code
const trie = new Trie();
const words = ["cat", "cats", "car"];
for (const word of words) {
  trie.insert(word);
}
const prefix = "ca";
const result = trie.startsWith(prefix);
console.log(`Words starting with "${prefix}": ${result.join(", ")}`);
node = node.children.get(char); // advance node to child
advance node to next letter node to build or traverse word
node.isEnd = true; // mark end of word
mark current node as end of a valid word
node = node.children.get(char); // advance node to prefix end
traverse trie nodes to reach prefix end
if (node.isEnd) words.push(prefix); // add word if end reached
collect word if current node marks word end
words.push(...this._collectAllWords(childNode, prefix + char)); // recurse children
recursively collect all words under current node
OutputSuccess
Words starting with "ca": cat, cats, car
Complexity Analysis
Time: O(m + n) where m is prefix length and n is total letters in subtree because we traverse prefix then collect all words
Space: O(n) for storing all nodes and collected words in recursion
vs Alternative: Hash map lookup is O(1) for full words but cannot efficiently find all words sharing a prefix without scanning all keys, which is O(k) where k is number of keys
Edge Cases
Empty prefix search
Returns all words in trie because prefix is empty
DSA Javascript
if (!node.children.has(char)) { return []; }
Prefix not in trie
Returns empty list because prefix path does not exist
DSA Javascript
if (!node.children.has(char)) { return []; }
Insert duplicate word
Marks same end node again, no duplicate nodes created
DSA Javascript
node.isEnd = true; // mark end of word
When to Use This Pattern
When you need to find all words sharing a prefix quickly, reach for the trie pattern because hash maps cannot efficiently handle prefix searches.
Common Mistakes
Mistake: Trying to use hash map keys to find prefix matches by scanning all keys
Fix: Use a trie to store words letter by letter so prefix traversal is direct and efficient
Summary
A trie stores words letter by letter to enable fast prefix searches.
Use a trie when you need to find or autocomplete words sharing the same start.
The key insight is that tries share common prefixes as shared paths, unlike hash maps that treat words as separate keys.