At first glance, a TernarySearchTree may not seem to gain much over a Binary Search Tree [BinaryTree]: it just seems to use more space and algorithmic complexity. In the BST you just stick at the current node if the n'th character matched, although you can't advance n right away when you go left/right. Likewise, it may seem to simply be a StringTrie where the data structure to look up individual letters is a BinaryTree, rather than an array.
In a TST, each comparison is much simpler than in a BST, and more complex than in a naive StringTrie. Each node requires only a constant amount of storage (one character, for a TST containing strings of characters), and that comparison is a constant-time operation (in most cases, a single op). The recursion is truly trivial. Compare this to a binary search tree, where you may have to compare multiple characters at any node, each node must contain an entire string, etc. Think of it this way: a BST containing the set { "foo", "bar", "foobar" } must contain those three strings, as such (standard sorting, given insertion order):
foo / \ bar foobarThe corresponding TST (which is, less sensitive to insertion order) is (with $ as the end-of-string character, less than any other character):
f /| b o | | a o // This comment is here because | |\ // trailing backslashes r $ b // can confuse wiki. | | $ a | r | $Many more nodes, but
TSTs broadly offer better performance than a BST, and takes up less space that a Trie. One can refer to that as an average. Or one can refer to it as a solution to two problems with the BST and the StringTrie.
References: