Java Program to Implement Ternary Search Tree

This is a Java Program to Implement Ternary Search Tree. A ternary search tree is a type of prefix tree where nodes are arranged as a binary search tree. Like other prefix trees, a ternary search tree can be used as an associative map structure with the ability for incremental string search.

Here is the source code of the Java Program to Implement Ternary Search Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  ** Java Program to Implement Ternary Search Tree
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.ArrayList;
  7.  
  8. /** class TSTNode **/
  9. class TSTNode
  10. {
  11.     char data;
  12.     boolean isEnd;
  13.     TSTNode left, middle, right;
  14.  
  15.     /** Constructor **/
  16.     public TSTNode(char data)
  17.     {
  18.         this.data = data;
  19.         this.isEnd = false;
  20.         this.left = null;
  21.         this.middle = null;
  22.         this.right = null;
  23.     }        
  24. }
  25.  
  26. /** class TernarySearchTree **/
  27. class TernarySearchTree
  28. {
  29.     private TSTNode root;
  30.     private ArrayList<String> al;
  31.  
  32.     /** Constructor **/
  33.     public TernarySearchTree()
  34.     {
  35.         root = null;
  36.     }
  37.     /** function to check if empty **/
  38.     public boolean isEmpty()
  39.     {
  40.         return root == null;
  41.     }
  42.     /** function to clear **/
  43.     public void makeEmpty()
  44.     {
  45.         root = null;
  46.     }
  47.     /** function to insert for a word **/
  48.     public void insert(String word)
  49.     {
  50.         root = insert(root, word.toCharArray(), 0);
  51.     }
  52.     /** function to insert for a word **/
  53.     public TSTNode insert(TSTNode r, char[] word, int ptr)
  54.     {
  55.         if (r == null)
  56.             r = new TSTNode(word[ptr]);
  57.  
  58.         if (word[ptr] < r.data)
  59.             r.left = insert(r.left, word, ptr);
  60.         else if (word[ptr] > r.data)
  61.             r.right = insert(r.right, word, ptr);
  62.         else
  63.         {
  64.             if (ptr + 1 < word.length)
  65.                 r.middle = insert(r.middle, word, ptr + 1);
  66.             else
  67.                 r.isEnd = true;
  68.         }
  69.         return r;
  70.     }
  71.  
  72.     /** function to delete a word **/
  73.     public void delete(String word)
  74.     {
  75.         delete(root, word.toCharArray(), 0);
  76.     }
  77.     /** function to delete a word **/
  78.     private void delete(TSTNode r, char[] word, int ptr)
  79.     {
  80.         if (r == null)
  81.             return;
  82.  
  83.         if (word[ptr] < r.data)
  84.             delete(r.left, word, ptr);
  85.         else if (word[ptr] > r.data)
  86.             delete(r.right, word, ptr);
  87.         else
  88.         {
  89.             /** to delete a word just make isEnd false **/
  90.             if (r.isEnd && ptr == word.length - 1)
  91.                 r.isEnd = false;
  92.  
  93.             else if (ptr + 1 < word.length)
  94.                 delete(r.middle, word, ptr + 1);
  95.         }        
  96.     }
  97.  
  98.     /** function to search for a word **/
  99.     public boolean search(String word)
  100.     {
  101.         return search(root, word.toCharArray(), 0);
  102.     }
  103.     /** function to search for a word **/
  104.     private boolean search(TSTNode r, char[] word, int ptr)
  105.     {
  106.         if (r == null)
  107.             return false;
  108.  
  109.         if (word[ptr] < r.data)
  110.             return search(r.left, word, ptr);
  111.         else if (word[ptr] > r.data)
  112.             return search(r.right, word, ptr);
  113.         else
  114.         {
  115.             if (r.isEnd && ptr == word.length - 1)
  116.                 return true;
  117.             else if (ptr == word.length - 1)
  118.                 return false;
  119.             else
  120.                 return search(r.middle, word, ptr + 1);
  121.         }        
  122.     }
  123.     /** function to print tree **/
  124.     public String toString()
  125.     {
  126.         al = new ArrayList<String>();
  127.         traverse(root, "");
  128.         return "\nTernary Search Tree : "+ al;
  129.     }
  130.     /** function to traverse tree **/
  131.     private void traverse(TSTNode r, String str)
  132.     {
  133.         if (r != null)
  134.         {
  135.             traverse(r.left, str);
  136.  
  137.             str = str + r.data;
  138.             if (r.isEnd)
  139.                 al.add(str);
  140.  
  141.             traverse(r.middle, str);
  142.             str = str.substring(0, str.length() - 1);
  143.  
  144.             traverse(r.right, str);
  145.         }
  146.     }
  147. }
  148.  
  149. /** class TernarySearchTree **/
  150. public class TernarySearchTreeTest
  151. {
  152.     public static void main(String[] args)
  153.     {
  154.         Scanner scan = new Scanner(System.in);
  155.  
  156.         /* Creating object of TernarySearchTree */
  157.         TernarySearchTree tst = new TernarySearchTree(); 
  158.         System.out.println("Ternary Search Tree Test\n"); 
  159.  
  160.         char ch;
  161.         /*  Perform tree operations  */
  162.         do    
  163.         {
  164.             System.out.println("\nTernary Search Tree Operations\n");
  165.             System.out.println("1. insert word");
  166.             System.out.println("2. search word");
  167.             System.out.println("3. delete word");
  168.             System.out.println("4. check empty");
  169.             System.out.println("5. make empty");
  170.  
  171.             int choice = scan.nextInt();            
  172.             switch (choice)
  173.             {
  174.             case 1 : 
  175.                 System.out.println("Enter word to insert");
  176.                 tst.insert( scan.next() );                     
  177.                 break;                          
  178.             case 2 : 
  179.                 System.out.println("Enter word to search");
  180.                 System.out.println("Search result : "+ tst.search( scan.next() ));
  181.                 break; 
  182.             case 3 : 
  183.                 System.out.println("Enter word to delete");
  184.                 tst.delete( scan.next() );                     
  185.                 break; 
  186.             case 4 : 
  187.                 System.out.println("Empty Status : "+ tst.isEmpty() );                
  188.                 break;    
  189.             case 5 : 
  190.                 System.out.println("Ternary Search Tree cleared"); 
  191.                 tst.makeEmpty();               
  192.                 break;                                        
  193.             default : 
  194.                 System.out.println("Wrong Entry \n ");
  195.                 break;   
  196.             }
  197.             System.out.println(tst);
  198.  
  199.             System.out.println("\nDo you want to continue (Type y or n) \n");
  200.             ch = scan.next().charAt(0);                        
  201.         } while (ch == 'Y'|| ch == 'y');        
  202.     }
  203. }

Ternary Search Tree Test
 
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
4
Empty Status : true
 
Ternary Search Tree : []
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
apple
 
Ternary Search Tree : [apple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
pine
 
Ternary Search Tree : [apple, pine]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
pineapple
 
Ternary Search Tree : [apple, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
pin
 
Ternary Search Tree : [apple, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
in
 
Ternary Search Tree : [apple, in, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
i
 
Ternary Search Tree : [apple, i, in, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
mango
 
Ternary Search Tree : [apple, i, in, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
man
 
Ternary Search Tree : [apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
an
 
Ternary Search Tree : [an, apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
1
Enter word to insert
a
 
Ternary Search Tree : [a, an, apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
2
Enter word to search
pin
Search result : true
 
Ternary Search Tree : [a, an, apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
2
Enter word to search
pines
Search result : false
 
Ternary Search Tree : [a, an, apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
2
Enter word to search
pine
Search result : true
 
Ternary Search Tree : [a, an, apple, i, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
3
Enter word to delete
i
 
Ternary Search Tree : [a, an, apple, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
2
Enter word to search
i
Search result : false
 
Ternary Search Tree : [a, an, apple, in, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
3
Enter word to delete
in
 
Ternary Search Tree : [a, an, apple, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
3
Enter word to delete
apple
 
Ternary Search Tree : [a, an, man, mango, pin, pine, pineapple]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
3
Enter word to delete
pineapple
 
Ternary Search Tree : [a, an, man, mango, pin, pine]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
2
Enter word to search
apple
Search result : false
 
Ternary Search Tree : [a, an, man, mango, pin, pine]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
4
Empty Status : false
 
Ternary Search Tree : [a, an, man, mango, pin, pine]
 
Do you want to continue (Type y or n)
 
y
 
Ternary Search Tree Operations
 
1. insert word
2. search word
3. delete word
4. check empty
5. make empty
5
Ternary Search Tree cleared
 
Ternary Search Tree : []
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

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

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.