We can use a while loop to determine the appropriate place to assign our word as a child key in a children hash. It does this by trying to look for each of our word's characters in the current node's children hash, and if there's a match, we try to place it in the next child's children hash. This handles the case where lineages like c -> a -> t in "category" may have already been previously added with a word like "cater".

1func (t *Trie) Add(val string) {
2  currentNode := t.head
3  var newNode *Node
4  currentChar := string(val[0])
5  val = val[1:]
6
7  // See how deep we can get in any existing branches
8  for _, ok := currentNode.children[currentChar]; ok && len(currentChar) > 0; _, ok = currentNode.children[currentChar] {
9    currentNode = currentNode.children[currentChar]
10    currentChar = string(val[0])
11    val = val[1:]
12  }
13}