5

I want to print or retrieve all the words stored in Trie Data Structure. This is because I want to compute Edit distance between a misspelled word and a word in Dictionary. Therefore I was thinking of retrieving each word from Trie and compute Edit distance. But I am not able to retrieve. I want some code snippet for this. This is how I have implemented the Trie using HashMap in Java

Now please tell me how to write code for printing all words stored in Trie. Any help is very much appreciated

TrieNode.java

package triehash;
import java.io.Serializable;
import java.util.HashMap;

public class TrieNode implements Serializable {

HashMap<Character, HashMap> root;

public TrieNode() {
   root = new HashMap<Character, HashMap>();   
   }
}

TrieDict.java

package triehash;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;;
import java.io.Serializable;
import java.util.HashMap;
import java.io.Serializable;

public class TrieDict {   
 public  TrieNode createTree()
 {
     TrieNode t = new TrieNode();
     return t;
 }

 public void add(String s, TrieNode root_node) {
    HashMap<Character, HashMap> curr_node = root_node.root;
    s = s.toLowerCase();
    for (int i = 0, n = s.length(); i < n; i++) {
        Character c = s.charAt(i);
        if (curr_node.containsKey(c))
            curr_node = curr_node.get(c);
        else {
            curr_node.put(c, new HashMap<Character, HashMap>());
            curr_node = curr_node.get(c);
        }
    }
    curr_node.put('\0', new HashMap<Character, HashMap>(0)); // term
  }

 public void serializeDict(TrieNode root_node)
 {    
   try{
        FileOutputStream fout = new FileOutputStream("/home/priya/NetBeansProjects/TrieHash/dict.ser");

    ObjectOutputStream oos = new ObjectOutputStream(fout);   
    oos.writeObject(root_node);
    oos.close();
    System.out.println("Done");

   }catch(Exception ex){
       ex.printStackTrace();
   }
}

 public void addAll(String[] sa,TrieNode root_node) {
    for (String s: sa)
        add(s,root_node);
 }

 public static void main(String[] args)
 {
    TrieDict td = new TrieDict();
    TrieNode tree = td.createTree();

    String[] words = {"an", "ant", "all", "allot", "alloy", "aloe", "are", "ate", "be"};
    for (int i = 0; i < words.length; i++)
      td.add( words[i],tree);       
    td.serializeDict(tree); /* seriliaze dict*/
 }   
}
Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
user2281107
  • 319
  • 2
  • 5
  • 14

1 Answers1

0

First, it's worth noting that the declared type of the root instance variable is a little odd. (Specifically, the value type of HashMap<Character,HashMap> excludes some of the generics you'd rather it used.) The code below should work, but you'll get some warnings as a result of this. You might try refactoring your code to use the type HashMap<Character,TrieNode> instead. Sorry if that's pedantic. :)

Try this, added as methods to TrieNode:

public Set<String> computeWords() {
    Set<String> result;

    if(root.size() == 0)
        result = new HashSet<String>();
    else
        result = computeWords(root, "");

    return result;
}

protected static Set<String> computeWords(HashMap tree, String prefix) {
    Set<String> result=new HashSet<String>();

    if(tree.size() == 0)
        result.add(prefix);
    else
        for(Object o : tree.keySet()) {
            Character c=(Character) o;
            prefix = prefix+c;
            result.addAll(computeWords((HashMap) tree.get(c), prefix));
            prefix = prefix.substring(0, prefix.length()-1);
        }

    return result;
}

For a given TrieNode object t, t.computeWords() would return the set of all words words encoded in t.

I believe this answers the question you were trying to ask. However, to answer the question as stated in the header, you'd print all the words for the same t like this:

for(String word : t.computeWords())
    System.out.println(word);

Also, this definitely isn't the most efficient implementation, especially since we create a bunch of HashSet objects in computeWords(HashMap,String), but it should work!

EDIT: This code also assumes that you terminate words with an empty HashMap. If you terminate words with null instead, you'd need to update the if(tree.size() == 0) check in the static method with if(tree == null). Sorry, should have called that out.

EDIT: Explained how to print all the words, just in case it wasn't clear.

EDIT: Fixed empty trie case.

sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • @sigpwned.. Thanks for your help. I am facing now another issue. The below code does not work String word1="ant" Set Words = ts.computeWords(tree.root); if (Words.contains(word1)) System.out.println("Word exists"); – user2281107 Apr 16 '13 at 08:55
  • Hi @user2281107. That sounds like a separate question, so you should probably ask it as another top-level question. – sigpwned Apr 16 '13 at 15:05