C++ Program to Implement Ternary Tree

This C++ program implements the ternary tree, is a tree data structure in which each node has at most three child nodes, usually distinguished as “left”, “mid” and “right”. Nodes with children are parent nodes, and child nodes may contain references to their parents.

Here is the source code of the C++ program to display the traveral and search of a ternary tree on giving character sequences as input. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to Implement Ternary Tree
  3.  */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<iostream>
  7. #include<conio.h>
  8. using namespace std;
  9. struct Node
  10. {
  11.     char data;
  12.     unsigned isEndOfString: 1;
  13.     struct Node *left, *eq, *right;
  14. }*temp = NULL;
  15. struct Node* newNode(char data)
  16. {
  17.     temp = new Node;
  18.     temp->data = data;
  19.     temp->isEndOfString = 0;
  20.     temp->left = temp->eq = temp->right = NULL;
  21.     return temp;
  22. }
  23. void insert(struct Node** root, char *word)
  24. {
  25.     if (!(*root))
  26.         *root = newNode(*word);
  27.  
  28.     if ((*word) < (*root)->data)
  29.         insert(&( (*root)->left ), word);
  30.  
  31.     else if ((*word) > (*root)->data)
  32.         insert(&( (*root)->right ), word);
  33.  
  34.     else
  35.     {
  36.         if (*(word+1))
  37.             insert(&( (*root)->eq ), word+1);
  38.  
  39.         else
  40.             (*root)->isEndOfString = 1;
  41.     }
  42. }
  43.  
  44. void traverseTSTUtil(struct Node* root, char* buffer, int depth)
  45. {
  46.     if (root)
  47.     {
  48.         traverseTSTUtil(root->left, buffer, depth);
  49.  
  50.         buffer[depth] = root->data;
  51.         if (root->isEndOfString)
  52.         {
  53.             buffer[depth+1] = '\0';
  54.             cout<<buffer<<endl;
  55.         }
  56.  
  57.         traverseTSTUtil(root->eq, buffer, depth + 1);
  58.  
  59.         traverseTSTUtil(root->right, buffer, depth);
  60.     }
  61. }
  62. void traverseTST(struct Node* root)
  63. {
  64.     char buffer[50];
  65.     traverseTSTUtil(root, buffer, 0);
  66. }
  67.  
  68. int searchTST(struct Node *root, char *word)
  69. {
  70.     if (!root)
  71.         return 0;
  72.  
  73.     if (*word < (root)->data)
  74.         return searchTST(root->left, word);
  75.  
  76.     else if (*word > (root)->data)
  77.         return searchTST(root->right, word);
  78.  
  79.     else
  80.     {
  81.         if (*(word+1) == '\0')
  82.             return root->isEndOfString;
  83.  
  84.         return searchTST(root->eq, word+1);
  85.     }
  86. }
  87.  
  88. int main()
  89. {
  90.     struct Node *root = NULL;
  91.  
  92.     insert(&root, "cat");
  93.     insert(&root, "cats");
  94.     insert(&root, "up");
  95.     insert(&root, "bug");
  96.  
  97.     cout<<"Following is traversal of ternary search tree\n";
  98.     traverseTST(root);
  99.  
  100.     cout<<"\nFollowing are search results for cats, bu and cat respectively\n";
  101.     searchTST(root, "cats")? cout<<"Found\n": cout<<"Not Found\n";
  102.     searchTST(root, "bu")?   cout<<"Found\n": cout<<"Not Found\n";
  103.     searchTST(root, "cat")?  cout<<"Found\n": cout<<"Not Found\n";
  104.  
  105.     getch();
  106. }

Output
Following is traversal of ternary search tree
bug
cat
cats
up
 
Following are search results for cats, bu and cat respectively
Found
Not Found
Found

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.