Advertisement
Bandorin

Untitled

Nov 26th, 2022
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
  3. {-# HLINT ignore "Use camelCase" #-}
  4. data BtreeFloat = Nil | Node Float BtreeFloat BtreeFloat
  5.   deriving (Eq, Show, Ord)
  6.  
  7. insert :: BtreeFloat -> Float -> BtreeFloat
  8. insert Nil flt = Node flt Nil Nil
  9. insert (Node val left right) flt
  10.   |flt < val = Node val (insert left flt)  right
  11.   |otherwise = Node val left (insert right flt)
  12.  
  13. depth :: BtreeFloat -> Int
  14. depth Nil = 0
  15. depth (Node val left right)
  16.   |depth left > depth right = 1 + depth left
  17.   |depth left < depth right = 1 + depth right
  18.   |depth left == depth right = 1 + depth right
  19.   |otherwise = 1 + depth right
  20.  
  21. max_node :: BtreeFloat -> Float
  22. max_node Nil = 0
  23. max_node (Node val Nil Nil) = val
  24. max_node (Node  val  left right) = max_node right
  25.  
  26. path_diff :: BtreeFloat -> Int
  27. path_diff Nil = depth Nil - shortest_path Nil
  28. path_diff (Node left val right) = depth (Node left val right) - shortest_path (Node left val right)
  29.  
  30. shortest_path :: BtreeFloat -> Int
  31. shortest_path Nil = 1
  32. shortest_path (Node val left right)
  33.   |shortest_path left > shortest_path right = 1 + shortest_path right
  34.   |shortest_path left < shortest_path right = 1 + shortest_path left
  35.   |shortest_path left == shortest_path right = 1 + shortest_path left
  36.   |otherwise = 1 + shortest_path left
  37.  
  38. tree :: BtreeFloat
  39. tree = Node 1 (Node 2 (Node 3 (Node 4 Nil Nil) (Node 5 (Node 6 Nil Nil) Nil)) Nil) (Node 7 Nil Nil)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement