Abstract
A Ternary Search Tree (TST) is a hybrid data structure that combines the prefix-searching logic of a Multiway Trie with the space-efficient storage of a Binary Search Tree (BST). Each node stores a single character and has exactly three potential children: Left, Middle, and Right.
- Category: Hybrid Character Branching Tree
- Branching Factor: Exactly 3 pointers per node (
leftChild,middleChild,rightChild).- Key Advantage: Eliminates the empty pointer array overhead of Multiway Tries while retaining prefix searching capabilities.
Structural Logic
In a TST, the relationship between a node and its children is determined by character comparisons and word progression:
- Left Child: Stores characters that are alphabetically smaller than the current node’s character.
- Right Child: Stores characters that are alphabetically larger than the current node’s character.
- Middle Child: Represents the next character in the current word string.
- Word Nodes: Nodes representing the end of a valid word are marked (e.g., colored blue).

Core Operations
Find(key)
To search for a key, compare the current character of the key with the current node’s label:
- Key Char < Node Label: Move to the Left child.
- Key Char > Node Label: Move to the Right child.
- Key Char == Node Label:
- If this is the last character of the key, check if the node is a word-node.
- Otherwise, move to the Middle child and advance to the next character in the key.
Algorithm 49 Ternary Search Tree Find Operation
procedure Find(key, root)
while and do
if then
else if then
else
if then
return node.isWordNode
return
Walk-Through Example (Success)
Searching for the word "mid":

- Start with
nodeas root ('c') and letter as'm'. 'm' > 'c', so move to right child ('m').'m' == 'm', advance to middle child ('e') and letter'i'.'i' > 'e', so move to right child ('i').'i' == 'i', advance to middle child ('n') and letter'd'.'d' < 'n', so move to left child ('d').'d' == 'd', last letter reached, and node is marked as word-node Success!
Walk-Through Example (Failure)
Searching for the word "cme":

- Start with
nodeas root ('c') and letter as'c'. 'c' == 'c', move to middle child ('a') and letter'm'.'m' > 'a', but node ('a') has no right child Failure!
Insert(key)
Compare the current character with the node’s label:
- Key Char < Node Label: Move Left. If
NULL, create a new Left child and build a Middle-child “spine” for remaining characters. - Key Char > Node Label: Move Right. If
NULL, create a new Right child and build a Middle-child “spine” for remaining characters. - Key Char == Node Label: If last character, mark as word-node. Otherwise, move Middle and advance character.
Algorithm 50 Ternary Search Tree Insertion
procedure Insert(key, root)
while do
if then
if then
else if then
if then
else
if then
return
if then
Walk-Through Example
Inserting the word "cabs" into an existing tree:

- Start at root (
'c') with letter'c'. Match move to middle ('a'), next letter'a'. - Match at (
'a') move to middle ('l'), next letter'b'. 'b' < 'l', but'l'has no left child create left child ('b').- Move to (
'b'), create middle child ('s'), mark as word-node.
Remove(key)
Use search logic to locate the target node representing the last character of the key. Unmark its isWordNode status.
Algorithm 51 Ternary Search Tree Removal
procedure Remove(key, root)
LocateTerminalNode(key, root)
if and then
Advanced Features
Alphabetical Iteration
Because TSTs maintain the BST property (), an In-Order Traversal retrieves words in sorted order.
Algorithm 52 TST Ascending In-Order Traversal
procedure AscendingInOrder(node)
if node == then
return
AscendingInOrder()
if node.isWordNode then
Output()
AscendingInOrder()
AscendingInOrder()
Auto-Complete
To find all words starting with a prefix:
- Traverse the TST to the node representing the end of the prefix.
- Perform an
AscendingInOrdertraversal on that node’s middle child subtree.
Evaluation for Lexicon ADT
| Feature | BST (AVL) | Multiway Trie | Ternary Search Tree |
|---|---|---|---|
| Search (Avg) | |||
| Space Efficiency | High | Very Low | High |
| Alphabetical | Yes | Yes | Yes |
| Auto-Complete | No | Yes | Yes |
Key Takeaways
- Space over Speed: TSTs avoid the “wasted pointer” problem of Multiway Tries because each node only has 3 pointers instead of (e.g., 26 or 256).
- Balance Matters: Like a BST, a TST can become skewed if words are inserted in a poor order. Shuffling words before insertion is a common optimization.
- The Middle Ground: It provides the prefix-matching power of a Trie with the memory footprint of a Tree.