Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Trees where
  2.  
  3.  
  4.  
  5. --import Prelude(Int(..), Show(..))
  6.  
  7.  
  8.  
  9. data Nat = Zero | Succ Nat
  10.  
  11.     deriving (Show, Eq, Ord)
  12.  
  13.  
  14.  
  15. data Tree a = Leaf a | Branch (Tree a) a (Tree a)
  16.  
  17.     deriving Show
  18.  
  19.  
  20.  
  21. data TreeTwo b = Leaft b | Brancht (Tree b) (Tree b)
  22.  
  23.     deriving Show
  24.  
  25.  
  26.  
  27. treeOne = Branch (Branch (Leaf 14) 20 (Leaf 21)) 25 (Branch (Leaf 29) 37 (Branch (Leaf 43) 50 (Leaf 52)))
  28.  
  29. treeTwo = Brancht (Brancht (Leaft 14) (Leaft 21)) (Brancht (Leaft 29) (Brancht (Leaft 43) (Leaft 52)))
  30.  
  31.  
  32.  
  33. --переворот
  34.  
  35. reverseTree :: Tree a -> Tree a
  36.  
  37. reverseTree (Branch left value right) = (Branch (reverseTree right) value (reverseTree left))
  38.  
  39. reverseTree (Leaf value) = Leaf value
  40.  
  41.  
  42.  
  43. --глубина
  44.  
  45. depth :: Tree a -> Nat
  46.  
  47. depth (Leaf a) = Succ Zero
  48.  
  49. depth (Branch left value right) = Succ(max (depth left) (depth right))
  50.  
  51.  
  52.  
  53. --список
  54.  
  55. leaves :: Tree a -> [a]
  56.  
  57. leaves (Leaf a) = [a]
  58.  
  59. leaves (Branch left a right) = leaves left ++ leaves right
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement