ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Implement a Trie (Prefix Tree)

MATLAB

Goal -- WPM

Ready
Exercise Algorithm Area
1classdef TrieNode
2properties
3children = containers.Map('char', TrieNode.empty);
4isEndOfWord = false;
5end
6end
7
8classdef Trie
9properties
10root;
11end
12
13methods
14function obj = Trie()
15% Constructor initializes the Trie with a root node.
16obj.root = TrieNode();
17end
18
19function insert(obj, word)
20% Inserts a word into the Trie.
21% Handles empty strings and ensures correct node creation.
22if isempty(word)
23return; % Cannot insert an empty string
24end
25
26currentNode = obj.root;
27for i = 1:length(word)
28char = word(i);
29if ~isKey(currentNode.children, char)
30currentNode.children(char) = TrieNode();
31end
32currentNode = currentNode.children(char);
33end
34currentNode.isEndOfWord = true;
35end
36
37function found = search(obj, word)
38% Searches for a word in the Trie.
39% Returns true if the word exists and is marked as end-of-word.
40if isempty(word)
41found = false; % Empty string is not considered a valid word search
42return;
43end
44
45currentNode = obj.root;
46for i = 1:length(word)
47char = word(i);
48if ~isKey(currentNode.children, char)
49found = false;
50return;
51end
52currentNode = currentNode.children(char);
53end
54found = currentNode.isEndOfWord;
55end
56
57function found = startsWith(obj, prefix)
58% Checks if there is any word in the Trie that starts with the given prefix.
59if isempty(prefix)
60found = true; % An empty prefix matches all words
61return;
62end
63
64currentNode = obj.root;
65for i = 1:length(prefix)
66char = prefix(i);
67if ~isKey(currentNode.children, char)
68found = false;
69return;
70end
71currentNode = currentNode.children(char);
72end
73found = true;
74end
75end
76end
Algorithm description viewbox

Implement a Trie (Prefix Tree)

Algorithm description:

This code implements a Trie (prefix tree) data structure in MATLAB, which is efficient for storing and retrieving strings based on their prefixes. It includes methods for inserting words, searching for exact word matches, and checking for the existence of words with a given prefix. Tries are widely used in applications like autocomplete systems, spell checkers, and IP routing tables.

Algorithm explanation:

The Trie is implemented using a `TrieNode` class, where each node can have multiple children (one for each possible character) and a flag indicating if it marks the end of a word. The `insert` method traverses the Trie, creating new nodes as needed, and marks the final node for the word. The `search` method follows the path for the given word and checks the `isEndOfWord` flag. The `startsWith` method checks if a path for the prefix exists. The time complexity for insertion, search, and prefix search is O(L), where L is the length of the word or prefix, as it involves a single traversal. The space complexity depends on the number of nodes, which is proportional to the total number of characters in all inserted words. Edge cases like empty strings and non-existent prefixes are handled. The structure ensures that all prefixes of a valid word are also implicitly represented.

Pseudocode:

class TrieNode:
  children = map()
  isEndOfWord = false

class Trie:
  root = TrieNode()

  function insert(word):
    currentNode = root
    for each character in word:
      if character not in currentNode.children:
        currentNode.children[character] = TrieNode()
      currentNode = currentNode.children[character]
    currentNode.isEndOfWord = true

  function search(word):
    currentNode = root
    for each character in word:
      if character not in currentNode.children:
        return false
      currentNode = currentNode.children[character]
    return currentNode.isEndOfWord

  function startsWith(prefix):
    currentNode = root
    for each character in prefix:
      if character not in currentNode.children:
        return false
      currentNode = currentNode.children[character]
    return true