|
|
@ -10,10 +10,18 @@ data Trie = TBranch { |
|
|
|
} |
|
|
|
deriving (Show) |
|
|
|
|
|
|
|
|
|
|
|
compress t@(TBranch root []) = t |
|
|
|
compress (TBranch root children) |
|
|
|
| length children == 1 = |
|
|
|
let compressed = (compress $ head children) in TBranch (root++(getRoot compressed)) (getChildren compressed) |
|
|
|
| otherwise = TBranch root (map compress children) |
|
|
|
|
|
|
|
notEmpty [] = False |
|
|
|
notEmpty _ = True |
|
|
|
|
|
|
|
buildTrie [] = TBranch "" [] |
|
|
|
buildTrie ("":_) = TBranch "" [] |
|
|
|
buildTrie words = |
|
|
|
let root = head $ head words |
|
|
|
groups = groupTails $ map tail words |
|
|
@ -22,7 +30,13 @@ buildTrie words = |
|
|
|
trie words = TBranch "" (map buildTrie $ groupTails $ tails words) |
|
|
|
|
|
|
|
groupTails [] = [] |
|
|
|
groupTails xs = L.groupBy ((==) `F.on` head) $ L.sort $ filter notEmpty xs |
|
|
|
groupTails xs = L.groupBy grouper $ L.sort xs |
|
|
|
where grouper [] [] = True |
|
|
|
grouper [] _ = False |
|
|
|
grouper _ [] = False |
|
|
|
grouper a b = (head a) == (head b) |
|
|
|
|
|
|
|
tails "" = [] |
|
|
|
tails (w@(c:cs)) = w : tails cs |
|
|
|
|
|
|
|
suffixTree word = compress $ trie word |
|
|
|