private static TernaryTrieNode InsertInternal(string key, int index, TernaryTrieNode currentNode) { var node = currentNode ?? new TernaryTrieNode { Value = key[index] }; if (key.Length == index + 1 && node.Value == key[index]) { node.IsWord = true; } else { if (node.Value == key[index]) { node.EqualToCurrentCharacter = InsertInternal(key, index + 1, node.EqualToCurrentCharacter); } else if (node.Value < key[index]) { node.GreaterThanCurrentCharacter = InsertInternal(key, index, node.GreaterThanCurrentCharacter); } else { node.LessThanCurrentCharacter = InsertInternal(key, index, node.LessThanCurrentCharacter); } } return(node); }
public void Insert(string key) { if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException(nameof(key)); } root = InsertInternal(key, 0, root); }