From b328c8da7fe795b22d34f12c68d7e482e03b1fdb Mon Sep 17 00:00:00 2001 From: wes Date: Mon, 3 Jul 2017 00:24:43 -0400 Subject: [PATCH] add compress function --- suffixes.hs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/suffixes.hs b/suffixes.hs index 42386dc..41f4e80 100644 --- a/suffixes.hs +++ b/suffixes.hs @@ -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