Javatpoint Logo
Javatpoint Logo

Ternary Search Tree

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. 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.

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.

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.


Next TopicStock Span Problem





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA