Skip to content

Commit

Permalink
cosmetic change.
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Oct 28, 2011
1 parent d08574c commit 0d4df1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
25 changes: 12 additions & 13 deletions Data/RBTree.hs
Expand Up @@ -29,28 +29,27 @@ fromList = foldl' (flip insert) empty
--

insert :: Ord a => a -> RBTree a -> RBTree a
insert a b = Fork B d e f
insert kx t = turnB (ins t)
where
Fork _ d e f = ins a b
ins x Leaf = Fork R Leaf x Leaf
ins x t@(Fork c l y r) = case compare x y of
LT -> balanceL c (ins x l) y r
GT -> balanceR c l y (ins x r)
EQ -> t
ins Leaf = Fork R Leaf kx Leaf
ins s@(Fork k l x r) = case compare kx x of
LT -> balanceL k (ins l) x r
GT -> balanceR k l x (ins r)
EQ -> s

balanceL :: Color -> RBTree a -> a -> RBTree a -> RBTree a
balanceL B (Fork R (Fork R a x b) y c) z d =
Fork R (Fork B a x b) y (Fork B c z d)
balanceL B (Fork R a x (Fork R b y c)) z d =
Fork R (Fork B a x b) y (Fork B c z d)
balanceL k a x b = Fork k a x b
balanceL k l x r = Fork k l x r

balanceR :: Color -> RBTree a -> a -> RBTree a -> RBTree a
balanceR B a x (Fork R b y (Fork R c z d)) =
Fork R (Fork B a x b) y (Fork B c z d)
balanceR B a x (Fork R (Fork R b y c) z d) =
Fork R (Fork B a x b) y (Fork B c z d)
balanceR k a x b = Fork k a x b
balanceR k l x r = Fork k l x r

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

Expand All @@ -72,9 +71,9 @@ unbalancedR _ _ _ _ = error "unbalancedR"
----------------------------------------------------------------

deleteMin :: RBTree a -> RBTree a
deleteMin t = t'
deleteMin t = s
where
((t', _), _) = deleteMin' t
((s, _), _) = deleteMin' t

deleteMin' :: RBTree a -> (RBTreeBDel a, a)
deleteMin' Leaf = error "deleteMin'"
Expand All @@ -94,9 +93,9 @@ blackify s@(Fork R _ _ _) = (turnB s, False)
blackify s = (s, True)

delete :: Ord a => a -> RBTree a -> RBTree a
delete x s = s'
delete x t = s
where
(s',_) = delete' x s
(s,_) = delete' x t

delete' :: Ord a => a -> RBTree a -> RBTreeBDel a
delete' _ Leaf = (Leaf, False)
Expand Down
21 changes: 10 additions & 11 deletions Data/RBTree/LL.hs
Expand Up @@ -34,19 +34,18 @@ fromList = foldl' (flip insert) empty
----------------------------------------------------------------

insert :: Ord a => a -> RBTree a -> RBTree a
insert a b = Fork B d e f
insert kx t = turnB (ins t)
where
Fork _ d e f = ins a b
ins x Leaf = Fork R Leaf x Leaf
ins x t@(Fork c l y r) = case compare x y of
LT -> balanceL c (ins x l) y r
GT -> balanceR c l y (ins x r)
EQ -> t
ins Leaf = Fork R Leaf kx Leaf
ins s@(Fork k l x r) = case compare kx x of
LT -> balanceL k (ins l) x r
GT -> balanceR k l x (ins r)
EQ -> s

balanceL :: Color -> RBTree a -> a -> RBTree a -> RBTree a
balanceL B (Fork R (Fork R a x b) y c) z d =
Fork R (Fork B a x b) y (Fork B c z d)
balanceL k a x b = Fork k a x b
balanceL k l x r = Fork k l x r

balanceR :: Color -> RBTree a -> a -> RBTree a -> RBTree a
balanceR B (Fork R a x b) y (Fork R c z d) =
Expand All @@ -55,7 +54,7 @@ balanceR B (Fork R a x b) y (Fork R c z d) =
-- x is either Fork or Leaf
balanceR k x y (Fork R c z d) =
Fork k (Fork R x y c) z d
balanceR k a x b = Fork k a x b
balanceR k l x r = Fork k l x r

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

Expand All @@ -74,7 +73,7 @@ isBlackLeftRed _ = False
deleteMin :: RBTree a -> RBTree a
deleteMin t = case deleteMin' (turnR t) of
Leaf -> Leaf
t' -> turnB t'
s -> turnB s

{-
This deleteMin' keeps an invariant: the target node is always red.
Expand Down Expand Up @@ -128,7 +127,7 @@ hardMin _ = error "hardMin"
deleteMax :: RBTree a -> RBTree a
deleteMax t = case deleteMax' (turnR t) of
Leaf -> Leaf
t' -> turnB t'
s -> turnB s

{-
This deleteMax' keeps an invariant: the target node is always red.
Expand Down

0 comments on commit 0d4df1c

Please sign in to comment.