Advertisement
Guest User

Joel

a guest
Mar 8th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.05 KB | None | 0 0
  1. module BinaryTree =
  2.     type Tree<'T when 'T : comparison> =
  3.         | Empty
  4.         | Leaf of 'T
  5.        | Node of Tree<'T> * 'T * Tree<'T>
  6.  
  7.     let leaf value =
  8.         let l = Leaf(value)
  9.         l
  10.  
  11.     let buildTree subT1 subT2 value =
  12.         let tree = Node(subT1, value, subT2)
  13.         tree
  14.  
  15.     let isLeaf item =
  16.         match item with
  17.         | Empty -> false
  18.         | Leaf -> true
  19.         | Node -> false
  20.  
  21.     let head tree =
  22.         match tree with
  23.         | Empty -> failwith "Empty tree"
  24.         | Leaf(i) -> i
  25.         | Node(l, v, r) -> v
  26.  
  27.     let left tree =
  28.         match tree with
  29.         | Empty -> failwith "Empty tree"
  30.         | Leaf -> failwith "Argument can't be a leaf"
  31.         | Node(l, v, r) -> l
  32.  
  33.     let right tree =
  34.         match tree with
  35.         | Empty -> failwith "Empty tree"
  36.         | Leaf -> failwith "Argument can't be a leaf"
  37.         | Node(l, v, r) -> r
  38.  
  39.     let empty =
  40.         let t = Empty
  41.         t
  42.  
  43.     let isEmpty tree =
  44.         match tree with
  45.         | Empty -> true
  46.         | _ -> false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement