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 Implement Queue using Two Stacks
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to find the number of occurrences of a given number using Binary Search approach
Semaphore trong Java
Java Program to Check whether Undirected Graph is Connected using DFS
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Implement Double Order Traversal of a Binary Tree
Lập trình đa luồng với Callable và Future trong Java
Deque và ArrayDeque trong Java
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
How to Break from Java Stream forEach
Finding the Differences Between Two Lists in Java
Introduction to Java 8 Streams
Java Program to Implement HashSet API
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Implement the RSA Algorithm
Java InputStream to String
@DynamicUpdate with Spring Data JPA
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
How to Implement Caching using Adonis.js 5
Performance Difference Between save() and saveAll() in Spring Data
Serverless Functions with Spring Cloud Function
Lớp Arrarys trong Java (Arrays Utility Class)
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Netflix Archaius with Various Database Configurations
Hướng dẫn sử dụng Java Annotation
Autoboxing và Unboxing trong Java
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Apache Commons Collections OrderedMap
LinkedHashSet trong Java hoạt động như thế nào?