Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Heap = Node {-# UNPACK #-} !Int {-# UNPACK #-} !(Heap) {-# UNPACK #-} !(Heap) | None
  2.    
  3. instance Show(Heap) where
  4.     show None = ""
  5.     show (Node a l r) = "("++(show a)++":"++(show l)++","++(show r)++")"
  6.  
  7. makeUnHeap k n = if k >= n
  8.                  then None
  9.                  else Node k (makeUnHeap (2*k+1) n) (makeUnHeap (2*k+2) n)
  10.  
  11. pushDown None = None
  12. pushDown (Node a None None) = Node a None None
  13. pushDown (Node a None (Node b l r)) = if a >= b then Node a None (Node b l r)
  14.                                     else Node b None (pushDown(Node a l r))
  15. pushDown (Node a (Node b l r) None) = if a >= b then Node a (Node b l r) None
  16.                                     else Node b (pushDown(Node a l r)) None
  17. pushDown (Node a (Node b l r) (Node c h s)) = if a >= b && a >= c
  18.                                             then Node a (Node b l r) (Node c h s)
  19.                                             else (if b >= c && b >= a
  20.                                                   then Node b (pushDown(Node a l r)) (Node c h s)
  21.                                                   else Node c (Node b l r) (pushDown(Node a h s))
  22.                                                  )
  23.  
  24. buildHeap None = None
  25. buildHeap (Node a l r) = pushDown(Node a (buildHeap l) (buildHeap r) )
  26.  
  27. {- Too slow!
  28.  
  29. _popElem :: Ord a => Heap a -> [Bool] -> (Heap a, a)
  30. _popElem (Node a l r) [] = (None, a)
  31. _popElem (Node a l r) (f:tail) = if f
  32.                                  then (let (nh,e) = _popElem r tail
  33.                                        in (Node a l nh, e))
  34.                                  else (let (nh,e) = _popElem l tail
  35.                                        in (Node a nh r, e))                                      
  36.  
  37. _path :: Integer -> [Bool]
  38. _path 0 = []
  39. _path n = even n : _path ( quot (n-1) 2 )
  40.  
  41. popLast :: Ord a => Heap a -> Integer -> (Heap a, a)
  42. popLast h n = _popElem h (reverse (_path (n - 1)))
  43. -}
  44.  
  45. popEny (Node a None None) = (None, a)
  46. popEny (Node a None r) = (Node a None nh, e)
  47.                          where (nh, e) = popEny r
  48. popEny (Node a l r) = (Node a nh r, e)
  49.                          where (nh, e) = popEny l
  50.  
  51.  
  52. changeHead (Node a l r) b = Node b l r
  53.  
  54. getRSorted None 0 = []
  55. getRSorted (Node a l r) 1 = [a]
  56. getRSorted (Node a l r) n = a : getRSorted ( pushDown(changeHead nh e) ) (n - 1)
  57.                             where (nh, e) = popEny (Node a l r)
  58. --                            where (nh, e) = popLast (Node a l r) n
  59.  
  60. getSorted h n = reverse(getRSorted h n)
  61.  
  62. main :: IO ()
  63. main = let n = 1000000
  64.        in putStrLn ( if ([0..n-1] == (getSorted(buildHeap(makeUnHeap 0 n)) n))
  65.                      then "OK"
  66.                      else "FAIL"
  67.                    )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement