
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Ternary Tree
A ternary tree, is a tree data structure in which each node has at most three child nodes, usually represented as “left”, “mid” and “right”. In this tree, nodes with children are parent nodes, and child nodes may contain references to their parents. This is a C++ Program to Implement Ternary Tree and traversal of the tree.
Algorithm
Begin Declare function insert(struct nod** root, char *w) if (!(*root)) then *root = newnod(*w); if ((*w) < (*root)->d) then insert(&( (*root)->l ), w); else if ((*w) > (*root)->d) then insert(&( (*root)->r ), w); else if (*(w+1)) insert(&( (*root)->eq ), w+1); else (*root)->EndOfString = 1; End.
For Traversal of tree:
Begin Declare function traverseTTtil(struct nod* root, char* buffer, int depth) if (root) then traverseTTtil(root->l, buffer, depth) buffer[depth] = root->d if (root->EndOfString) then buffer[depth+1] = '\0' print the value of buffer. traverseTTtil(root->eq, buffer, depth + 1); traverseTTtil(root->r, buffer, depth); End.
Example
#include<stdlib.h> #include<iostream> using namespace std; struct nod { char d; unsigned End. fString: 1; struct nod *l, *eq, *r; }*t = NULL; struct nod* newnod(char d) { t = new nod; t->d = d; t->End. fString = 0; t->l = t->eq = t->r = NULL; return t; } void insert(struct nod** root, char *w) { if (!(*root)) *root = newnod(*w); if ((*w) < (*root)->d) insert(&( (*root)->l ), w); else if ((*w) > (*root)->d) insert(&( (*root)->r ), w); else { if (*(w+1)) insert(&( (*root)->eq ), w+1); else (*root)->End. fString = 1; } } void traverseTTtil(struct nod* root, char* buffer, int depth) { if (root) { traverseTTtil(root->l, buffer, depth); buffer[depth] = root->d; if (root->End. String) { buffer[depth+1] = '\0'; cout<<buffer<<endl; } traverseTTtil(root->eq, buffer, depth + 1); traverseTTtil(root->r, buffer, depth); } } void traverseTT(struct nod* root) { char buffer[50]; traverseTTtil(root, buffer, 0); } int main() { struct nod *root = NULL; insert(&root, "mat"); insert(&root, "bat"); insert(&root, "hat"); insert(&root, "rat"); cout<<"Following is traversal of ternary tree\n"; traverseTT(root); }
Output
Following is traversal of ternary tree bat hat mat rat
- Related Articles
- C++ Program to Implement AVL Tree
- C++ Program to Implement B+ Tree
- C++ Program to Implement Cartesian Tree
- C++ Program to Implement Fusion Tree
- C++ Program to Implement Interval Tree
- C++ Program to Implement Splay Tree
- C++ program to Implement Threaded Binary Tree
- C++ Program to Implement B Tree\n
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Randomized Binary Search Tree
- C++ Program to Implement self Balancing Binary Search Tree
- Convert Ternary Expression to a Binary Tree in C++
- Python Program to Implement Binomial Tree
- C++ Program to Implement a Binary Search Tree using Linked Lists
- C++ Program to Implement Double Order Traversal of a Binary Tree

Advertisements