BTI Beyond The Interviews

DSA · 8 sections

Tries (Prefix Trees)

Prefix trees for autocomplete, spell-check, and XOR-max problems.

Premium · preview 99 cards Updated 24 Oct 2026
Free to preview. The first 3 cards and the full outline are below — the rest of this sheet is part of the DSA & Data Structures bundle.

What's inside

Preview — first 3 of 99 cards

What is a trie (prefix tree)?

A tree where each node represents a prefix; edges are labeled by characters. The root represents the empty string. A path from root spells a prefix; marked terminal nodes complete words.

class TrieNode:
    def __init__(self):
        self.children = {}
        self.is_end = False

What does a trie node store?

Children keyed by character (array or map) plus an end-of-word flag. Optional: count, frequency, or value payload.

struct Node {
  Node* ch[26];
  bool isEnd;
  int count; // words through this node
};

How does insert work?

Start at root; for each character create child if missing and descend; mark final node isEnd=True. Time O(L).

def insert(root, word):
    node = root
    for c in word:
        if c not in node.children:
            node.children[c] = TrieNode()
        node = node.children[c]
    node.is_end = True

96 more cards in this sheet

Unlock the DSA & Data Structures bundle — ₹399

One-time payment. Unlocks this sheet and 8 other sheets in the bundle, plus a typeset PDF of each.

Unlock the bundle See all bundles

Or get all 76 sheets for ₹999 — one payment, every future sheet included.