What is Ternary Tree

Last Updated : 26 Sep, 2023

Data Structure and Algorithms Course
Recent articles on Tree

What is a Ternary Tree?

A Ternary Tree is a tree data structure in which each node has at most three child nodes, usually distinguished as “left”, “mid” and “right”.

For example: Consider the tree below. Since each node of this tree has only 3 children, it can be said that this tree is a Ternary Tree.

Ternary Tree

Ternary Tree

Some interesting problems on Ternary Tree:

  1. Create a Doubly Linked List from a Ternary Tree
  2. Ternary Search Tree
  3. Ternary Search Tree (Deletion)
  4. Longest word in ternary search tree
  5. How to implement text Auto-complete feature using Ternary Search Tree

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above


Share your thoughts in the comments

Similar Reads

What is Generic Tree or N-ary Tree
Data Structure and Algorithms Course Recent articles on N-ary Tree What is a N-ary Tree? Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every no
4 min read
Binary Search Tree vs Ternary Search Tree
For effective searching, insertion, and deletion of items, two types of search trees are used: the binary search tree (BST) and the ternary search tree (TST). Although the two trees share a similar structure, they differ in some significant ways. FeatureBinary Search Tree (BST)Ternary Search Tree (TST)NodeHere, each node has at most two children. H
3 min read
Convert ternary expression to Binary Tree using Stack
Given a string str that contains a ternary expression which may be nested. The task is to convert the given ternary expression to a binary tree and return the root.Examples: Input: str = "a?b:c" Output: a b c a / \ b c The preorder traversal of the above tree is a b c. Input: str = "a?b?c:d:e" Output: a b c d e a / \ b e / \ c d Approach: This is a
10 min read
How to implement text Auto-complete feature using Ternary Search Tree
Given a set of strings S and a string patt the task is to autocomplete the string patt to strings from S that have patt as a prefix, using a Ternary Search Tree. If no string matches the given prefix, print "None".Examples: Input: S = {"wallstreet", "geeksforgeeks", "wallmart", "walmart", "waldomort", "word"], patt = "wall" Output: wallstreet wallm
16 min read
Ternary Search Tree meaning & definition in DSA
Ternary Search Tree is defined as a special trie data structure where the child nodes of a standard trie are ordered as a binary search tree where each node can have at most three children and the left, middle and right subtrees of a node contain values that are less than, equal or greater than the node. Characteristics of Ternary Search Tree:TST i
3 min read
Ternary Search Tree
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 poin
13 min read
Create a Doubly Linked List from a Ternary Tree
Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like a binary tree but instead of having two nodes, it has three nodes i.e. left, middle, and right. The doubly linked list should hold the following properties – The left pointer of the ternary tree should act as prev pointer of the doubly linked list.The middle po
12 min read
Convert Ternary Expression to a Binary Tree
Given a string that contains ternary expressions. The expressions may be nested, task is convert the given ternary expression to a binary Tree. Examples: Input : string expression = a?b:c Output : a / \ b cInput : expression = a?b?c:d:eOutput : a / \ b e / \ c dAsked In : Facebook Interview Idea is that we traverse a string make first character as
11 min read
Ternary Search Tree (Deletion)
In the SET 1 post on TST we have described how to insert and search a node in TST. In this article, we will discuss algorithm on how to delete a node from TST. During delete operation we delete the key in bottom-up manner using recursion. The following are possible cases when deleting a key from trie. Key may not be there in TST. Solution: Delete o
14 min read
Longest word in ternary search tree
Given a set of words represented in a ternary search tree, find the length of largest word among them. Examples: Input : {"Prakriti", "Raghav", "Rashi", "Sunidhi"} Output : Length of largest word in ternary search tree is: 8 Input : {"Boats", "Boat", "But", "Best"} Output : Length of largest word in ternary search tree is: 5 Prerequisite : Ternary
11 min read