Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 'T tree =
- | Node of 'T * ('T tree list)
- | Leaf of 'T
- type 'T bintree =
- | BinNode of 'T * 'T bintree * 'T bintree
- | BinLeaf of 'T
- // ----- SAMPLES -----
- let rec printTree(t: 'T tree) =
- match t with
- | Node(v, ls) -> printfn "Node"; ls |> List.iter printTree
- | Leaf(v) -> printfn "Leaf"
- let sampleTree = Node(3, [Leaf(4); Leaf(6)])
- //printTree(sampleTree)
- //Node
- //Leaf
- //Leaf
- let sampleBinTree = BinNode(3, BinLeaf(4), BinLeaf(6))
- let rec printBinTree(t: 'T bintree) =
- match t with
- | BinNode(v, l, r) -> printfn "BinNode"; printBinTree l; printBinTree r
- | BinLeaf(v) -> printfn "BinLeaf"
- //printBinTree(sampleBinTree)
- // BinNode
- // BinLeaf
- // BinLeaf
- // ----- SOLUTION(ВАРИАНТ 14) -----
- let generateBinTree =
- let rnd = System.Random()
- let rec gen n =
- if (rnd.Next(100) <= n) then
- (BinNode(0, gen (n - 20), gen (n - 20)))
- else
- (BinLeaf(0))
- gen 100
- let rec solution(t: 'T bintree) =
- match t with
- | BinNode(v, l, r) -> (solution l + solution r)
- | BinLeaf(v) -> 1
- let generatedBinTree = generateBinTree
- printBinTree(generatedBinTree)
- printfn "%d" (solution(generateBinTree))
- System.Console.ReadLine() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement