Advertisement
gelita

Autocomplete trie

Mar 27th, 2020
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.52 KB | None | 0 0
  1. // package whatever; // don't place package name!
  2.  
  3. import java.util.*;
  4.  
  5. class MyCode {
  6.     public static void main (String[] args) {
  7.     //Given an input list of words, design autocomplete function
  8.     //to return list of words when given an input prefix
  9.    
  10.         String[] words = {"car","camp","catheter","cart","cat","crab","apple","ball","boy","bolster"};
  11.     Trie trie = new Trie();
  12.     for(String word: words) trie.addWord(word);
  13.    
  14.     System.out.println(trie.autocomplete(""));
  15.     }
  16. }
  17.  
  18.  
  19. class TrieNode {
  20.   char data;
  21.   boolean endWord;
  22.   HashMap<Character, TrieNode> children;
  23.  
  24.   public TrieNode(char val){
  25.     this.data = val;
  26.     this.endWord = false;
  27.     this.children = new HashMap<>();
  28.   }
  29. }
  30.  
  31. class Trie {
  32.   TrieNode root;
  33.  
  34.   public Trie(){
  35.     this.root = new TrieNode(Character.MIN_VALUE); //'\u0000'
  36.   }
  37.  
  38.   public void addWord(String word) {
  39.     //Create pointer starting root trienode
  40.     TrieNode curr = this.root;
  41.    
  42.     HashMap<Character, TrieNode> currChildren;
  43.     //Loop characters of word and inserting new node for each into trie
  44.     for(Character ch: word.toCharArray()) {
  45.       currChildren = curr.children;
  46.       if(!currChildren.containsKey(ch)) {
  47.         currChildren.put(ch, new TrieNode(ch));
  48.       }
  49.       //Move curr node pointer to child node associated with child char
  50.       curr = currChildren.get(ch);
  51.     }
  52.     curr.endWord = true;
  53.   }
  54.  
  55.   private TrieNode getPrefixEndNode(String prefix) {
  56.     //Create pointer starting root trienode
  57.     TrieNode curr = this.root;
  58.    
  59.     HashMap<Character, TrieNode> currChildren;
  60.     //Loop characters of word and inserting new node for each into trie
  61.     for(Character ch: prefix.toCharArray()) {
  62.       currChildren = curr.children;
  63.       if(!currChildren.containsKey(ch)) {
  64.         return null;
  65.       }
  66.       //Move curr node pointer to child node associated with child char
  67.       curr = currChildren.get(ch);
  68.     }
  69.     return curr;
  70.   }
  71.  
  72.   public ArrayList<String> autocomplete(String prefix) {
  73.     TrieNode prefixEndNode = getPrefixEndNode(prefix);
  74.     ArrayList<String> result = new ArrayList<>();
  75.    
  76.     if(prefixEndNode != null) {
  77.       //DFS
  78.       dfs(prefix, prefixEndNode, result);
  79.     }
  80.     return result;
  81.   }
  82.  
  83.   private void dfs(String currPrefix, TrieNode currNode, ArrayList<String> result) {
  84.     if(currNode.endWord) {
  85.       result.add(currPrefix);
  86.     }
  87.    
  88.     for(TrieNode child: currNode.children.values()) {
  89.       dfs(currPrefix+child.data, child, result);
  90.     }
  91.   }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement