我创造了一棵树
type 'a tree = {
mutable cont: 'a;
mutable left: 'a bin_tree;
mutable right: 'a bin_tree
}
and 'a bin_tree =
Empty
| Node of 'a tree;;
我正在努力处理一些简单的功能,比如
- 插入元素(到propper子树,没有重复)
- 合并两棵二叉树
我用谷歌搜索了我的问题,但我经常遇到错误。例如:
let rec insert x tree =
match tree with
Empty -> Node(x, Empty, Empty)
| Node(y, left, right) ->
if x <= y then Node(y, insert x left, right)
else Node(y, left, insert x right)
或者如果我尝试:
let rec insert x = function
Empty -> Node(Empty, x, Empty)
| Node(lb, r, rb) -> if x < r then Node(insert x lb, r, rb)
else Node{lb; r; insert x rb} ;;
我不断收到语法错误。