Ternary Search Tree

Introduction:

Ternary Search Tree is a trie data structure commonly used as a low-memory alternative to trie in various applications such as spell check and looking for nearby neighbors.

Ternary Search Trees (TST) stand as a sophisticated and efficient data structure that combines the best of binary search trees and trie structures. Introduced by Jon Bentley and Robert Sedgewick in their seminal paper "Ternary Search Trees" in 1998.

To comprehend the ternary search tree, we must first grasp the concept of the trie. A trie, also known as a digital tree, radix tree, or prefix tree, is a type of search tree, an ordered tree data structure used to hold a dynamic set or associative array using strings as keys.

Structure of Ternary Search Trees:

A Ternary Search Tree is essentially a type of trie where each node has three children instead of the traditional two in binary trees. The nodes are designed to store individual characters from keys in a sorted order. This unique structure enables efficient searching, insertion, and deletion operations.

1. Node Structure:

  • Key: Represents the character associated with the node.
  • Data: Holds the value associated with the character.
  • Left, Middle, and Right Pointers: Point to the three children nodes.

2. Balanced Structure:

  • TSTs inherently maintain a balanced structure, ensuring efficient searching and space optimization.
  • The middle child node helps in balancing the tree, preventing skewed structures commonly found in binary trees.

Let's consider an example to illustrate the structure of a Ternary Search Tree:

In this example, the TST represents the strings "CAT," "ME," "MET," "MEX," "METAL," and "METEOR." Each node in the tree corresponds to a character in these strings.

Representation of Ternary Search Trees:

In the trie (standard) data structure, each node contains 26 pointers for its children, but in a ternary search tree, each node contains only 3 pointers:

  • The left pointer points to the node whose value is lower than the current node's value.
  • The equal pointer leads to a node whose value is the same as the current node's value.
  • The right pointer leads to the node whose value exceeds the current node's value.

Aside from the three-pointers mentioned above, each node contains a field for indicating data (character in the case of a dictionary) and a field for indicating the end of a string.

So, it's similar to BST, which saves data in a specific order. On the other hand, data in a ternary search tree is dispersed among the nodes. For example, you required the four nodes to store the term "Bird".

One advantage of ternary search trees over tries is that ternary search trees take up less space (only three-pointers per node compared to 26 in standard tries). In addition, ternary search trees may be utilized in any situation where a hash table is used to hold strings.

Tries are appropriate when there is the correct distribution of words throughout the alphabets, allowing for the most effective use of space. Ternary search trees are preferable when the strings to be stored all have the same prefix; ternary search trees are the most economical in terms of space.

Operations on Ternary Search Trees:

1. Insertion:

The insertion operation involves traversing the tree, creating new nodes as needed to accommodate the characters of the key.

The process ensures that each node is inserted in a sorted order, maintaining the inherent structure of the TST.

2. Search:

The search operation involves traversing the tree based on the characters of the key.

At each step, the algorithm compares the current character with the character stored in the current node.

The search is successful when the entire key is traversed, and the corresponding data is found.

3. Deletion:

Deletion in TSTs is a complex operation involving recursive traversal and restructuring to maintain the tree's balance.

Care must be taken to handle various cases, such as nodes with one, two, or three children.

Properties:

  • Trie-Like Structure: TSTs share similarities with tries, where each node represents a character of a string. However, unlike tries, TSTs do not have a node for every character. Instead, a TST node contains only a single character and has pointers to nodes with the next character in the string.
  • Binary Search Tree Characteristics: Each node in a TST follows the binary search tree property, where the nodes in the left subtree have values less than the current node, and nodes in the right subtree have values greater than the current node.
  • Efficient Search: TSTs efficiently support prefix search, substring search, and exact match search. The tree structure allows for rapid pruning of irrelevant subtrees during search operations.

Implementation:

Explanation:

  • The program defines a class called TSTNode, representing a node in the Ternary Search Tree.
  • Each node contains a character (data), a boolean flag (isEnd) indicating whether the node represents the end of a word, and three pointers (left, middle, and right) pointing to the left, middle, and right child nodes, respectively.
  • The constructor initializes the node with the given character.
  • The program also defines a class named TernarySearchTree, which encapsulates the TST It includes a private member root representing the root of the TST.
  • The Ternary Search Tree supports insertion through a recursive helper function insert. This function takes a node, a key (string to be inserted), and the current index in the key.
  • It recursively traverses the tree based on the characters of the key, creating new nodes as needed, until it reaches the end of the key. The insert function is used internally and is not exposed in the public interface.
  • The TST provides a search operation through a recursive helper function search. This function checks if the characters of the key match the tree nodes and continues the search until it reaches the end of the key, verifying if the last node is marked as the end of a word.
  • The public search method, search(const std::string& key), serves as an interface for the user and initiates the search process by calling the private search function with the root and the initial index.
  • The public insertion method, insert(const std::string& key), serves as an interface and starts the insertion process by calling the private insert function with the root and the initial index.
  • In the main function, an instance of TernarySearchTree is created, and three words ("apple," "banana," and "orange") are inserted into the TST using the insert method.
  • Then, the program searches for the words "apple" and "grape" using the search method and prints whether each word is found or not.

Program Output:

Ternary Search Tree

Java Code

Now let's write a Java program to implement a ternary search tree.

Output

The above Java code gives the following output.

Ternary Search Tree Test
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
red
Ternary Search Tree : [red]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
blur e
Ternary Search Tree : [blue, red]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
green
Ternary Search Tree : [blue, green, red]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
yellow
Ternary Search Tree : [blue, green, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
pink
Ternary Search Tree : [blue, green, pink, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
black
Ternary Search Tree : [black, blue, green, pink, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
grey
Ternary Search Tree : [black, blue, green, grey, pink, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
mustard
Ternary Search Tree : [black, blue, green, grey, mustard, pink, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
White
Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
1
Enter the word to insert
purple
Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
2
Enter the word to search
black
Search result: true
Ternary Search Tree : [White, black, blue, green, grey, mustard, pink, purple, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
3
Enter the word to delete
blue    mustard
Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
4
Empty Status: false
Ternary Search Tree : [White, black, blue, green, grey, pink, purple, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
3
Enter the word to delete
black
Ternary Search Tree : [White, blue, green, grey, pink, purple, red, yellow]
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Tree is empty.
5. To make the Ternary Search Tree empty.
5
Ternary Search Tree cleared
Ternary Search Tree : []
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To delete a word from the Ternary Search Tree
4. To check if the Ternary Search Tree is empty.
5. To make the Ternary Search Tree empty.
4
Empty Status: true
Ternary Search Tree : []
Do you want to continue (Type y or n) 
n

In the above Java code, we have implemented the ternary search tree data structure and various functions like adding new data in the ternary search tree, deleting the existing data from the ternary search tree, to check whether the ternary search tree is empty or not, there is also one operation to empty the tree that will delete all the content within the ternary search tree and the traversal of the ternary search tree.

For all these operations of the ternary search tree, there are different functions written, and then according to the need of the operation on the ternary search tree data structure, those functions are called.

C++ Code

Let's write the C++ code for the ternary search tree.

Output

The above C++ code gives the following output.

Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
orange 
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
banana
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
grapes 
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
apple
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
watermelon
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
guava
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
1
Enter the word to insert
Kiwi
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
3
Contents of the Ternary Search Tree are::
Kiwi
apple
Banana
Watermelon
guava
grapes
orange
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
2
Enter the word to search
Kiwi
Kiwi found in Ternary Search Tree
Do you want to continue (Type y or n) 
y
Select one of the operations for Ternary Search Tree::
1. To insert a new word in the Ternary Search Tree.
2. To search an already existing word in the Ternary Search Tree.
3. To Traverse the Ternary Search Tree.
2
Enter the word to search
mango
mango not found in Ternary Search Tree
Do you want to continue (Type y or n) 
n

In the above C++ code, we have implemented the ternary search tree data structure and various functions like adding new data, deleting the existing data, and the traversal of the ternary search tree. For all these operations of the ternary search tree, there are different functions written, and then according to the need of the operation on the ternary search tree data structure, those functions are called.

Real-World Applications:

1. Spell Checking:

TSTs are widely used in spell-checking algorithms to efficiently search for and suggest correct spellings.

2. IP Routing:

Ternary Search Trees are employed in IP routing tables to quickly locate the longest matching prefix for a given IP address.

3. Auto-Completion:

TSTs power auto-completion features in search engines and text editors, providing quick and accurate suggestions.

4. Symbol Tables:

TSTs serve as a foundation for efficient symbol tables, aiding in the quick retrieval and modification of symbols.






Latest Courses