Java Program to Implement Patricia Trie

This is a Java Program to Implement Patricia Trie. A radix tree (also patricia trie or radix trie or compact prefix tree) is a space-optimized trie data structure where each node with only one child is merged with its child.

Here is the source code of the Java Program to Implement Patricia Trie. 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 Patricia Trie
  3.  **/
  4.  
  5. import java.util.Scanner;
  6.  
  7. /** Class PatriciaNode **/
  8. class PatriciaNode
  9. {
  10.     int bitNumber;
  11.     int data;
  12.     PatriciaNode leftChild, rightChild;
  13. }
  14.  
  15. /* Class PatriciaTrie */
  16. class PatriciaTrie
  17. {
  18.     private PatriciaNode root;
  19.     private static final int MaxBits = 10;
  20.  
  21.     /** Constructor **/
  22.     public PatriciaTrie()
  23.     {
  24.         root = null;        
  25.     }
  26.     /** function to check if empty **/
  27.     public boolean isEmpty()
  28.     {
  29.         return root == null;
  30.     }
  31.     /** function to clear **/
  32.     public void makeEmpty()
  33.     {
  34.         root = null;        
  35.     }
  36.     /** function to get ith bit of key k from left **/
  37.     private boolean bit(int k, int i)
  38.     {
  39.         String binary = Integer.toString(k, 2);    
  40.         while (binary.length() != MaxBits)
  41.             binary = "0" + binary;    
  42.         if (binary.charAt(i - 1) == '1')
  43.             return true ;
  44.         return false;
  45.     }
  46.     /** function to search for an element **/
  47.     public boolean search(int k)
  48.     {
  49.         int numOfBits = (int) (Math.log(k)/Math.log(2)) + 1;
  50.         if (numOfBits > MaxBits)
  51.         {
  52.             System.out.println("Error : Number too large");
  53.             return false;
  54.         }
  55.         PatriciaNode searchNode = search(root, k);
  56.         if (searchNode.data == k)
  57.             return true;
  58.         else
  59.             return false;
  60.     }
  61.     /** function to search for an element **/
  62.     private PatriciaNode search(PatriciaNode t, int k)
  63.     {
  64.         PatriciaNode currentNode, nextNode;
  65.         if (t == null)
  66.         {
  67.             return null;
  68.         }
  69.         nextNode = t.leftChild;
  70.         currentNode = t;
  71.         while (nextNode.bitNumber > currentNode.bitNumber)
  72.         {
  73.             currentNode = nextNode;
  74.             nextNode = (bit(k, nextNode.bitNumber)) ? nextNode.rightChild : nextNode.leftChild;
  75.         }
  76.         return nextNode;
  77.     }
  78.     /** function to insert and element **/
  79.     public void insert(int ele)
  80.     {
  81.         int numOfBits = (int) (Math.log(ele)/Math.log(2)) + 1;
  82.         if (numOfBits > MaxBits)
  83.         {
  84.             System.out.println("Error : Number too large");
  85.             return;
  86.         }
  87.         root = insert(root, ele);        
  88.     }
  89.     /** function to insert and element **/
  90.     private PatriciaNode insert(PatriciaNode t, int ele)
  91.     {
  92.         PatriciaNode current, parent, lastNode, newNode;
  93.         int i;
  94.  
  95.         if (t == null)
  96.         {
  97.             t = new PatriciaNode();
  98.             t.bitNumber = 0;
  99.             t.data = ele;
  100.             t.leftChild = t;
  101.             t.rightChild = null;    
  102.             return t;        
  103.         }
  104.  
  105.         lastNode = search(t, ele);
  106.  
  107.         if (ele == lastNode.data)
  108.         {
  109.             System.out.println("Error : key is already present\n");
  110.             return t;
  111.         }
  112.  
  113.         for (i = 1; bit(ele, i) == bit(lastNode.data, i); i++);
  114.  
  115.         current = t.leftChild; parent = t;
  116.         while (current.bitNumber > parent.bitNumber && current.bitNumber < i)
  117.         {
  118.             parent = current;
  119.             current = (bit(ele, current.bitNumber)) ? current.rightChild : current.leftChild;
  120.         }
  121.  
  122.         newNode = new PatriciaNode();
  123.         newNode.bitNumber = i;
  124.         newNode.data = ele;
  125.         newNode.leftChild = bit(ele, i) ? current : newNode;
  126.         newNode.rightChild = bit(ele, i) ? newNode : current;        
  127.  
  128.         if (current == parent.leftChild)
  129.             parent.leftChild = newNode;
  130.         else
  131.             parent.rightChild = newNode;
  132.  
  133.         return t;
  134.     }        
  135. }
  136.  
  137. /* Class PatriciaTrie Test */
  138. public class PatriciaTrieTest
  139. {
  140.     public static void main(String[] args)
  141.     {            
  142.         Scanner scan = new Scanner(System.in);
  143.  
  144.         /* Creating object of PatriciaTrie */
  145.         PatriciaTrie pt = new PatriciaTrie(); 
  146.         System.out.println("Patricia Trie Test\n"); 
  147.  
  148.         char ch;
  149.         /*  Perform trie operations  */
  150.         do    
  151.         {
  152.             System.out.println("\nPatricia Trie Operations\n");
  153.             System.out.println("1. insert ");
  154.             System.out.println("2. search");
  155.             System.out.println("3. check emepty");
  156.             System.out.println("4. make emepty");
  157.  
  158.             int choice = scan.nextInt();            
  159.             switch (choice)
  160.             {
  161.             case 1 : 
  162.                 System.out.println("Enter key element to insert");
  163.                 pt.insert( scan.nextInt() );                     
  164.                 break;                          
  165.             case 2 : 
  166.                 System.out.println("Enter key element to search");
  167.                 System.out.println("Search result : "+ pt.search( scan.nextInt() ));
  168.                 break;  
  169.             case 3 : 
  170.                 System.out.println("Empty Status : "+ pt.isEmpty() );                
  171.                 break;    
  172.             case 4 : 
  173.                 System.out.println("Patricia Trie cleared"); 
  174.                 pt.makeEmpty();               
  175.                 break;                                        
  176.             default : 
  177.                 System.out.println("Wrong Entry \n ");
  178.                 break;   
  179.             }
  180.  
  181.             System.out.println("\nDo you want to continue (Type y or n) \n");
  182.             ch = scan.next().charAt(0);                        
  183.         } while (ch == 'Y'|| ch == 'y');               
  184.     }
  185. }

Patricia Trie Test
 
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
3
Empty Status : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
24
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
6
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
28
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
5
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
63
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
19
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
94
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
1
Enter key element to insert
24
Error : key is already present
 
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
24
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
5
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
6
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
7
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
19
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
15
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
94
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
97
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
32
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
2
Enter key element to search
28
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Patricia Trie Operations
 
1. insert
2. search
3. check emepty
4. make emepty
4
Patricia Trie cleared
 
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.

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.