2015-02-24 25 views
3

我想用O(m^2)而不是Ukkonen写一个天真的建筑算法来编写后缀树类。天真的后缀树在Java中的实现

我的疑问是关于如何表示树。到目前为止,我已经得到了这个。但我不认为这是节点和边的写类结构。任何有关如何保持节点和边缘之间的映射/关系的建议。重要的一点是,在边缘我们只是存储开始和结束索引以节省空间。

class suffixTree{ 
Node[] nodes; 
Edge[] edge; 
} 

class Node{ 
    int node_id; 
    Edge[] outgoingEdges; 
} 

class Edge{ 
    int start_index; 
    int end_index; 
    int start_node_id; 
    int end_node_id; 
} 
+0

我不明白你为什么需要'nodes'和'edges'。您应该只需要对根节点的引用。每个叶节点都应该包含对字符串中偏移量的引用。 'Edge.start_node_id'是不必要的。 – 2015-02-24 12:56:49

+0

非常感谢您的回复。你能否详细说明一下。可能会有一些阶级结构。 – Andy897 2015-02-24 14:44:27

+0

我告诉过你我将对班级结构所做的改变。剩下的就好了。 – 2015-02-24 16:20:09

回答

2

我会做这种方式:

class SuffixTree { 
    // A tree only needs to know about the root Node. 
    Node root; 
} 

// A Node contains a mapping from the first character to 
// an Edge that corresponds to it. 
// It doesn't need to know about anything else. 
class Node { 
    Map<Character, Edge> edgeMap; 
} 

// An Edge contains only the start and the end index of the substring 
// and the destination Node. 
class Edge { 
    int startIndex; 
    int endIndex; 
    Node destination; 
} 

最重要的变化是:

  1. 入门在所有三类去掉冗余信息。

  2. 使用引用而不是数组和索引。

+0

非常感谢@Kraskevich。没有得到Map edgeMap。你认为角色会有什么优势? – Andy897 2015-02-24 17:07:18

+0

@ Andy897此边缘所对应的子字符串的第一个字符。 – kraskevich 2015-02-24 17:31:12