Background image of landing

Unrivalled
Education
Solutions for your
Family

What is a ternary search tree, and how is it used?

A ternary search tree (TST) is an advanced type of trie data structure specifically designed for the efficient storage and retrieval of strings.

A TST is distinguished from a standard trie (also known as a prefix tree) by its unique structure, which allows each node to have up to three children: a left child, a middle child, and a right child. The left child contains all strings that are lexicographically less than the character in the current node, the right child holds all strings that are greater, and the middle child represents all strings that begin with the character of the current node.

Each node in a TST stores a single character, and a string is represented by traversing a path from the root to a designated node known as the “end of string” node. This characteristic enables a TST to efficiently handle multiple strings that share the same prefix, making it particularly valuable for applications like autocomplete, where the goal is to retrieve all strings starting with a specific prefix.

One of the primary advantages of a TST over traditional tries is its reduced memory usage. In a standard trie, a child node exists for every possible character in the alphabet, which can lead to significant memory overhead. In contrast, a TST only allocates child nodes for characters that are actually present in the stored strings, resulting in improved space efficiency, especially when dealing with large character sets.

Regarding time complexity, searching for a string in a TST typically takes O(logn)O(\log n) in the average case and O(n)O(n) in the worst case, where nn represents the length of the string. This efficiency makes TSTs faster than binary search trees for most string-related operations.

In summary, a ternary search tree is a robust data structure that excels in the efficient storage and retrieval of strings. Its design is particularly advantageous for tasks that involve prefix matching, such as autocomplete functionalities.

Answered by: Dr. Ava Johnson
A-Level Computer Science Tutor
Medal Icon

100%

Globe Icon

Global

Crest Icon

97%

Professional Tutors

International Tuition

Independent School Entrance Success

All of our elite tutors are full-time professionals, with at least five years of tuition experience and over 5000 accrued teaching hours in their subject.

Based in Cambridge, with operations spanning the globe, we can provide our services to support your family anywhere.

Our families consistently gain offers from at least one of their target schools, including Eton, Harrow, Wellington and Wycombe Abbey.

Medal Icon

100%

Professional Tutors

All of our elite tutors are full-time professionals, with at least five years of tuition experience and over 5000 accrued teaching hours in their subject.

Globe Icon

Global

International Tuition

Based in Cambridge, with operations spanning the globe, we can provide our services to support your family anywhere.

Crest Icon

97%

Independent School Entrance Success

Our families consistently gain offers from at least one of their target schools, including Eton, Harrow, Wellington and Wycombe Abbey.

Book a free
30-minute consultation
session

At the Beyond Tutors we recognise that no two students are the same. 

That’s why we’ve transcended the traditional online tutoring model of cookie-cutter solutions to intricate educational problems. Instead, we devise a bespoke tutoring plan for each individual student, to support you on your path to academic success.

To help us understand your unique educational needs, we provide a free 30-minute consultation with one of our founding partners, so we can devise the tutoring plan that’s right for you.

To ensure we can best prepare for this consultation, we ask you to fill out the short form below.

Hire a Tutor

All the form fields are optional, but we ask you to provide as much information as possible so that we are in a better position to quickly meet your tutoring requirements.

Still have questions?
Let's get in touch