Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- type Tree =
- |Empty
- |Node of int *Tree *Tree
- let rec insert (root:Tree) x =
- match root with
- |Empty -> Node(x,Empty,Empty)
- |Node(value,left,right) -> if x < value then Node(value,insert left x, right)
- else Node(value,left,insert right x)
- let rec show root h =
- match root with
- |Node(value,left,right) ->
- show right (h+1)
- for i in 1 .. h do
- Console.Write(" ")
- Console.WriteLine(value)
- Console.WriteLine()
- show left (h+1)
- |Empty -> Console.Write("")
- let rec getEvenNumList (root:Tree) =
- match root with
- | Node(value,left,right) -> (fun v m -> if v%2=0 then v::m else m) value (getEvenNumList right) @ (getEvenNumList left)
- | Empty -> []
- [<EntryPoint>]
- let main argv =
- let r = new Random()
- let L = [ for i in 1..6 do yield r.Next(10) ]
- let list_to_tree L = List.fold (fun root x -> insert root x) Empty L
- let root = list_to_tree L
- let list = getEvenNumList root
- show root 0
- printf "%A" list
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement