A ternary search tree is a special trie data structure where the child nodes of a standard trie are ordered as a binary search tree.
Representation of ternary search trees:
Unlike trie(standard) data structure where each node contains 26 pointers for its children, each node in a ternary search tree contains only 3 pointers:
1. The left pointer points to the node whose value is less than the value in the current node.
2. The equal pointer points to the node whose value is equal to the value in the current node.
3. The right pointer points to the node whose value is greater than the value in the current node.
Apart from above three pointers, each node has a field to indicate data(character in case of dictionary) and another field to mark end of a string.
So, more or less it is similar to BST which stores data based on some order. However, data in a ternary search tree is distributed over the nodes. e.g. It needs 4 nodes to store the word "Geek".
Below figure shows how exactly the words in a ternary search tree are stored?

One of the advantage of using ternary search trees over tries is that ternary search trees are a more space efficient (involve only three pointers per node as compared to 26 in standard tries). Further, ternary search trees can be used any time a hashtable would be used to store strings.
Tries are suitable when there is a proper distribution of words over the alphabets so that spaces are utilized most efficiently. Otherwise ternary search trees are better. Ternary search trees are efficient to use(in terms of space) when the strings to be stored share a common prefix.
Applications of ternary search trees:
1. Ternary search trees are efficient for queries like "Given a word, find the next word in dictionary(near-neighbor lookups)" or "Find all telephone numbers starting with 9342 or "typing few starting characters in a web browser displays all website names with this prefix"(Auto complete feature)".
2. Used in spell checks: Ternary search trees can be used as a dictionary to store all the words. Once the word is typed in an editor, the word can be parallelly searched in the ternary search tree to check for correct spelling.
Implementation:
Following is C implementation of ternary search tree. The operations implemented are, search, insert, and traversal.
C++
// C++ program to demonstrate Ternary Search Tree (TST)
// insert, traverse and search operations
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
// A node of ternary search tree
struct Node {
char data;
// True if this character is last character of one of
// the words
unsigned isEndOfString = 1;
Node *left, *eq, *right;
};
// A utility function to create a new ternary search tree
// node
Node* newNode(char data)
{
Node* temp = new Node();
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
// Function to insert a new word in a Ternary Search Tree
void insert(Node** root, char* word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller than root's
// character, then insert this word in left subtree of
// root
if ((*word) < (*root)->data)
insert(&((*root)->left), word);
// If current character of word is greater than root's
// character, then insert this word in right subtree of
// root
else if ((*word) > (*root)->data)
insert(&((*root)->right), word);
// If current character of word is same as root's
// character,
else {
if (*(word + 1))
insert(&((*root)->eq), word + 1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(Node* root, char* buffer, int depth)
{
if (root) {
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth);
// Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString) {
buffer[depth + 1] = '\0';
cout << buffer << endl;
}
// Traverse the subtree using equal pointer (middle
// subtree)
traverseTSTUtil(root->eq, buffer, depth + 1);
// Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
}
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
// Function to search a given word in TST
int searchTST(Node* root, char* word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else {
if (*(word + 1) == '\0')
return root->isEndOfString;
return searchTST(root->eq, word + 1);
}
}
// Driver program to test above functions
int main()
{
Node* root = NULL;
char cat[] = "cat";
char cats[] = "cats";
char up[] = "up";
char bug[] = "bug";
char bu[] = "bu";
insert(&root, cat);
insert(&root, cats);
insert(&root, up);
insert(&root, bug);
cout << "Following is traversal of ternary search "
"tree\n";
traverseTST(root);
cout << "\nFollowing are search results for cats, bu "
"and cat respectively\n";
searchTST(root, cats) ? cout << "Found\n"
: cout << "Not Found\n";
searchTST(root, bu) ? cout << "Found\n"
: cout << "Not Found\n";
searchTST(root, cat) ? cout << "Found\n"
: cout << "Not Found\n";
return 0;
}
// This code is contributed by tapeshdua420.
C
// C program to demonstrate Ternary Search Tree (TST)
// insert, traverse and search operations
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
// A node of ternary search tree
struct Node {
char data;
// True if this character is last character of one of
// the words
unsigned isEndOfString : 1;
struct Node *left, *eq, *right;
};
// A utility function to create a new ternary search tree
// node
struct Node* newNode(char data)
{
struct Node* temp
= (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->isEndOfString = 0;
temp->left = temp->eq = temp->right = NULL;
return temp;
}
// Function to insert a new word in a Ternary Search Tree
void insert(struct Node** root, char* word)
{
// Base Case: Tree is empty
if (!(*root))
*root = newNode(*word);
// If current character of word is smaller than root's
// character, then insert this word in left subtree of
// root
if ((*word) < (*root)->data)
insert(&((*root)->left), word);
// If current character of word is greater than root's
// character, then insert this word in right subtree of
// root
else if ((*word) > (*root)->data)
insert(&((*root)->right), word);
// If current character of word is same as root's
// character,
else {
if (*(word + 1))
insert(&((*root)->eq), word + 1);
// the last character of the word
else
(*root)->isEndOfString = 1;
}
}
// A recursive function to traverse Ternary Search Tree
void traverseTSTUtil(struct Node* root, char* buffer,
int depth)
{
if (root) {
// First traverse the left subtree
traverseTSTUtil(root->left, buffer, depth);
// Store the character of this node
buffer[depth] = root->data;
if (root->isEndOfString) {
buffer[depth + 1] = '\0';
printf("%s\n", buffer);
}
// Traverse the subtree using equal pointer (middle
// subtree)
traverseTSTUtil(root->eq, buffer, depth + 1);
// Finally Traverse the right subtree
traverseTSTUtil(root->right, buffer, depth);
}
}
// The main function to traverse a Ternary Search Tree.
// It mainly uses traverseTSTUtil()
void traverseTST(struct Node* root)
{
char buffer[MAX];
traverseTSTUtil(root, buffer, 0);
}
// Function to search a given word in TST
int searchTST(struct Node* root, char* word)
{
if (!root)
return 0;
if (*word < (root)->data)
return searchTST(root->left, word);
else if (*word > (root)->data)
return searchTST(root->right, word);
else {
if (*(word + 1) == '\0')
return root->isEndOfString;
return searchTST(root->eq, word + 1);
}
}
// Driver program to test above functions
int main()
{
struct Node* root = NULL;
insert(&root, "cat");
insert(&root, "cats");
insert(&root, "up");
insert(&root, "bug");
printf(
"Following is traversal of ternary search tree\n");
traverseTST(root);
printf("\nFollowing are search results for cats, bu "
"and cat respectively\n");
searchTST(root, "cats") ? printf("Found\n")
: printf("Not Found\n");
searchTST(root, "bu") ? printf("Found\n")
: printf("Not Found\n");
searchTST(root, "cat") ? printf("Found\n")
: printf("Not Found\n");
return 0;
}
Java
// java code addiiton
import java.io.*;
import java.util.*;
public class Main {
static class Node {
char data;
boolean isEndOfString;
Node left, eq, right;
public Node(char data)
{
this.data = data;
this.isEndOfString = false;
this.left = null;
this.eq = null;
this.right = null;
}
}
public static Node insert(Node root, String word)
{
if (root == null) {
root = new Node(word.charAt(0));
}
if (word.charAt(0) < root.data) {
root.left = insert(root.left, word);
}
else if (word.charAt(0) > root.data) {
root.right = insert(root.right, word);
}
else {
if (word.length() > 1) {
root.eq
= insert(root.eq, word.substring(1));
}
else {
root.isEndOfString = true;
}
}
return root;
}
public static void
traverseTSTUtil(Node root, char[] buffer, int depth)
{
if (root != null) {
traverseTSTUtil(root.left, buffer, depth);
buffer[depth] = root.data;
if (root.isEndOfString) {
System.out.println(
new String(buffer, 0, depth + 1));
}
traverseTSTUtil(root.eq, buffer, depth + 1);
traverseTSTUtil(root.right, buffer, depth);
}
}
public static void traverseTST(Node root)
{
char[] buffer = new char[50];
traverseTSTUtil(root, buffer, 0);
}
public static boolean searchTST(Node root, String word)
{
if (root == null) {
return false;
}
if (word.charAt(0) < root.data) {
return searchTST(root.left, word);
}
else if (word.charAt(0) > root.data) {
return searchTST(root.right, word);
}
else {
if (word.length() > 1) {
return searchTST(root.eq,
word.substring(1));
}
else {
return root.isEndOfString;
}
}
}
public static void main(String[] args)
{
Node root = new Node(' ');
root = insert(root, "cat");
root = insert(root, "cats");
root = insert(root, "up");
root = insert(root, "bug");
System.out.println(
"Following is traversal of ternary search tree:");
traverseTST(root);
System.out.println(
"\nFollowing are search results for 'cats', 'bu', and 'up':");
System.out.println(searchTST(root, "cats")
? "Found"
: "Not Found");
System.out.println(
searchTST(root, "bu") ? "Found" : "Not Found");
System.out.println(
searchTST(root, "up") ? "Found" : "Not Found");
}
}
// The code is contributed by Arushi Goel.
Python3
class Node:
def __init__(self, data):
self.data = data
self.isEndOfString = False
self.left = None
self.eq = None
self.right = None
def insert(root, word):
if not root:
root = Node(word[0])
if word[0] < root.data:
root.left = insert(root.left, word)
elif word[0] > root.data:
root.right = insert(root.right, word)
else:
if len(word) > 1:
root.eq = insert(root.eq, word[1:])
else:
root.isEndOfString = True
return root
def traverseTSTUtil(root, buffer, depth):
if root:
traverseTSTUtil(root.left, buffer, depth)
buffer[depth] = root.data
if root.isEndOfString:
print("".join(buffer[:depth+1]))
traverseTSTUtil(root.eq, buffer, depth+1)
traverseTSTUtil(root.right, buffer, depth)
def traverseTST(root):
buffer = [''] * 50
traverseTSTUtil(root, buffer, 0)
def searchTST(root, word):
if not root:
return False
if word[0] < root.data:
return searchTST(root.left, word)
elif word[0] > root.data:
return searchTST(root.right, word)
else:
if len(word) > 1:
return searchTST(root.eq, word[1:])
else:
return root.isEndOfString
root = Node('')
insert(root, "cat")
insert(root, "cats")
insert(root, "up")
insert(root, "bug")
print("Following is traversal of ternary search tree:")
traverseTST(root)
print("\nFollowing are search results for 'cats', 'bu', and 'up':")
print("Found" if searchTST(root, "cats") else "Not Found")
print("Found" if searchTST(root, "bu") else "Not Found")
print("Found" if searchTST(root, "up") else "Not Found")
# This code is contributed by Shivam Tiwari
JavaScript
class Node {
constructor(data) {
this.data = data;
this.isEndOfString = false;
this.left = null;
this.eq = null;
this.right = null;
}
}
function insert(root, word) {
if (!root) {
root = new Node(word[0]);
}
if (word[0] < root.data) {
root.left = insert(root.left, word);
} else if (word[0] > root.data) {
root.right = insert(root.right, word);
} else {
if (word.length > 1) {
root.eq = insert(root.eq, word.slice(1));
} else {
root.isEndOfString = true;
}
}
return root;
}
function traverseTSTUtil(root, buffer, depth) {
if (root) {
traverseTSTUtil(root.left, buffer, depth);
buffer[depth] = root.data;
if (root.isEndOfString) {
console.log(buffer.slice(0, depth+1).join(""));
}
traverseTSTUtil(root.eq, buffer, depth+1);
traverseTSTUtil(root.right, buffer, depth);
}
}
function traverseTST(root) {
let buffer = new Array(50).fill("");
traverseTSTUtil(root, buffer, 0);
}
function searchTST(root, word) {
if (!root) {
return false;
}
if (word[0] < root.data) {
return searchTST(root.left, word);
} else if (word[0] > root.data) {
return searchTST(root.right, word);
} else {
if (word.length > 1) {
return searchTST(root.eq, word.slice(1));
} else {
return root.isEndOfString;
}
}
}
let root = new Node("");
insert(root, "cat");
insert(root, "cats");
insert(root, "up");
insert(root, "bug");
console.log("Following is traversal of ternary search tree:");
traverseTST(root);
console.log("\nFollowing are search results for 'cats', 'bu', and 'up':");
console.log(searchTST(root, "cats") ? "Found" : "Not Found");
console.log(searchTST(root, "bu") ? "Found" : "Not Found");
console.log(searchTST(root, "up") ? "Found" : "Not Found");
// This code is contributed by Shivam Tiwari
C#
using System;
public class TernarySearchTree {
public class Node {
public char data;
public bool isEndOfString;
public Node left, eq, right;
public Node(char data)
{
this.data = data;
this.isEndOfString = false;
this.left = this.eq = this.right = null;
}
}
private Node root;
public TernarySearchTree() { this.root = null; }
public void Insert(string word)
{
if (string.IsNullOrEmpty(word))
return;
this.root = InsertHelper(this.root, word, 0);
}
private Node InsertHelper(Node node, string word,
int index)
{
if (node == null) {
node = new Node(word[index]);
}
if (word[index] < node.data) {
node.left
= InsertHelper(node.left, word, index);
}
else if (word[index] > node.data) {
node.right
= InsertHelper(node.right, word, index);
}
else {
if (index < word.Length - 1) {
node.eq = InsertHelper(node.eq, word,
index + 1);
}
else {
node.isEndOfString = true;
}
}
return node;
}
public bool Search(string word)
{
if (string.IsNullOrEmpty(word))
return false;
return SearchHelper(this.root, word, 0);
}
private bool SearchHelper(Node node, string word,
int index)
{
if (node == null)
return false;
if (word[index] < node.data) {
return SearchHelper(node.left, word, index);
}
else if (word[index] > node.data) {
return SearchHelper(node.right, word, index);
}
else {
if (index == word.Length - 1) {
return node.isEndOfString;
}
else {
return SearchHelper(node.eq, word,
index + 1);
}
}
}
public void Traverse()
{
TraverseHelper(this.root, "");
}
private void TraverseHelper(Node node, string buffer)
{
if (node == null)
return;
TraverseHelper(node.left, buffer);
buffer += node.data;
if (node.isEndOfString) {
Console.WriteLine(buffer);
}
TraverseHelper(
node.eq, buffer.Substring(0, buffer.Length - 1)
+ node.data);
TraverseHelper(
node.right,
buffer.Substring(0, buffer.Length - 1));
}
}
public class Program {
static void Main(string[] args)
{
TernarySearchTree trie = new TernarySearchTree();
trie.Insert("cat");
trie.Insert("cats");
trie.Insert("up");
trie.Insert("bug");
Console.WriteLine(
"Following is traversal of ternary search tree");
trie.Traverse();
Console.WriteLine(
"\nFollowing are search results for cats, bu and cat respectively");
Console.WriteLine(
trie.Search("cats") ? "Found" : "Not Found");
Console.WriteLine(trie.Search("bu") ? "Found"
: "Not Found");
Console.WriteLine(trie.Search("cat") ? "Found"
: "Not Found");
Console.ReadKey();
}
}
// This code is contributed By Shivam Tiwari
OutputFollowing is traversal of ternary search tree
bug
cat
cats
up
Following are search results for cats, bu and cat respectively
Found
Not Found
Found
Time Complexity: The time complexity of the ternary search tree operations is similar to that of binary search tree. i.e. the insertion, deletion, and search operations take time proportional to the height of the ternary search tree.
Auxiliary Space: O(n), where n is the number of keys in TST.
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem