我想打印或检索存储在Trie Data Structure中的所有单词.这是因为我想计算拼写错误的单词和词典中的单词之间的编辑距离.因此我想从Trie中检索每个单词并计算编辑距离.但我无法找回.我想要一些代码片段.这就是我HashMap在Java中实现Trie的方法
现在请告诉我如何编写代码来打印存储在Trie中的所有单词.很感谢任何形式的帮助
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>();
}
}
Run Code Online (Sandbox Code Playgroud)
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*/
}
}
Run Code Online (Sandbox Code Playgroud)
首先,值得注意的是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;
}
Run Code Online (Sandbox Code Playgroud)
对于给定的TrieNode对象t,t.computeWords()将返回 中编码的所有单词的集合t。
我相信这回答了您想问的问题。但是,要回答标题中所述的问题,您需要打印所有相同的单词,t如下所示:
for(String word : t.computeWords())
System.out.println(word);
Run Code Online (Sandbox Code Playgroud)
另外,这绝对不是最有效的实现,特别是因为我们在 中创建了一堆HashSet对象computeWords(HashMap,String),但它应该可以工作!
编辑:此代码还假设您以空终止单词HashMap。如果您改为使用 终止单词null,则需要使用 更新方法if(tree.size() == 0)中的检查。抱歉,应该说出来。staticif(tree == null)
编辑:解释了如何打印所有单词,以防万一不清楚。
编辑:修复了空特里树案例。
| 归档时间: |
|
| 查看次数: |
1141 次 |
| 最近记录: |