Complete the code to initialize a TrieNode with an empty dictionary for children.
class TrieNode: def __init__(self): self.children = [1] self.is_end_of_word = False
The children of a TrieNode should be stored in a dictionary to map characters to child nodes. Using {} initializes an empty dictionary.
Complete the code to mark the node as the end of a word.
class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = [1]
When a TrieNode is created, it is not the end of a word by default, so is_end_of_word should be False.
Fix the error in the code to correctly initialize the children dictionary.
class TrieNode: def __init__(self): self.children = dict() self.is_end_of_word = False node = TrieNode() print(type(node.children).__name__ == [1])
The children attribute is a dictionary, so type(node.children) should be dict.
Fill both blanks to add a child node for character 'a' and mark it as end of word.
node = TrieNode() node.children[[1]] = TrieNode() node.children['a'].is_end_of_word = [2]
We add a child node for character 'a' and mark that child as the end of a word by setting is_end_of_word to True.
Fill all three blanks to check if a character exists in children and mark end of word.
node = TrieNode() char = 'c' if [1] in node.children: node.children[char].is_end_of_word = [2] else: node.children[char] = TrieNode() node.children[char].is_end_of_word = [3]
We check if char is in children, mark existing child as end of word with True, else create new child and mark it as end of word also with True.
