我无法理解怎么说树从给定的输入字符串中生成。
本质上,您创建了一个包含您列出的所有后缀的patricia trie。当插入到patricia trie中时,您搜索的是从输入字符串中的第一个字符开始的孩子的根,如果它存在,则继续向下,但如果不存在,则从根中创建一个新节点。根在输入字符串($,a,e,h,i,n,r,s,t,w)中将具有与独特字符一样多的孩子。您可以为输入字符串中的每个字符继续该过程。
后缀树用于在给定的字符串中查找给定的子字符串,但给定的树如何帮助该字符串?
如果您正在查找子字符串“hen”,则从根开始搜索以“h”开头的子项。如果子字符串“h”的长度然后继续处理子字符“h”,直到您到达字符串的末尾,或者输入字符串和子字符串“h”中的字符不匹配。如果你匹配所有的孩子“h”,即输入“hen”与孩子“h”中的“he”匹配,那么继续前进到“h”的孩子,直到你到达“n”,如果它找不到孩子的开始用“n”表示子字符串不存在。
Compact Suffix Trie code:
└── (black)
├── (white) as
├── (white) e
│ ├── (white) eir
│ ├── (white) en
│ └── (white) ere
├── (white) he
│ ├── (white) heir
│ ├── (white) hen
│ └── (white) here
├── (white) ir
├── (white) n
├── (white) r
│ └── (white) re
├── (white) s
├── (white) the
│ ├── (white) their
│ └── (white) there
└── (black) w
├── (white) was
└── (white) when
Suffix Tree code:
String = the$their$there$was$when$
End of word character = $
└── (0)
├── (22) $
├── (25) as$
├── (9) e
│ ├── (10) ir$
│ ├── (32) n$
│ └── (17) re$
├── (7) he
│ ├── (2) $
│ ├── (8) ir$
│ ├── (31) n$
│ └── (16) re$
├── (11) ir$
├── (33) n$
├── (18) r
│ ├── (12) $
│ └── (19) e$
├── (26) s$
├── (5) the
│ ├── (1) $
│ ├── (6) ir$
│ └── (15) re$
└── (29) w
├── (24) as$
└── (30) hen$
我发现这2个视频了解后缀树非常有帮助。 http://www.youtube.com/watch?v=VA9m_l6LpwI&http://www.youtube.com/watch?v=F3nbY3hIDLQ#t=2360 – spats