Wacky_rubia

red-black tree

May 15th, 2015
299
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 c)++ 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_ins color a left (insertLeaf right x)
  41.     | otherwise = balance_ins color a (insertLeaf left x) right
  42.  
  43. delete :: Ord a => BinTree a -> a -> BinTree a
  44. delete (Node color a left right) x
  45.     | a == x = delete_current (Node color a left right)
  46.     | x > a = balance color a left (delete right x)
  47.     | otherwise = balance color a (delete left x) right
  48.  
  49. delete_current :: Ord a => BinTree a -> BinTree a
  50. delete_current (Node color a left right)
  51.     | left == Leaf && right == Leaf = Leaf
  52.     | left == Leaf = right
  53.     | right == Leaf = left
  54.     | otherwise = do
  55.         let next = find_next right
  56.         balance color next left (delete right next)
  57.  
  58.  
  59. find_next (Node _ a left _)
  60.     | left == Leaf = a
  61.     | otherwise = find_next left
  62.  
  63. --bal :: Color -> a -> BinTree a -> BinTree a -> BinTree a
  64. --bal Black y (Node Red x a@(Node Red _ _ _) b) (Node Red z c d) = Node Red y (Node Black x a b) (Node Black z c d)
  65. --bal Black y (Node Red x a b@(Node Red _ _ _)) (Node Red z c d) = Node Red y (Node Black x a b) (Node Black z c d)
  66. --bal Black y (Node Red x a b) (Node Red z c@(Node Red _ _ _) d) = Node Red y (Node Black x a b) (Node Black z c d)
  67. --bal Black y (Node Red x a b) (Node Red z c d@(Node Red _ _ _)) = Node Red y (Node Black x a b) (Node Black z c d)
  68.  
  69. --bal Black y (Node Red x a@(Node Red _ _ _) b) c = Node Black x a (Node Red y b c)
  70. --bal Black x a (Node Red y b c@(Node Red _ _ _)) = Node Black y (Node Red x a b) c
  71.  
  72. --bal Black z (Node Red x a (Node Red y b c)) d = Node Black y (Node Red x a b) (Node Red z c d)
  73. --bal Black x a (Node Red z (Node Red y b c) d) = Node Black y (Node Red x a b) (Node Red z c d)
  74.  
  75. --bal color a x b = Node color a x b
  76.  
  77. balance_ins :: Color -> a -> BinTree a -> BinTree a -> BinTree a
  78. balance_ins Black b (Node Red a (Node Red x Leaf Leaf) Leaf) (Node Red c Leaf Leaf) = Node Red b (Node Black a (Node Red x Leaf Leaf) Leaf) (Node Black c Leaf Leaf)
  79. balance_ins Black b (Node Red a (Node Red x Leaf Leaf) Leaf) Leaf = Node Red b (Node Black a (Node Red x Leaf Leaf) Leaf) Leaf
  80.  
  81. balance_ins Black b (Node Red c Leaf Leaf) (Node Red a Leaf (Node Red x Leaf Leaf)) = Node Red b (Node Black c Leaf Leaf) (Node Black a Leaf (Node Red x Leaf Leaf))
  82. balance_ins Black b Leaf (Node Red a Leaf (Node Red x Leaf Leaf)) = Node Red b Leaf (Node Black a Leaf (Node Red x Leaf Leaf))
  83.  
  84. balance_ins Black b (Node Red a (Node Red x left right) right1) c = Node Black a (Node Red x left right) (Node Red b right1 c)
  85. balance_ins Black b (Node Red a (Node Red x left right) right1) c = Node Black a (Node Red x left right) (Node Red b right1 c)
  86. balance_ins c a b z = Node c a b z
  87.  
  88. balance_del :: Color -> a -> BinTree a -> BinTree a -> BinTree a
  89. balance_del Black a (Node Black x l r) (Node Red b c d) = Node Black b (Node Red a (Node Black x l r) c) d
  90. balance_del color a (Node Black x l r) (Node Black b c@(Node Black _ _ _) d@(Node Black _ _ _)) = Node Black a (Node Black x l r) (Node Red b c d)
  91. balance_del color a (Node Black x l r) (Node Black b (Node Red c l1 r1) d@(Node Black _ _ _)) = Node color a (Node Black x l r) (Node Black c l1 (Node Red b r1 d))
  92. balance_del color a (Node Black x l r) (Node Black b (Node color2 c l1 r1) d) = Node color b (Node Black a (Node Black x l r) (Node color2 c l1 r1)) d  
  93. balance_del color a x b = Node color a x b
  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 tree 10
  122.     --print $ tree1
  123.     --let tree2 = delete tree1 4
  124.     --print $ tree2
  125.     --let tree3 = delete tree2 13
  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