Skip to content

Instantly share code, notes, and snippets.

View Abhiroop's full-sized avatar
:electron:
"Syntactic sugar causes cancer of the semicolon."

Abhiroop Sarkar Abhiroop

:electron:
"Syntactic sugar causes cancer of the semicolon."
View GitHub Profile
balL (T B t1 y (T B t2 z t3)) = balance' (T B t1 y (T R t2 z t3))
balL (T B t1 y (T R (T B t2 u t3) z t4@(T B l value r))) =
T R (T B t1 y t2) u (balance' (T B t3 z (T R l value r)))
balL (T B (T R t1 x t2) y t3) = T R (T B t1 x t2) y t3
balL :: Tree a -> Tree a
balR :: Tree a -> Tree a
delete :: (Ord a) => a -> Tree a -> Tree a
delete x t = makeBlack $ del x t
where makeBlack (T _ a y b) = T B a y b
del :: (Ord a) => a -> Tree a -> Tree a
del x t@(T _ l y r)
| x < y = delL x t
| x > y = delR x t
| otherwise = fuse l r
balance :: Color -> Tree a -> a -> Tree a -> Tree a
balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
balance color a x b = T color a x b
T B (T R (T R a x b) y c) z d
T B (T R a x (T R b y c)) z d
T B a x (T R (T R b y c) z d)
T B a x (T R b y (T R c z d))
insert :: (Ord a) => a -> Tree a -> Tree a
insert x s = makeBlack $ ins s
where ins E = T R E x E
ins (T color a y b)
| x < y = balance color (ins a) y b
| x == y = T color a y b
| x > y = balance color a y (ins b)
makeBlack (T _ a y b) = T B a y b
member :: (Ord a) => a -> Tree a -> Bool
member x E = False
member x (T _ a y b)
| x < y = member x a
| x == y = True
| otherwise = member x b