Advertisement
NLinker

RB tree insert only (release)

Jul 9th, 2017
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Tree where
  2.  
  3. data Color = R | B
  4. data Tree a = E | T Color (Tree a) a (Tree a)
  5.  
  6. insert :: Ord a => a -> Tree a -> Tree a
  7. insert x t =
  8.   let T _ a y b = ins t in
  9.   T B a y b
  10.   where
  11.     ins E          =  T R E x E
  12.     ins s@(T col a y b)
  13.       | x < y      =  balance col (ins a) y b
  14.       | x > y      =  balance col a y (ins b)
  15.       | otherwise  =  s
  16.  
  17. balance :: Color -> Tree a -> a -> Tree a -> Tree a
  18. 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)
  19. 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)
  20. 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)
  21. 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)
  22. balance col a x b = T col a x b
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement