Search⌘ K
AI Features

Implement Trie (Prefix Tree)

Understand how to implement a trie, or prefix tree, supporting three core operations: inserting words, searching full words, and searching prefixes. This lesson helps you efficiently manage and query strings, a common pattern in coding interviews. You will practice implementing these functions to enhance your problem-solving skills using tries.

Statement

Trie is a tree-like data structure used to store strings. The tries are also called prefix trees because they provide very efficient prefix-matching operations. Implement a trie data structure with three functions that perform the following tasks:

  • Insert (word): This inserts a word into the trie.

  • Search (word): This searches the given word in the trie and returns TRUE if the complete word is found (i.e., all characters match and the last node is marked as the end of a word). Otherwise, return FALSE.

  • Search prefix (prefix): This searches the given prefix ...