Ternary search tree

The figure below shows a ternary search tree with the strings "cute","cup","at","as","he","us" and "i": As with other trie data structures, each node in a ternary search tree represents a prefix of the stored strings.

Inserting keys in alphabetical order is one way to attain the worst possible degenerate tree.

A lookup procedure begins by checking the root node of the tree and determining which of the following conditions has occurred.

Alternatively, ternary search trees are effective when storing a large number of relatively short strings (such as words in a dictionary).

[1] Hashtables can also be used in place of ternary search trees for mapping strings to values.

There is some evidence that shows ternary search trees running faster than hash maps.

[1] Additionally, hash maps do not allow for many of the uses of ternary search trees, such as near-neighbor lookups.

If storing dictionary words is all that is required (i.e., storage of information auxiliary to each word is not required), a minimal deterministic acyclic finite state automaton (DAFSA) would use less space than a trie or a ternary search tree.

This is because a DAFSA can compress identical branches from the trie which correspond to the same suffixes (or parts) of different words being stored.

Ternary search trees can be used to solve many problems in which a large number of strings must be stored and retrieved in an arbitrary order.