Advertisement
gelita

Trie

Feb 8th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.00 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class MyCode {
  4.     public static void main (String[] args) {
  5.         String[] dict = {"cat","car","cart","catheter","bat","balance","bot","apple"};
  6.     Trie trie = new Trie();
  7.    
  8.     for(String word : dict) trie.addWord(word);
  9.     System.out.println(trie.autocomplete("car"));
  10.     }
  11. }
  12.  
  13.  
  14. class TrieNode {
  15.   public char value;
  16.   public boolean endWord;
  17.   public HashMap<Character, TrieNode> children;
  18.  
  19.   public TrieNode(char ch) {
  20.     this.value = ch;
  21.     this.endWord = false;
  22.     this.children = new HashMap<>();
  23.   }
  24. }
  25.  
  26. class Trie {
  27.   TrieNode root;
  28.  
  29.   public Trie() {
  30.     this.root = new TrieNode('\u0000');
  31.   }
  32.  
  33.   public void addWord(String word) {
  34.     //Grab root node into current node pointer
  35.     TrieNode curr = this.root;
  36.     //Loop over chars in word
  37.     for(Character ch: word.toCharArray()){
  38.       //Check if child char not exists in children of curr trienode
  39.       if(!curr.children.containsKey(ch)) {  
  40.         //Add new trie node for child char to children of curr trienode
  41.         curr.children.put(ch, new TrieNode(ch));
  42.       }
  43.       //Move to the trienode associated with the child char
  44.       curr = curr.children.get(ch);
  45.     }
  46.     //Set endWord flag for curr pointer to true
  47.     curr.endWord = true;
  48.   }
  49.  
  50.   public TrieNode getPrefixEndNode(String prefix) {
  51.     //Grab root node into current node pointer
  52.     TrieNode curr = this.root;
  53.     //Loop over chars in word
  54.     for(Character ch: prefix.toCharArray()){
  55.       //Check if child char not exists in children of curr trienode
  56.       if(!curr.children.containsKey(ch)) {  
  57.         return null;
  58.       }
  59.       //Move to the trienode associated with the child char
  60.       curr = curr.children.get(ch);
  61.     }
  62.     return curr;
  63.   }
  64.  
  65.   public boolean prefixExists(String prefix) {
  66.     return getPrefixEndNode(prefix) != null;
  67.   }
  68.  
  69.   public ArrayList<String> autocomplete(String prefix) {
  70.     //Init empty arraylist
  71.     ArrayList<String> result = new ArrayList<>();
  72.     //Get the prefix end node
  73.     TrieNode endPrefixNode = getPrefixEndNode(prefix);
  74.    
  75.     //if prefix end node not null, dfs from node
  76.     if(endPrefixNode != null) {
  77.       //dfs with prefix string, end prefix node, result list, null char literal
  78.       dfs(prefix, endPrefixNode, result, '\u0000');
  79.     }
  80.     // return result list
  81.     return result;
  82.    
  83.   }
  84.  
  85.   private void dfs(String currStr, TrieNode currNode, ArrayList<String> result, char currCh){
  86.     //Add currCh to currStr
  87.     currStr += currCh;
  88.     //Base case when endWord flag reached
  89.     if(currNode.endWord){
  90.       //Add currStr to results
  91.       result.add(currStr);
  92.     }
  93.    
  94.     //Loop through child chars of currNode
  95.     for(TrieNode childNode: currNode.children.values()) {
  96.       //dfs on currStr, child TrieNode, result arraylist, child char
  97.       dfs(currStr, childNode, result, childNode.value);
  98.     }
  99.    
  100.   }
  101.  
  102.   /*
  103.  
  104.   Given 3 different systems with dictionaries of words, come up with efficient way of storing data
  105.  
  106.   */
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement