Java Program to Implement Trie

This is a Java Program to implement Trie. A trie is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node, instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that correspond to keys of interest.

Here is the source code of the Java program to implement Trie. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 *  Java Program to Implement Trie
 */
 
import java.util.*;
 
class TrieNode 
{
    char content; 
    boolean isEnd; 
    int count;  
    LinkedList<TrieNode> childList; 
 
    /* Constructor */
    public TrieNode(char c)
    {
        childList = new LinkedList<TrieNode>();
        isEnd = false;
        content = c;
        count = 0;
    }  
    public TrieNode subNode(char c)
    {
        if (childList != null)
            for (TrieNode eachChild : childList)
                if (eachChild.content == c)
                    return eachChild;
        return null;
    }
}
 
class Trie
{
    private TrieNode root;
 
     /* Constructor */
    public Trie()
    {
        root = new TrieNode(' '); 
    }
     /* Function to insert word */
    public void insert(String word)
    {
        if (search(word) == true) 
            return;        
        TrieNode current = root; 
        for (char ch : word.toCharArray() )
        {
            TrieNode child = current.subNode(ch);
            if (child != null)
                current = child;
            else 
            {
                 current.childList.add(new TrieNode(ch));
                 current = current.subNode(ch);
            }
            current.count++;
        }
        current.isEnd = true;
    }
    /* Function to search for word */
    public boolean search(String word)
    {
        TrieNode current = root;  
        for (char ch : word.toCharArray() )
        {
            if (current.subNode(ch) == null)
                return false;
            else
                current = current.subNode(ch);
        }      
        if (current.isEnd == true) 
            return true;
        return false;
    }
    /* Function to remove a word */
    public void remove(String word)
    {
        if (search(word) == false)
        {
            System.out.println(word +" does not exist in trie\n");
            return;
        }             
        TrieNode current = root;
        for (char ch : word.toCharArray()) 
        { 
            TrieNode child = current.subNode(ch);
            if (child.count == 1) 
            {
                current.childList.remove(child);
                return;
            } 
            else 
            {
                child.count--;
                current = child;
            }
        }
        current.isEnd = false;
    }
}    
 
/* Class Trie Test */
public class TrieTest
{
    public static void main(String[] args)
    {            
        Scanner scan = new Scanner(System.in);
        /* Creating object of AATree */
        Trie t = new Trie(); 
        System.out.println("Trie Test\n");          
        char ch;
        /*  Perform tree operations  */
        do    
        {
            System.out.println("\nTrie Operations\n");
            System.out.println("1. insert ");
            System.out.println("2. delete");
            System.out.println("3. search");
 
            int choice = scan.nextInt();            
            switch (choice)
            {
            case 1 : 
                System.out.println("Enter string element to insert");
                t.insert( scan.next() );                     
                break;                          
            case 2 : 
                System.out.println("Enter string element to delete");
                try
                {
                    t.remove( scan.next() ); 
                }                    
                catch (Exception e)
                {
                    System.out.println(e.getMessage()+" not found ");
                }
                break;                         
            case 3 : 
                System.out.println("Enter string element to search");
                System.out.println("Search result : "+ t.search( scan.next() ));
                break;                                          
            default : 
                System.out.println("Wrong Entry \n ");
                break;   
            }
 
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = scan.next().charAt(0);                        
        } while (ch == 'Y'|| ch == 'y');               
    }
}
Trie Test
 
 
Trie Operations
 
1. insert
2. delete
3. search
1
Enter string element to insert
trie
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
1
Enter string element to insert
tree
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
1
Enter string element to insert
branch
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
1
Enter string element to insert
beach
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
bean
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
beach
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
2
Enter string element to delete
bean
bean does not exist in trie
 
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
2
Enter string element to delete
beach
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
beach
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
2
Enter string element to delete
tree
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
tree
Search result : false
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
trie
Search result : true
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
2
Enter string element to delete
trie
 
Do you want to continue (Type y or n)
 
y
 
Trie Operations
 
1. insert
2. delete
3. search
3
Enter string element to search
trie
Search result : false
 
Do you want to continue (Type y or n)
 
n

Related posts:

Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
How to Get the Last Element of a Stream in Java?
Consumer trong Java 8
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Convert a Decimal Number to Binary Number using Stacks
What is Thread-Safety and How to Achieve it?
Using JWT with Spring Security OAuth
Java Program to Construct a Random Graph by the Method of Random Edge Selection
More Jackson Annotations
wait() and notify() Methods in Java
Guide to the Synchronized Keyword in Java
Java Program to Implement the MD5 Algorithm
New in Spring Security OAuth2 – Verify Claims
Java Program to Implement ConcurrentLinkedQueue API
So sánh ArrayList và Vector trong Java
Most commonly used String methods in Java
Spring Boot - Batch Service
An Introduction to ThreadLocal in Java
Request Method Not Supported (405) in Spring
Configuring a DataSource Programmatically in Spring Boot
Java Program to Implement Sorted Array
Injecting Prototype Beans into a Singleton Instance in Spring
Getting Started with Stream Processing with Spring Cloud Data Flow
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Solve the 0-1 Knapsack Problem
Implementing a Runnable vs Extending a Thread
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
SOAP Web service: Authentication trong JAX-WS
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Program to Implement LinkedBlockingDeque API