Trie (Prefix Tree)
Tree-like data structure for efficient storage and retrieval of strings, particularly for prefix-based operations. Each node represents a character, paths from root to leaves form strings. Invented by Edward Fredkin in 1960, essential for autocomplete, spell checking, IP routing.
Visualization
Interactive visualization for Trie (Prefix Tree)
Interactive visualization with step-by-step execution
Implementation
1class TrieNode {
2 children: Map<string, TrieNode> = new Map();
3 isEndOfWord: boolean = false;
4}
5
6class Trie {
7 private root: TrieNode = new TrieNode();
8
9 insert(word: string): void {
10 let node = this.root;
11 for (const char of word) {
12 if (!node.children.has(char)) {
13 node.children.set(char, new TrieNode());
14 }
15 node = node.children.get(char)!;
16 }
17 node.isEndOfWord = true;
18 }
19
20 search(word: string): boolean {
21 let node = this.root;
22 for (const char of word) {
23 if (!node.children.has(char)) return false;
24 node = node.children.get(char)!;
25 }
26 return node.isEndOfWord;
27 }
28
29 startsWith(prefix: string): boolean {
30 let node = this.root;
31 for (const char of prefix) {
32 if (!node.children.has(char)) return false;
33 node = node.children.get(char)!;
34 }
35 return true;
36 }
37
38 delete(word: string): boolean {
39 return this.deleteHelper(this.root, word, 0);
40 }
41
42 private deleteHelper(node: TrieNode, word: string, index: number): boolean {
43 if (index === word.length) {
44 if (!node.isEndOfWord) return false;
45 node.isEndOfWord = false;
46 return node.children.size === 0;
47 }
48
49 const char = word[index];
50 const childNode = node.children.get(char);
51 if (!childNode) return false;
52
53 const shouldDeleteChild = this.deleteHelper(childNode, word, index + 1);
54
55 if (shouldDeleteChild) {
56 node.children.delete(char);
57 return node.children.size === 0 && !node.isEndOfWord;
58 }
59
60 return false;
61 }
62}Deep Dive
Theoretical Foundation
Trie stores strings character by character in tree nodes. Root is empty, each edge represents character, nodes mark end of words. Unlike hash tables, Trie provides prefix-based operations efficiently. Space-time tradeoff: uses more memory but faster for prefix queries.
Complexity
Time
O(m)
O(m)
O(m)
Space
O(ALPHABET_SIZE * m * n)
Applications
Industry Use
Autocomplete systems
Spell checkers
IP routing (longest prefix match)
T9 predictive text
Dictionary implementations
Genome sequence analysis
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.