Skip to content

Commit

Permalink
missing methods for splay.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Jan 27, 2012
1 parent 37fabf7 commit e4249df
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 54 deletions.
12 changes: 10 additions & 2 deletions Data/Set/BUSplay.hs
Expand Up @@ -33,6 +33,7 @@ module Data.Set.BUSplay (
, valid
, showSet
, printSet
, (===)
) where

import Data.List (foldl')
Expand All @@ -45,9 +46,16 @@ data Splay a = Leaf | Node (Splay a) a (Splay a) deriving Show
instance (Eq a) => Eq (Splay a) where
t1 == t2 = toList t1 == toList t2

data LRb a = L a (Splay a) | R a (Splay a) deriving Show
{-| Checking if two splay sets are exactly the same shape.
-}
(===) :: Eq a => Splay a -> Splay a -> Bool
Leaf === Leaf = True
(Node l1 x1 r1) === (Node l2 x2 r2) = x1 == x2 && l1 === l2 && r1 === r2
_ === _ = False

data Direction a = L a (Splay a) | R a (Splay a) deriving Show

type Path a = [LRb a]
type Path a = [Direction a]

search :: Ord a => a -> Splay a -> (Splay a, Path a)
search k s = go s []
Expand Down
61 changes: 57 additions & 4 deletions Data/Set/Splay.hs
Expand Up @@ -17,9 +17,9 @@ module Data.Set.Splay (
-- * Membership
, member
-- * Deleting
-- , delete
, delete
, deleteMin
-- , deleteMax
, deleteMax
-- * Checking
, null
-- * Set operations
Expand All @@ -30,8 +30,9 @@ module Data.Set.Splay (
, partition
-- , merge
, minimum
-- , maximum
, maximum
, valid
, (===)
, showSet
, printSet
) where
Expand All @@ -46,6 +47,13 @@ data Splay a = Leaf | Node (Splay a) a (Splay a) deriving Show
instance (Eq a) => Eq (Splay a) where
t1 == t2 = toList t1 == toList t2

{-| Checking if two splay sets are exactly the same shape.
-}
(===) :: Eq a => Splay a -> Splay a -> Bool
Leaf === Leaf = True
(Node l1 x1 r1) === (Node l2 x2 r2) = x1 == x2 && l1 === l2 && r1 === r2
_ === _ = False

----------------------------------------------------------------

{-| Splitting smaller and bigger with splay.
Expand Down Expand Up @@ -133,7 +141,7 @@ fromList = foldl' (flip insert) empty

----------------------------------------------------------------

{-| Creating a list from a set. O(N)
{-| Creating a list from a set.
>>> toList (fromList [5,3])
[3,5]
Expand Down Expand Up @@ -179,6 +187,18 @@ minimum :: Splay a -> (a, Splay a)
minimum Leaf = error "minimum"
minimum t = let (x,mt) = deleteMin t in (x, Node Leaf x mt)

{-| Finding the maximum element.
>>> fst $ maximum (fromList [3,5,1])
5
>>> maximum empty
*** Exception: maximum
-}

maximum :: Splay a -> (a, Splay a)
maximum Leaf = error "maximum"
maximum t = let (x,mt) = deleteMax t in (x, Node mt x Leaf)

----------------------------------------------------------------

{-| Deleting the minimum element.
Expand All @@ -196,6 +216,39 @@ deleteMin (Node (Node Leaf lx lr) x r) = (lx, Node lr x r)
deleteMin (Node (Node ll lx lr) x r) = let (k,mt) = deleteMin ll
in (k, Node mt lx (Node lr x r))

{-| Deleting the maximum
>>> snd (deleteMax (fromList [(5,"a"), (3,"b"), (7,"c")])) == fromList [(3,"b"), (5,"a")]
True
>>> deleteMax empty
*** Exception: deleteMax
-}

deleteMax :: Splay a -> (a, Splay a)
deleteMax Leaf = error "deleteMax"
deleteMax (Node l x Leaf) = (x,l)
deleteMax (Node l x (Node rl rx Leaf)) = (rx, Node l x rl)
deleteMax (Node l x (Node rl rx rr)) = let (k,mt) = deleteMax rr
in (k, Node (Node l x rl) rx mt)

----------------------------------------------------------------

{-| Deleting this element from a set. O(log N)
>>> delete 5 (fromList [5,3]) == singleton 3
True
>>> delete 7 (fromList [5,3]) == fromList [3,5]
True
>>> delete 5 empty == empty
True
-}

delete :: Ord a => a -> Splay a -> Splay a
delete x t = case member x t of
(True, Leaf) -> Leaf
(True, Node l _ r) -> union l r
(False, _) -> t

----------------------------------------------------------------
{-| Creating a union set from two sets.
Expand Down

0 comments on commit e4249df

Please sign in to comment.