Skip to content

Instantly share code, notes, and snippets.

@sordina
Created September 29, 2015 04:04
Show Gist options
  • Save sordina/350c6015e666f4971add to your computer and use it in GitHub Desktop.
Save sordina/350c6015e666f4971add to your computer and use it in GitHub Desktop.
-- Clustering algorithm v2
destroy _ E = E
destroy 1 (L _) = E
destroy _ (L x) = L x
destroy n x@(N l r) | count x == n = E
| count x > n = N (destroy n l) (destroy n r)
| otherwise = x
search _ E = []
search 1 (L x) = [L x]
search _ (L x) = []
search n x@(N l r) | count x == n = [x]
| count x > n = search n l ++ search n r
| otherwise = []
expand n = takeWhile (> 0) $ concatMap ex [0..] where ex x = [n-x,n+x]
scanner (t,_) n = (destroy n t, search n t)
cluster n t = concatMap (map leaves . snd) $ scanl scanner (t,[]) (expand (count t `div` n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment