Hy
I am using Trie data structure for the implementation of dictionary search.
The problem i am facing is when a word is formed by user and searched it return True on the basis of prefix.

How i can change the code to return true only when whole string match instead of some prefix.?


Thanx in advance

Code

public class Trys {
     static Trys trieDSA;
    public static BufferedReader br = null;
    private static class Node {
        
        private boolean isWord = false; // indicates a complete word
        private int prefixes = 0; // indicates how many words have the prefix
        private Map children = new HashMap(); // references to all possible
                                                // children
    }

    private Node root = new Node();

    /**
     * Inserts a new word into the trie
     *
     * @param word
     */
    public void insertWord(String word) {
        if (searchWord(word) == true)
            return;

        Node current = root;
        for (char c : word.toCharArray()) {
            if (current.children.containsKey(Character.valueOf(c))) {
                Node child = (Node) current.children.get(Character.valueOf(c));
                child.prefixes++;

                current = child;
            } else {
                Node child = new Node();
                child.prefixes = 1;

                current.children.put(Character.valueOf(c), child);
                current = child;
            }
        }
        // we have reached the endof the word, hence mark it true
        // if during a search we reach the end of the search string and this
        // flag is still false, then the search string is not registered as a
        // valid
        // word in the trie but is a prefix
        current.isWord = true;
    }

    /**
     * Searches for a word in the trie
     *
     * @param word
     */
    public boolean searchWord(String word) {
        Node current = root;
        for (char c : word.toCharArray()) {
            if (current.children.containsKey(Character.valueOf(c))) {
                current = (Node) current.children.get(Character.valueOf(c));
            } else {
                return false;
            }
        }
        // if at the end of the search of entire word the boolean variable is
        // still false means that the given word is not regitered as a valid
        // word in the trie, but is a prefix
        return current.isWord;
    }
    
    

        
    public static void main(String[] args) {
        Trys trie = new Trys();
         try {
            
               br = new BufferedReader(new FileReader(new File("C:/Users/cyber/Downloads/WordList.txt")));
               String eachLine = null;
               while((eachLine=br.readLine())!=null){
                trie.insertWord(eachLine);
               }
              } catch (Exception e) {
               e.printStackTrace();
              }


    
        System.out.println(trie.searchWord("jo"));
        System.out.println(trie.searchWord("joe"));

        
    }