-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuffixTree.py
More file actions
68 lines (57 loc) · 1.77 KB
/
SuffixTree.py
File metadata and controls
68 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Suffix Tree
class Node:
def __init__(self, sub="", children=None):
self.sub = sub
self.ch = children or []
class SuffixTree:
def __init__(self, str):
self.nodes = [Node()]
for i in range(len(str)):
self.addingSuffix(str[i:])
def addingSuffix(self, suf):
n = 0
i = 0
while i < len(suf):
b = suf[i]
x2 = 0
while True:
children = self.nodes[n].ch
if x2 == len(children):
n2 = len(self.nodes)
self.nodes.append(Node(suf[i:], []))
self.nodes[n].ch.append(n2)
return
n2 = children[x2]
if self.nodes[n2].sub[0] == b:
break
x2 = x2 + 1
sub2 = self.nodes[n2].sub
j = 0
while j < len(sub2):
if suf[i + j] != sub2[j]:
n3 = n2
n2 = len(self.nodes)
self.nodes.append(Node(sub2[:j], [n3]))
self.nodes[n3].sub = sub2[j:]
self.nodes[n].ch[x2] = n2
break
j = j + 1
i = i + j
n = n2
def printTreeEdges(self):
if len(self.nodes) == 0:
return
def f(n, pre):
children = self.nodes[n].ch
if len(children) == 0 and n != 0:
print(self.nodes[n].sub)
return
if n!= 0 :
print(self.nodes[n].sub)
for c in children[:-1]:
f(c, pre)
f(children[-1], pre )
f(0, "")
if __name__ == '__main__':
inputString = input()
SuffixTree(inputString).printTreeEdges()