Advertisement
Guest User

lab2

a guest
Nov 25th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Lab2 where  
  2.  
  3. data BinaryTree = Empty | Node BinaryTree Integer BinaryTree
  4.  
  5. -- Создание пустого дерева
  6. emptyTree :: BinaryTree
  7. emptyTree = Empty
  8.  
  9. -- Добавления элемента
  10. insert :: BinaryTree -> Integer -> BinaryTree
  11. insert (Empty) a = Node Empty a Empty
  12. insert (Node left x right) a
  13.     | a > x = Node left x (insert right a)
  14.     | a < x = Node (insert left a) x right
  15.     | a == x = Node left x right
  16.  
  17. --Удаление элемента
  18. remove :: BinaryTree -> Integer -> BinaryTree
  19. remove (Empty) a = Empty
  20. remove (Node left x right) a
  21.     | a > x = Node left x (remove right a)
  22.     | a < x = Node (remove left a) x right
  23.     | a == x =
  24.         if isEmpty right
  25.         then left
  26.         else Node left leftmost right'
  27.            where
  28.                isEmpty Empty = True
  29.                isEmpty _ = False
  30.                (leftmost, right') = deleteleftmost right
  31.                     where
  32.                         deleteleftmost (Node Empty x right) = (x, right)
  33.                         deleteleftmost (Node left x right) = deleteleftmost left
  34.  
  35. --Поиск элемента
  36. containsElement :: BinaryTree -> Integer -> Bool
  37. containsElement (Empty) a  = False
  38. containsElement (Node left x right) a
  39.     | a == x = True
  40.     | a < x  = containsElement left a
  41.     | a > x  = containsElement right a
  42.  
  43. --Поиск в дереве наименьшего элемента, который больше или равен заданному
  44. nearestGE :: BinaryTree -> Integer -> Integer
  45. nearestGE Empty x = error "No such element"
  46. nearestGE (Node left x right) a
  47.     | a == x = a
  48.     | a > x = if isEmpty right then error "No such element" else nearestGE right a
  49.     | a < x = findMoreThenA'ButLessThenX left x a
  50.        where
  51.            isEmpty Empty = True
  52.            isEmpty _ = False
  53.            findMoreThenA'ButLessThenX (Node left b right) x a
  54.                 | b < a = a
  55.                 | b > a = findMoreThenA'ButLessThenX left a b
  56.  
  57. --Создание дерева из списка
  58. treeFromList :: [Integer] -> BinaryTree
  59. treeFromList [] = emptyTree
  60. treeFromList (h : t) = insert (treeFromList t) h
  61.  
  62. --Создания списка из дерева
  63. listFromTree :: BinaryTree -> [Integer]
  64. listFromTree Empty=[]
  65. listFromTree (Node left x right) = (listFromTree left ++ [x]) ++ listFromTree right
  66.  
  67. --Проверка
  68. test = listFromTree (emptyTree `insert` 1 `insert` 2 `insert` 4 `insert` 3 `remove`4 `insert` 9 `insert` 12 `insert` 14)
  69. a = treeFromList test
  70. b = nearestGE a 11
  71.  
  72. main = do
  73.    putStrLn(show test)
  74.    putStrLn(show b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement