Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module BinaryTree =
- type Tree<'T when 'T : comparison> =
- | Empty
- | Leaf of 'T
- | Node of Tree<'T> * 'T * Tree<'T>
- let leaf value =
- let l = Leaf(value)
- l
- let buildTree subT1 subT2 value =
- let tree = Node(subT1, value, subT2)
- tree
- let isLeaf item =
- match item with
- | Empty -> false
- | Leaf -> true
- | Node -> false
- let head tree =
- match tree with
- | Empty -> failwith "Empty tree"
- | Leaf(i) -> i
- | Node(l, v, r) -> v
- let left tree =
- match tree with
- | Empty -> failwith "Empty tree"
- | Leaf -> failwith "Argument can't be a leaf"
- | Node(l, v, r) -> l
- let right tree =
- match tree with
- | Empty -> failwith "Empty tree"
- | Leaf -> failwith "Argument can't be a leaf"
- | Node(l, v, r) -> r
- let empty =
- let t = Empty
- t
- let isEmpty tree =
- match tree with
- | Empty -> true
- | _ -> false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement