Java 后缀树实现问题
我正在研究一些后缀树实现,这里有一个参考实现,问题是引用第19行的索引如何用于类后缀树节点?我不确定索引是否有用,我想我们可能只需要保留所有节点及其子节点的字符值?“找不到太多的索引值”用于类后缀树节点 请随时纠正我。任何见解都将不胜感激Java 后缀树实现问题,java,algorithm,trie,suffix-tree,Java,Algorithm,Trie,Suffix Tree,我正在研究一些后缀树实现,这里有一个参考实现,问题是引用第19行的索引如何用于类后缀树节点?我不确定索引是否有用,我想我们可能只需要保留所有节点及其子节点的字符值?“找不到太多的索引值”用于类后缀树节点 请随时纠正我。任何见解都将不胜感激 public class SuffixTree { SuffixTreeNode root = new SuffixTreeNode(); public SuffixTree(String s) { for (int i = 0
public class SuffixTree {
SuffixTreeNode root = new SuffixTreeNode();
public SuffixTree(String s) {
for (int i = 0; i < s.length(); i++) {
String suffix = s.substring(i);
root.insertString(suffix, i);
}
}
public ArrayList<Integer> getIndexes(String s) {
return root.getIndexes(s);
}
}
public class SuffixTreeNode {
HashMap<Character, SuffixTreeNode> children = new
HashMap<Character, SuffixTreeNode>();
char value;
ArrayList<Integer> indexes = new ArrayList<Integer>();
public SuffixTreeNode() { }
public void insertString(String s, int index) {
indexes.add(index);
if (s != null && s.length() > 0) {
value = s.charAt(0);
SuffixTreeNode child = null;
if (children.containsKey(value)) {
child = children.get(value);
} else {
child = new SuffixTreeNode();
children.put(value, child);
}
String remainder = s.substring(1);
child.insertString(remainder, index);
}
}
public ArrayList<Integer> getIndexes(String s) {
if (s == null || s.length() == 0) {
return indexes;
} else {
char first = s.charAt(0);
if (children.containsKey(first)) {
String remainder = s.substring(1);
return children.get(first).getIndexes(remainder);
}
}
return null;
}
}
public class Question {
public static void main(String[] args) {
String testString = “mississippi”;
String[] stringList = {“is”, “sip”, “hi”, “sis”};
SuffixTree tree = new SuffixTree(testString);
for (String s : stringList) {
ArrayList<Integer> list = tree.getIndexes(s);
if (list != null) {
System.out.println(s + “: “ + list.toString());
}
}
}
}
索引对于您正在查看的后缀树的实现来说肯定是必需的。后缀树有多个版本,其中一些版本比其他版本更优化。indexes变量在将子字符串为、sip、hi、sis存在于原始字符串mississippi中的索引返回给调用方法时起着不可或缺的作用。getIndexes在其基本情况下返回索引这是获取每个子字符串的出现列表的方式。见下面的输出
is: [1, 4]
sip: [6]
sis: [3]
@在递归方法中使用基本情况。这是告诉递归调用停止调用自身并返回堆栈的条件。基本情况是代码中的第39行。不,在第4-6行的for循环中没有一个bug索引正在增加。@LinMa看看生成的输出,在代码中分步进行,就像你是编译器一样,你会理解它的。看看子字符串,它打印出1的索引,4是correct@LinMa我讨厌这样回答,但是;确实,您必须说服自己/向自己证明应用程序递归地在做什么。我建议大家研究递归问题,youtube上有很多很好的教程。它将需要多个视频,你不会得到它后,只是一个让我们。@LinMa我不完全理解你的解释,但它是这样的。是的,你完全正确。索引意味着比赛的开始。对于子字符串sip,索引为6,因为在原始单词中,索引6开始匹配的开始,即在元素[6][7][8]中。子字符串的相同之处在于它有索引1和4,因为在原始单词中,您在这些位置找到了该子字符串的匹配项。如果你觉得我的解决方案有帮助,如果它回答了你原来的问题,别忘了接受。如果您还需要什么,请告诉我我运行了您的代码,它正在按设计工作。如果你有一个bug,假设你没有减少字符串,你会得到一个stackoverflow异常,因为你永远不会碰到递归的基本情况,陷入一个永远的循环