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:

Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Hướng dẫn Java Design Pattern – MVC
Spring RestTemplate Error Handling
HttpClient Connection Management
Java Program to Implement Warshall Algorithm
Java Program to Perform Arithmetic Operations on Numbers of Size
Introduction to Spring MVC HandlerInterceptor
Send an email with an attachment
Java Program to Represent Graph Using Incidence List
Spring MVC Setup with Kotlin
Java Program to Find Number of Articulation points in a Graph
Java Program to Find Path Between Two Nodes in a Graph
Intro to Spring Boot Starters
Spring Boot - Tomcat Deployment
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java – Byte Array to Reader
Java Program to Implement CountMinSketch
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement Self organizing List
Spring Security Authentication Provider
Java Program to Implement Brent Cycle Algorithm
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Convert char to String in Java
Java Program to Search for an Element in a Binary Search Tree
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement Disjoint Sets
An Intro to Spring Cloud Contract
Hướng dẫn Java Design Pattern – Service Locator
Apache Camel with Spring Boot