Java Program to Implement Hash Trie

This is a Java Program to implement Hash 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 Hash Trie. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 *   Java Program to Implement Hash Trie
 **/
 
import java.io.*;
import java.util.*;
 
class HashTrie
{
    private HashMap<Character, HashMap> root;
 
    /** Constructor **/
    public HashTrie() 
    {
       root = new HashMap<Character, HashMap>();
    }
    /** Parameterised Constructor **/
    public HashTrie(String[] arr) 
    {
        root = new HashMap<Character, HashMap>();
        for (String s: arr)
            add(s);
    }
 
    /** Function to add a string to hash trie **/
    public void add(String str) 
    {
        HashMap<Character, HashMap> node = root;
        for (int i = 0; i < str.length(); i++)
        {
            if (node.containsKey(str.charAt(i)))
                node = node.get(str.charAt(i));
            else 
            {
                node.put(str.charAt(i), new HashMap<Character, HashMap>());
                node = node.get(str.charAt(i));
            }
        }
        /** end of string **/
        node.put('\0', new HashMap<Character, HashMap>(0)); 
    }
 
    /** Function to check if hash trie contains a given word **/
    public boolean contains(String str) 
    {
        HashMap<Character, HashMap> currentNode = root;
        for (int i = 0; i < str.length(); i++)
        {
            if (currentNode.containsKey(str.charAt(i)))
                currentNode = currentNode.get(str.charAt(i));
            else 
                return false;
        }        
        return currentNode.containsKey('\0') ? true : false;            
    }
}
 
/** Class HashTrieTest **/
public class HashTrieTest
{
    public static void main(String[] args) throws IOException
    {    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        /** Accept words **/        
        System.out.println("Trie Test\n");
        System.out.println ("Enter words to be entered into trie");
        String input = br.readLine();
        String[] s = input.split(" ");
        /** Create Trie with accepted words **/
        HashTrie t = new HashTrie(s);
        /** Trie Test **/
        char ch = 'n';
        do
        { 
            System.out.println("\nEnter word to search ");
            String key = br.readLine();
            System.out.println("Search status : "+ t.contains(key));
 
            System.out.println("\nDo you want to continue (Type y or n) \n");
            ch = br.readLine().charAt(0);                        
        } while (ch == 'Y'|| ch == 'y');         
    }
}
Trie Test
 
Enter words to be entered into trie
trie tree test branch leaf root fruit key lock hash
 
Enter word to search
trie
Search status : true
 
Do you want to continue (Type y or n)
 
y
 
Enter word to search
tree
Search status : true
 
Do you want to continue (Type y or n)
 
y
 
Enter word to search
trench
Search status : false
 
Do you want to continue (Type y or n)
 
y
 
Enter word to search
triee
Search status : false
 
Do you want to continue (Type y or n)
 
y
 
Enter word to search
trei
Search status : false
 
Do you want to continue (Type y or n)
 
y
 
Enter word to search
branch
Search status : true
 
Do you want to continue (Type y or n)
 
n

Related posts:

Hướng dẫn Java Design Pattern – Transfer Object
Supplier trong Java 8
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Guide to the Synchronized Keyword in Java
Introduction to the Java NIO Selector
Java Program to Implement Suffix Tree
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Split a String in Java
A Guide to LinkedHashMap in Java
Getting Started with GraphQL and Spring Boot
Java Program to Solve TSP Using Minimum Spanning Trees
Disable DNS caching
Java Program to Find the Edge Connectivity of a Graph
The XOR Operator in Java
String Processing with Apache Commons Lang 3
Check If Two Lists are Equal in Java
Java Program to Represent Graph Using 2D Arrays
How to Remove the Last Character of a String?
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
The Spring @Controller and @RestController Annotations
Apache Camel with Spring Boot
Java Program to Create a Random Graph Using Random Edge Generation
Spring Boot - File Handling
Java Program to Check Cycle in a Graph using Graph traversal
Spring WebClient and OAuth2 Support
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Converting String to Stream of chars