This approach involves creating a basic Trie data structure using a tree of nodes. Each node represents a character in a word. Starting from a root node, each character of the word is inserted sequentially, with branches representing the progression to subsequent characters. This forms a chain of nodes (linked in a tree-like manner) from the root to the nodes representing complete words. For searching, we traverse these nodes based on the characters of the input word or prefix to check for existence.
Time Complexity: Insert, search, and startsWith operations are O(m), where m is the length of the word/prefix.
Space Complexity: O(26 * n * m) in the worst case, where n is the number of inserted words and m is the average length of the words. Each insertion can potentially add m nodes with 26 possible children each.
1class TrieNode {
2 constructor() {
3 this.children = {};
4 this.isEndOfWord = false;
5 }
6}
7
8class Trie {
9 constructor() {
10 this.root = new TrieNode();
11 }
12
13 insert(word) {
14 let node = this.root;
15 for (let char of word) {
16 if (!node.children[char]) {
17 node.children[char] = new TrieNode();
18 }
19 node = node.children[char];
20 }
21 node.isEndOfWord = true;
22 }
23
24 search(word) {
25 let node = this.root;
26 for (let char of word) {
27 if (!node.children[char]) {
28 return false;
29 }
30 node = node.children[char];
31 }
32 return node.isEndOfWord;
33 }
34
35 startsWith(prefix) {
36 let node = this.root;
37 for (let char of prefix) {
38 if (!node.children[char]) {
39 return false;
40 }
41 node = node.children[char];
42 }
43 return true;
44 }
45}
This JavaScript implementation features a TrieNode class containing a dictionary for nodes and a boolean to signal word ending. Trie, built from an instance of TrieNode, allows word insertion by guiding a cursor from the root node through a character path, creating intermediate child nodes as necessary, and finally marking the terminal node of the word complete. The search method checks the presence and end marking, navigating each character sequentially. The startsWith method follows the provided character prefix completely while verifying node availability without requiring end marking.
This approach relies on maps (or hashmaps) for storing children nodes dynamically. The advantage of using maps over arrays in this context arises from reduced space consumption when handling sparse trees since only existing characters are stored. Nodes manage their children using hash references, leading to more flexible branching.
Time Complexity: O(m) per operation where m is the word/prefix length.
Space Complexity: O(n * m), where n estimates the word count and m accounts for varying lengths since nodes only maintain necessary mappings.
1class TrieNode:
2 def __init__(self):
3 self.children = {}
4 self.is_end_of_word = False
5
6class Trie:
7 def __init__(self):
8 self.root = TrieNode()
9
10 def insert(self, word: str) -> None:
11 node = self.root
12 for char in word:
13 node = node.children.setdefault(char, TrieNode())
14 node.is_end_of_word = True
15
16 def search(self, word: str) -> bool:
17 node = self.root
18 for char in word:
19 if char not in node.children:
20 return False
21 node = node.children[char]
22 return node.is_end_of_word
23
24 def startsWith(self, prefix: str) -> bool:
25 node = self.root
26 for char in prefix:
27 if char not in node.children:
28 return False
29 node = node.children[char]
30 return True
In this Python solution, each TrieNode contains a dictionary (children) providing direct access to only required child characters. This approach optimizes storage, especially reducing overhead when branching is sparse. The root provides the base node in the Trie. The insert method applies setdefault for dictionary operation - simplifying conditional insertion logic. Both search and startsWith check the presence of each character in sequence traversing the root to target node, utilizing dictionary fast access and probing methods.