Topic - Q Implement Trie (Prefix Tree) - Information O..
Topic - Q Implement Trie (Prefix Tree) - Information O..
b) Search (word):
function search(root, word):
currentNode = root
for each character 'char' in word:
if 'char' is not a key in currentNode.children:
return false // Prefix not found
currentNode = currentNode.children['char']
return currentNode.isEndOfWord // True if the exact word exists
c) startsWith (prefix):
function startsWith(root, prefix):
currentNode = root
for each character 'char' in prefix:
if 'char' is not a key in currentNode.children:
return false // Prefix not found
currentNode = currentNode.children['char']
return true // Prefix exists