Advertisement
noctual

Задание 7

Jun 13th, 2021
1,745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.11 KB | None | 0 0
  1. open System
  2.  
  3. type Tree =
  4.     |Empty
  5.     |Node of int *Tree *Tree
  6.  
  7. let rec insert (root:Tree) x =
  8.     match root with
  9.         |Empty -> Node(x,Empty,Empty)
  10.         |Node(value,left,right) -> if x < value then Node(value,insert left x, right)
  11.                                                 else Node(value,left,insert right x)
  12. let rec show root h =
  13.     match root with
  14.     |Node(value,left,right) ->
  15.         show right (h+1)
  16.         for i in 1 .. h do
  17.             Console.Write("    ")
  18.         Console.WriteLine(value)
  19.         Console.WriteLine()
  20.         show left (h+1)
  21.     |Empty -> Console.Write("")
  22.  
  23. let rec getEvenNumList (root:Tree) =
  24.     match root with
  25.         | Node(value,left,right) -> (fun v m -> if v%2=0 then v::m else m) value (getEvenNumList right) @ (getEvenNumList left)
  26.         | Empty -> []
  27.  
  28. [<EntryPoint>]
  29. let main argv =
  30.     let r = new Random()
  31.     let L = [ for i in 1..6 do yield r.Next(10) ]
  32.     let list_to_tree L = List.fold (fun root x -> insert root x) Empty L
  33.     let root = list_to_tree L
  34.     let list = getEvenNumList root
  35.     show root 0
  36.     printf "%A" list
  37.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement