Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- type 't btree =
- Node of 't * 't btree * 't btree
- | Nil
- [<EntryPoint>]
- let main argv =
- let infix root left right = (left(); root(); right()) //порядок обхода
- let iterh trav f t = //обход: здесь trav - функция порядка обхода
- let rec tr t h =
- match t with
- Node (x,L,R) -> trav
- (fun () -> (f x L R h)) // обход корня
- (fun () -> tr L (h+1)) // обход левого поддерев
- (fun () -> tr R (h+1)); // обход правого поддерева
- | Nil -> ()
- tr t 0
- let spaces n = List.fold (fun s _ -> s+" ") "" [0..n]
- let print_tree T = iterh infix (fun x L R h -> printfn "%s%A" (spaces (h+5))x) T
- let rec insert x t =
- match t with
- Nil -> Node(x,Nil,Nil)
- | Node(z,L,R) -> if x<z then Node(z,insert x L,R)
- else Node(z,L,insert x R)
- let L =
- [
- let r = new Random()
- printfn "Количество элементов?"
- //let n = Convert.ToInt32(Console.ReadLine())
- let n = 10
- for i in 1..n do
- yield r.Next(-15, 16)
- ]
- printfn "Исходный список %A" L
- let list_to_tree L = List.fold (fun t x -> insert x t) Nil L
- let BT = list_to_tree L
- print_tree BT
- let print_ans T = iterh infix (fun x L R h -> if L<>Nil&&R<>Nil then printfn "%A " x) T
- printfn "Ответ: "
- print_ans BT
- System.Console.ReadLine() |> ignore
- 0
- // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement