Pabl0o0

Untitled

Nov 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. module T =
  2. struct
  3. type 'a bst_t =
  4. | Leaf
  5. | Node of 'a bst_t * 'a * 'a bst_t (* Node (left, key, right) *)
  6. (* a node may also carry a data associated with the key.
  7. It is omitted here for neater demonstration *)
  8.  
  9. let create () = Leaf
  10.  
  11. let rec push x = function
  12. | Leaf -> Node (Leaf, x, Leaf) (* Leaf is a STOP *)
  13. | Node (left, k, right) ->
  14. if x < k then Node (push x left, k, right) (* if x is smaller, go left *)
  15. else Node (left, k, push x right) (* otherwise, go right *)
  16.  
  17. let rec find x = function
  18. | Leaf -> false
  19. | Node (left, k, right) ->
  20. if x < k then find x left
  21. else if x = k then true
  22. else find x right
  23.  
  24.  
  25. end;;
  26.  
  27. T.create()
  28. T.push(5)
  29. T.push(3)
  30. T.push(1)
  31. T.push(4)
  32. T.push(7)
  33.  
  34. T.find(3)
Advertisement
Add Comment
Please, Sign In to add comment