我正在尝试用Java构建Trie实现,我已经做到了。实现的主要部分已经完成,我有添加一个单词或检查结构是否包含单词的方法,但是我在尝试打印特里树时遇到问题。
我正在尝试以.dot
格式打印Trie,以便能够使用GraphViz创建Trie的.pdf
文件。
当前,我为print
类提供了一个TrieNode
方法,该方法以正确的格式正确打印了除索引以外的所有节点。我似乎无法获得为节点连接找到正确的索引号的功能。
这是一个最小的工作示例:
Trie
类别:
package trie;
import trie.*;
public class Trie {
private TrieNode root;
public Trie () {
this.root = new TrieNode();
}
public TrieNode getRoot() {
return this.root;
}
public void add(String word) {
this.getRoot().add(word);
}
public boolean contains(String word) {
int current;
int length = word.length();
TrieNode crawler = this.getRoot();
for (current = 0; current < length; current++) {
char currentChar = word.charAt(current);
if (!(crawler.getChildren().containsKey(currentChar))) {
return false;
}
crawler = crawler.getChildren().get(currentChar);
}
return (crawler.isWord());
}
public static void main(String[] args) {
Trie t = new Trie();
t.add("apples");
t.add("bananas");
System.out.println(t.getRoot().print(0, 1));
}
}
TrieNode
类别:
package trie;
import trie.*;
import java.util.*;
public class TrieNode {
private HashMap<Character, TrieNode> children;
private String content;
private boolean isWord;
public TrieNode() {
this.children = new HashMap<Character, TrieNode>();
this.content = "";
this.isWord = false;
}
public TrieNode(String word, Character charToAdd) {
this.children = new HashMap<Character, TrieNode>();
this.content = word + String.valueOf(charToAdd);
this.isWord = true;
}
public String getContent() {
return this.content;
}
public HashMap<Character, TrieNode> getChildren() {
return this.children;
}
public boolean isWord() {
return this.isWord;
}
public void makeWord() {
this.isWord = true;
}
public void addChild(Character charToAdd) {
TrieNode child = new TrieNode(this.getContent(), charToAdd);
child.isWord = false;
this.getChildren().put(charToAdd, child);
}
public void add(String word) {
if (word.length() == 0) {
this.makeWord();
} else {
char first = word.charAt(0);
if (!(this.getChildren().containsKey(first))) {
this.addChild(first);
}
this.getChildren().get(first).add(word.substring(1));
}
}
public String print(int from, int to) {
String res = "";
if (this.getChildren().isEmpty()) {
from = 0;
to = to + 1;
return "";
} else {
for (char c : this.getChildren().keySet()) {
res += String.format("\t %d -> %d [label=\" %s\"]\n", from, to, c);
res += this.getChildren().get(c).print(to, to+1);
}
}
return res;
}
}
main
程序的执行结果为:
0 -> 1 [label=" a"]
1 -> 2 [label=" p"]
2 -> 3 [label=" p"]
3 -> 4 [label=" l"]
4 -> 5 [label=" e"]
5 -> 6 [label=" s"]
0 -> 1 [label=" b"]
1 -> 2 [label=" a"]
2 -> 3 [label=" n"]
3 -> 4 [label=" a"]
4 -> 5 [label=" n"]
5 -> 6 [label=" a"]
6 -> 7 [label=" s"]
我正试图让它返回:
0 -> 1 [label=" a"]
1 -> 2 [label=" p"]
2 -> 3 [label=" p"]
3 -> 4 [label=" l"]
4 -> 5 [label=" e"]
5 -> 6 [label=" s"]
0 -> 7 [label=" b"]
7 -> 8 [label=" a"]
8 -> 9 [label=" n"]
9 -> 10 [label=" a"]
10 -> 11 [label=" n"]
11 -> 12 [label=" a"]
12 -> 13 [label=" s"]
索引应该回到根索引所在的位置,但是我找不到阻止它重置的方法...
编辑:我唯一需要更改的变量是to
变量,但似乎无法在上下文之外重新定义
你有什么想法吗?