package com.spakai.index;

import java.util.HashMap;
import java.util.Map;

class TrieNode {
    char c;
    HashMap<Character, TrieNode>  children = new HashMap<>();
    boolean isLeaf;
    
    public TrieNode() {
        
    }
    
    public TrieNode(char c) {
        this.c = c;
    }
    
}

public class TrieIndex {
    private TrieNode root;
    
    public TrieIndex() {
        root = new TrieNode();
    }
    
    public void insert(String word) {
         Map<Character, TrieNode> children = root.children;
         
         for(int i=0; i < word.length(); i++) {
             char c = word.charAt(i);
             
             TrieNode t;
             if (children.containsKey(c)) {
                 t = children.get(c);
             } else {
                 t = new TrieNode(c);
                 children.put(c, t);
             }
             
             children = t.children;
             
             if ( i == word.length() - 1) {
                 t.isLeaf = true;
             }
             
         }
    }
    
    public boolean search(String word) {
        TrieNode t = searchNode(word);
 
        if(t != null && t.isLeaf) 
            return true;
        else
            return false;
        
    }
    
    public String bestSearch(String word) {
        
        Map<Character, TrieNode> children = root.children;
        TrieNode t = null;
        String bestMatched = "";
        boolean flaggedAsLeaf = false;
        
        for( int i =0 ; i < word.length(); i++) {
            char c = word.charAt(i);
            if (children.containsKey(c)) {
                t = children.get(c);
                bestMatched += t.c;
                if (t.isLeaf) flaggedAsLeaf= true;
                children = t.children;
            } else {
                if (flaggedAsLeaf) {
                    return bestMatched;
                } else {
                    return null;
                }
            }
        }
        
        return null;
        
    }
    
    public TrieNode searchNode(String word) {
        
        Map<Character, TrieNode> children = root.children;
        TrieNode t = null;
        for (int i =0 ; i < word.length(); i++) {
            char c = word.charAt(i);
            if(children.containsKey(c)) {
                t = children.get(c);
                children = t.children;
            } else {
                return null;
            }
            
          
            
        }
        return t;  
    }
    
}

@@ -94,24 +94,41 @@
     char c;%0A
+    String word;%0A
     HashMap%3C
@@ -965,24 +965,56 @@
 th() - 1) %7B%0A
+                 t.word = word;%0A
             
@@ -1445,63 +1445,28 @@
 ing 
-bestMatched = %22%22;%0A        boolean flaggedAsLeaf = false
+longestPrefix = null
 ;%0A  
@@ -1659,27 +1659,8 @@
     
-bestMatched += t.c;
 %0A   
@@ -1690,66 +1690,52 @@
 af) 
-flaggedAsLeaf= true;%0A                children = t.children
+%7B%0A                    longestPrefix = t.word
 ;%0A  
@@ -1740,32 +1740,29 @@
             
-%7D else %7B
+    %7D
 %0A           
@@ -1770,73 +1770,31 @@
     
-if (flaggedAsLeaf) %7B%0A                    return bestMatched;%0A    
+children = t.children;%0A
     
@@ -1830,42 +1830,29 @@
     
-    
 return 
-null;%0A                %7D
+longestPrefix;
 %0A   

Refactor code to store complete word at leaf node