我正在binary search tree
OCaml 中构建操作。
type ('a, 'b) bst =
| Node of 'a * 'b * ('a, 'b) bst * ('a, 'b) bst
| Leaf;;
let rec insert k v = function
| Leaf -> Node (k, v, Leaf, Leaf)
| Node (k', v', left, right) ->
if k < k' then Node (k', v', insert k v left, right)
else if k = k' then Node (k, v, left, right)
else Node (k', v', left, insert k v right);;
let rec delete k = function
| Leaf -> Leaf
| Node (k', v, l, r) as p ->
if k < k' then Node (k', v, (delete k l),r)
else if k > k' then Node (k', v, l, (delete k r))
else
match (l, r) with
| (Leaf, Leaf) -> Leaf
| (l, Leaf) -> l
| (Leaf, r) -> r
| (_, _) ->
let Node (km, vm, _, _) = max l in
Node (km, vm, delete km l, Leaf)
谁能告诉我我的deletion
代码是否足够好或有任何改进?