class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_end_of_word trie = Trie() words = ["apple", "app", "bat"] for w in words: trie.insert(w) print(trie.search("app"))
The word "app" was inserted into the Trie. The search method returns True only if the word exists and is marked as an end of a word. Since "app" was inserted, the output is True.
class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_end_of_word trie = Trie() words = ["cat", "car", "dog"] for w in words: trie.insert(w) print(trie.search("cow"))
The word "cow" was not inserted into the Trie. The search method returns False when any character is missing in the Trie path.
def search(self, word): node = self.root for char in word: if char not in node.children: return False node = node.children[char] return node.is_end_of_word
The search method returns True only if the full word exists and is marked as an end of a word. "hel" is a prefix but not a complete word, so it returns False.
class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True def search(self, word): node = self.root for char in word: node = node.children[char] return node.is_end_of_word trie = Trie() trie.insert("test") print(trie.search("team"))
The search method does not check if the character exists in children before accessing it. If the character is missing, it raises a KeyError.
class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False class Trie: def __init__(self): self.root = TrieNode() def insert(self, word): node = self.root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True def count_words(self): def dfs(node): count = 1 if node.is_end_of_word else 0 for child in node.children.values(): count += dfs(child) return count return dfs(self.root) trie = Trie() words = ["go", "gone", "go", "gopher", "gone"] for w in words: trie.insert(w) print(trie.count_words())
The words inserted are "go", "gone", "go" (duplicate), "gopher", and "gone" (duplicate). Unique words are "go", "gone", and "gopher" totaling 3.
