Goal: Store every word as a path of letters from a shared root, one node per character.
Building the trie
For each word, start at the root and walk its letters. At every letter, if the current node has no child for it, create one. Then move into that child.
After the word's last letter, mark that node as an end. A dictionary of plain nested maps is enough: each node is a map from a letter to the next node.
node = {}
node["c"] = {} # a child for "c"
node["c"]["a"] = {} # then "a" below itInsert "cat": walk from the root, creating c, a, t. Mark the last node: a word ends here.
Searching the trie
To check whether the target is one of the inserted words, walk its letters the same way, but never create a node. If a letter is missing, the target was never inserted, like looking for a street that isn't on the map.
If every letter is found, the target exists only if the final node is marked as an end.
node = root
for c in word:
if c not in node:
return False
node = node[c]