Bitpath

Lesson 2 · Unit 04

Insert, search, starts-with

Three walks that cost O(length), and word versus prefix

11 exercisesabout 3 min

What you practise

  • O(length) operations
  • Word vs prefix

Try a question from this lesson

Pick one

A trie holds only the word cart. You follow the letters c, a, r from the root and never hit a missing link. What can you conclude?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Insert: walk and create

Go down the word one letter at a time. Where the next letter has no node yet, create one. After the last letter, switch on the end flag.

That is one step per letter: O(length of the word), no matter how many words the trie already holds.

def insert(root, word):
    node = root
    for ch in word:
        if ch not in node.kids:
            node.kids[ch] = TrieNode()
        node = node.kids[ch]
    node.end = True
function insert(root, word) {
  let node = root;
  for (const ch of word) {
    if (!node.kids.has(ch)) {
      node.kids.set(ch, new TrieNode());
    }
    node = node.kids.get(ch);
  }
  node.end = true;
}
function insert(
  root: TrieNode,
  word: string,
): void {
  let node = root;
  for (const ch of word) {
    if (!node.kids.has(ch)) {
      node.kids.set(ch, new TrieNode());
    }
    node = node.kids.get(ch)!;
  }
  node.end = true;
}

Python notefor ch in word walks a string one character at a time, and ch not in node.kids tests the keys of the dict.

TypeScript notekids.get(ch) has type TrieNode | undefined, since a lookup can miss. The ! promises the key is there: the lines above have just made sure of it.

Concept

One walk, two questions

Search and starts-with begin the same way: follow the letters down from the root, creating nothing.

This helper returns the node where the text ends, or nothing as soon as a letter is missing. Search then asks whether that node is flagged. Starts-with only asks whether there is a node.

def walk(root, text):
    node = root
    for ch in text:
        if ch not in node.kids:
            return None
        node = node.kids[ch]
    return node
function walk(root, text) {
  let node = root;
  for (const ch of text) {
    if (!node.kids.has(ch)) return null;
    node = node.kids.get(ch);
  }
  return node;
}
function walk(
  root: TrieNode,
  text: string,
): TrieNode | null {
  let node = root;
  for (const ch of text) {
    if (!node.kids.has(ch)) return null;
    node = node.kids.get(ch)!;
  }
  return node;
}

Python noteReturning None from inside the loop ends the walk at the first missing letter, however long the text is.

JavaScript noteReturning null from inside the loop ends the walk at the first missing letter, however long the text is.

TypeScript noteThe return type TrieNode | null makes every caller deal with the missing case before it can touch the node.

8 more exercises in the app

  • 3× type the answer
  • 2× fill the blanks
  • 1× multiple choice
  • 1× true or false
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy