Menu
Coddy logo textTech

TrieNode Class

Lesson 3 of 14 in Coddy's Tries - Data Structures Series #8 course.

Every node in a trie is one small object that holds two pieces of information: the children hanging off it (a map from a character to the next node) and a flag isEndOfWord that marks whether the path from the root to this node spells a complete inserted word.

That is the whole class. No values, no keys: the position of a node in the tree IS its meaning. A path of edges labeled c, a, t from the root reaches the node where we set isEndOfWord = true when we insert "cat".

Let's start by building this TrieNode class - the Trie class in the next lesson will use it.

challenge icon

Challenge

Easy

Write a class TrieNode with a constructor that takes no input.

Initialize two fields:

  • children set to an empty map (or your language's equivalent for a character-to-node map).
  • isEndOfWord set to false.

Try it yourself

#include <stdio.h>
#include "trienode.h"

int main() {
    TrieNode* n = TrieNode_new();
    printf("%s %s\n",
           TrieNode_childrenCount(n) == 0 ? "true" : "false",
           n->isEndOfWord ? "true" : "false");
    return 0;
}

All lessons in Tries - Data Structures Series #8