okasaki

Okasaki's Purely Functional Data Structures
git clone git://git.jtobin.io/okasaki.git
Log | Files | Refs | LICENSE

SplayHeap.hs (559B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 module SplayHeap where
      4 
      5 data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show
      6 
      7 insert :: Ord a => a -> Tree a -> Tree a
      8 insert x t = Node (smaller x t) x (bigger x t)
      9 
     10 bigger :: Ord a => a -> Tree a -> Tree a
     11 bigger _ Leaf = Leaf
     12 bigger pivot (Node a x b)
     13   | x <= pivot = bigger pivot b
     14   | otherwise  = case a of
     15       Leaf       -> Node Leaf x b
     16       Node l y r ->
     17         if   y <= pivot
     18         then Node (bigger pivot r) x b
     19         else Node (bigger pivot l) y (Node r x b)
     20 
     21 -- FIXME
     22 smaller :: a
     23 smaller = undefined
     24