Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
What is Ternary Tree?
Next article icon

What is Ternary Tree?

Last Updated : 28 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A Ternary Tree is a special type of tree data structure. Unlike a regular binary tree where each node can have up to two child nodes. The article explains the basic structure and properties of ternary trees, such as the number of possible children per node, tree height, and node depth. It also discusses why ternary trees can be useful, highlighting applications in areas like string searching and database indexing. It also introduces some common problems and algorithms related to ternary trees.

What is a Ternary Tree?

A Ternary Tree is a type of tree data structure where each node can have up to three child nodes. This is different from a binary tree, where each node can have at most two child nodes. In a ternary tree, the first child node is called the "left" child, the second child node is called the "middle" child, and the third child node is called the "right" child.

Basic Structure of a Ternary Tree

In a ternary tree:

  • Each node has three possible children: a left child, a middle child, and a right child.
  • The nodes are connected by edges that represent the parent-child relationships.

Here's a simple visualization of a ternary tree:

Ternary Tree
Ternary Tree

Properties of Ternary Trees

  1. Children Count: Each node in a ternary tree can have zero, one, two, or three children.
  2. Height: The height of a ternary tree is the length of the longest path from the root to a leaf. A leaf is a node with no children.
  3. Depth: The depth of a node is the number of edges from the root to the node.

Why Use a Ternary Tree?

Ternary trees are useful in specific scenarios where more than two children per node are beneficial. Here are some common applications:

  1. Ternary Search Trees: These are specialized ternary trees used for storing and searching strings. They combine the advantages of binary search trees and digital search tries, making them efficient for certain types of string search operations.
  2. Multi-way Trees: In databases and file systems, multi-way trees (like B-trees) generalize ternary trees to have more than three children, but the concept starts with understanding ternary trees.

Basic Operations on a Ternary Tree

  • Insertion: Adding a new node to the tree.
  • Deletion: Removing a node from the tree.
  • Traversal: Visiting all the nodes in the tree in a specific order. Common traversal methods include:
    • Pre-order Traversal: Visit the root, then recursively visit the left, middle, and right subtrees.
    • In-order Traversal: Recursively visit the left subtree, visit the root, then the middle subtree, and finally the right subtree.
    • Post-order Traversal: Recursively visit the left, middle, and right subtrees, then visit the root.

Applications of Ternary Trees

Here are a few key applications of ternary trees:

  • Ternary Search Trees: Ternary search trees are specialized ternary trees used for efficient storage and searching of strings. They combine the advantages of binary search trees and digital search tries, making them effective for certain string-based operations.
  • Multi-way Trees: In databases and file systems, multi-way trees like B-trees generalize the concept of ternary trees to have more than three children per node. Understanding the structure and properties of ternary trees is a foundational step for these more complex multi-way tree data structures.
  • Text Auto-completion: Ternary search trees can be leveraged to implement text auto-completion features, where suggestions are provided as the user types. The tree structure allows for efficient prefix-based lookups and string traversal.
  • Prefix-based Indexing: Similar to auto-completion, ternary search trees can be used to build prefix-based indexes for quickly finding words or phrases that match a given prefix, which is useful in search engines and natural language processing applications.

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

Next Article
What is Ternary Tree?

H

harendrakumar123
Improve
Article Tags :
  • Tree
  • DSA
Practice Tags :
  • Tree

Similar Reads

    What is Binary Tree?
    A binary tree is a type of tree data structure in which each node can have at most two child nodes, known as the left child and the right child. Each node of the tree consists of - data and pointers to the left and the right child. Example of Binary TreeProperties of a Binary Tree: The following are
    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
    13 min read
    What is Binary Search Tree
    A binary search tree (BST) is a binary tree in which the left subtree of a node contains only nodes with less value and the right subtree of a node contains only nodes with values greater than it. Binary Search TreeCharacteristics of Binary Search Tree: The properties of a binary search tree are as
    3 min read
    What is Full Binary Tree?
    A full binary tree is a type of binary tree in which every node has either zero or two children. Full Binary Tree exampleProperties of Full Binary Tree: Let i be the number of internal nodes, n be the total number of nodes, l be the number of leaf nodes and h be the total number of levels The number
    2 min read
    Why use Ternary search tree (TST)?
    Ternary search tree (TST) is a very popular data structure used in the problem solving becaus of following reasons: First, they offer efficient search operations, particularly for strings, with lookup times typically on the order of O(log n), where n is the length of the search key. This makes them
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences