A map whose key is always a {@link CharSequence} (usually a {@link String}), and whose value is a char, usually used to store an index into some other data structure. This class is intended to serve as a base class or helper class for natural-language dictionaries and similar data structures.
The values {@link Character#MIN_VALUE} (0x0000) and {@link Character#MAX_VALUE} (0xFFFF) have special significanceto the internal structures in this class. Mapping strings that include these values will have unpredictable and probably horrible results.
Ternary means "grouped in threes". Each node in the tree has up to three child nodes: a "high" branch for values greater than that in the node, a "low" branch for values less than that in the node, and an "equal" branch for the value that is equal to that in the node. This provides efficiency in both speed and memory for processing textual data. The memory benefits come from the fact that similar Strings are actually stored in the same location. A properly balances tree is fast because it does not need to traverse a large number of nodes to find a value.
A ternary search tree is a hybrid between a binary tree and a digital search tree (trie). Ternary trees have some nice properties, including the following:
In this implementation the value is a char (think "unsigned short"), and is stored in the leaf nodes of the tree. This limits the number of nodes to 65,536. Branches that contain only one key are compressed to one node by storing a pointer to the trailer substring of the key. A compressed branch needs only one node per key plus the size of the string key. Compressed branches are decompressed as needed when another key with same prefix is inserted. This saves a lot of space, especially for long keys.
This class could be extended to or embedded in a general map that would allow arbitrary objects to be values in the map. One scheme for doing so would be to create an array of the value Objects, and use this class to store the indexes to that array.
|
|