exercises.sml 715 B

12345678910111213141516171819202122232425262728293031323334353637
  1. type key = string
  2. datatype tree = LEAF | TREE of tree * key * tree
  3. val empty = LEAF
  4. fun insert (key, LEAF) = TREE(LEAF, key, LEAF)
  5. | insert (key, TREE(l,k,r)) =
  6. if key < k
  7. then TREE(insert(key,l),k,r)
  8. else if key > k
  9. then TREE(l,k,insert(key,r))
  10. else TREE(l,key,r)
  11. fun member key LEAF = false
  12. | member key (TREE(l,k,r)) =
  13. if key = k
  14. then true
  15. else (member key l) orelse (member key r)
  16. (* This is not exhaustive, and will fail if the tree
  17. was not created using `insert` *)
  18. fun fastermember key LEAF = false
  19. | fastermember key (TREE(l,k,r)) =
  20. if key = k
  21. then true
  22. else if key < k
  23. then fastermember key l
  24. else fastermember key r