How to print all words stored in a tree if three were implemented using a hashmap in Java?

I want to print or download all the words stored in the Trie Data Structure. This is because I want to calculate the distance “Edit” between the misspelled word and the word in the dictionary. So I was thinking about extracting each word from Trie and calculating the distance Edit. But I can’t get it. I need a code snippet for this. This is how I implemented Trie using HashMapin Java

Now please tell me how to write code to print all the words stored in Trie. Any help is 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*/
 }   
}
+5
source share
1 answer

-, , root . ( , HashMap<Character,HashMap> , .) , . , HashMap<Character,TrieNode>. , .:)

, 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;
}

TrieNode t, t.computeWords() , t.

, , . , , , t :

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

, , , HashSet computeWords(HashMap,String), !

EDIT. , HashMap. null , if(tree.size() == 0) static if(tree == null). , .

EDIT. , , , .

: .

0

All Articles