Advertisement
Margoshinka

govno

Jun 7th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. open System
  2. type 't btree =
  3. Node of 't * 't btree * 't btree
  4. | Nil
  5. [<EntryPoint>]
  6. let main argv =
  7. let infix root left right = (left(); root(); right()) //порядок обхода
  8. let iterh trav f t = //обход: здесь trav - функция порядка обхода
  9. let rec tr t h =
  10.  
  11.  
  12. match t with
  13. Node (x,L,R) -> trav
  14. (fun () -> (f x h )) // обход корня
  15. (fun () -> tr L (h+1)) // обход левого поддерев
  16. (fun () -> tr R (h+1)); // обход правого поддерева
  17.  
  18. | Nil -> ()
  19. tr t 0
  20.  
  21.  
  22.  
  23.  
  24. let spaces n = List.fold (fun s _ -> s+" ") "" [0..n]
  25. let print_tree T = iterh infix (fun x h -> printfn "%s%A" (spaces (h+3))x) T
  26. let rec insert x t =
  27. match t with
  28. Nil -> Node(x,Nil,Nil)
  29. | Node(z,L,R) -> if x<z then Node(z,insert x L,R)
  30. else Node(z,L,insert x R)
  31. let L =
  32. [
  33. let r = new Random()
  34. printfn "Количество элементов?"
  35. let n = Convert.ToInt32(Console.ReadLine())
  36. for i in 1..n do
  37. yield r.Next(-15, 16)
  38. ]
  39. printfn "Исходный список %A" L
  40. let list_to_tree L = List.fold (fun t x -> insert x t) Nil L
  41.  
  42. let BT = list_to_tree L
  43.  
  44. print_tree BT
  45.  
  46. 0 // return an integer exit code
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement