Wacky_rubia

red-black tree2

May 16th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO
  2. --import Data.List as List
  3. import qualified Data.Map as Map
  4. import qualified Data.Set as Set
  5.  
  6. data Color = Red | Black deriving (Eq)
  7. instance Show(Color) where
  8.     show c
  9.         | c == Red = "0"
  10.         | otherwise = "1"
  11.  
  12. data BinTree a = Node Color a (BinTree a) (BinTree a) | Leaf deriving (Eq)
  13.  
  14. instance Show a => Show(BinTree a) where
  15.     show t = show1 t 0 where
  16.         show1 Leaf _ = "\n"
  17.         show1 (Node c x l r) n =
  18.             show1 l (n + 2) ++ (replicate n ' ') ++ (show x)++ show1 r (n + 2)
  19.  
  20. empty :: BinTree a
  21. empty = Leaf
  22.  
  23. include :: Ord a => a -> BinTree a -> Bool
  24. include _ Leaf = False
  25. include x (Node _ a left right)
  26.     | x == a = True
  27.     | x > a = include x right
  28.     | otherwise = include x left
  29.  
  30. insert :: Ord a => BinTree a -> a -> BinTree a
  31. insert tree x = insertOne $ insertLeaf tree x
  32.  
  33. insertOne :: BinTree a -> BinTree a
  34. insertOne (Node color x left right) = Node Black x left right
  35.  
  36. insertLeaf :: Ord a => BinTree a -> a -> BinTree a
  37. insertLeaf Leaf x = Node Red x Leaf Leaf
  38. insertLeaf (Node color a left right) x
  39.     | x == a = Node color a left right
  40.     | x > a = balance color a left (insertLeaf right x)
  41.     | otherwise = balance color a (insertLeaf left x) right
  42.  
  43. find_next (Node _ a left _)
  44.     | left == Leaf = a
  45.     | otherwise = find_next left
  46.  
  47.  
  48. delete :: Ord a => a -> BinTree a -> BinTree a
  49. delete x tree = turnB' s
  50.  where
  51.    (s,_) = delete' x tree
  52.  
  53. delete' :: Ord a => a -> BinTree a -> RBTreeBDel a
  54. delete' _ Leaf = (Leaf, False)
  55. delete' x (Node c y l r) = case compare x y of
  56.    LT -> let (l',d) = delete' x l
  57.              t = Node c y l' r
  58.           in if d then unbalancedR c y l' r else (t, False)
  59.    GT -> let (r',d) = delete' x r
  60.              t = Node c y l r'
  61.           in if d then unbalancedL c y l r' else (t, False)
  62.    EQ -> case r of
  63.        Leaf -> if c == Black then blackify l else (l, False)
  64.        _ -> let next = find_next r
  65.                 (r',d) = delete' next r
  66.                 t = Node c next l r'
  67.              in if d then unbalancedL c next l r' else (t, False)
  68.  
  69. blackify :: BinTree a -> RBTreeBDel a
  70. blackify s@(Node Red _ _ _) = (turnB s, False)
  71. blackify s = (s, True)
  72.  
  73. turnR :: BinTree a -> BinTree a
  74. turnR Leaf = error "turnR"
  75. turnR (Node _ x left right) = Node Red x left right
  76.  
  77. turnB :: BinTree a -> BinTree a
  78. turnB Leaf = error "turnB"
  79. turnB (Node _ x left right) = Node Black x left right
  80.  
  81. turnB' :: BinTree a -> BinTree a
  82. turnB' Leaf = Leaf
  83. turnB' (Node _ x left right) = Node Black x left right
  84.  
  85. type RBTreeBDel a = (BinTree a, Bool)
  86.  
  87. unbalancedL :: Color -> a -> BinTree a -> BinTree a -> RBTreeBDel a
  88. unbalancedL color x left@(Node Black _ _ _) right = (balance Black x (turnR left) right, color == Black)
  89. unbalancedL Black x (Node Red lx ll lr@(Node Black _ _ _)) r = (Node Black lx ll (balance Black x (turnR lr) r), False)
  90.  
  91. unbalancedR :: Color -> a -> BinTree a -> BinTree a -> (BinTree a, Bool)
  92. unbalancedR color x left right@(Node Black _ _ _) = (balance Black x left (turnR right), color == Black)
  93. unbalancedR Black x l (Node Red rx rl@(Node Black _ _ _) rr) = (Node Black rx (balance Black x l (turnR rl)) rr, False)
  94.  
  95. balance :: Color -> a -> BinTree a -> BinTree a -> BinTree a
  96. balance Black z (Node Red y (Node Red x a b) c) d = Node Red y (Node Black x a b) (Node Black z c d)
  97. balance Black z (Node Red x a (Node Red y b c)) d = Node Red y (Node Black x a b) (Node Black z c d)
  98. balance Black x a (Node Red y b (Node Red z c d)) = Node Red y (Node Black x a b) (Node Black z c d)
  99. balance Black x a (Node Red z (Node Red y b c) d) = Node Red y (Node Black x a b) (Node Black z c d)
  100. balance color a x b = Node color a x b
  101.  
  102. listToTree::Ord a => [a] -> BinTree a
  103. listToTree xs = foldl insert Leaf xs
  104.  
  105. treeToList:: BinTree a -> [a]
  106. treeToList Leaf = []
  107. treeToList (Node _ x l r) = (treeToList l)++[x]++(treeToList r)
  108.  
  109. treeSort:: Ord a => [a] -> [a]
  110. treeSort = treeToList.listToTree
  111.  
  112. height:: BinTree a -> Int
  113. height Leaf = 0
  114. height (Node _ _ l r) = 1 + max (height l) (height r)
  115.  
  116. main = do
  117.     let tree = listToTree [x | x <- [1..13]]
  118.     print $ tree
  119.     --let tree0 = insert tree 18
  120.     --print $ tree0
  121.     let tree1 = delete 10 tree
  122.     print $ tree1
  123.     let tree2 = delete 4 tree1
  124.     print $ tree2
  125.     let tree3 = delete 13 tree2
  126.     print $ tree3
  127.     --let tree4 = insert tree3 10
  128.     --print $ tree4
  129.     --let new_tree = insert tree 7
  130.     --print $ new_tree
Advertisement
Add Comment
Please, Sign In to add comment