如何在OCaml中实现map.ml中的平衡bst?

如何在OCaml中实现map.ml中的平衡bst?,ocaml,binary-search-tree,Ocaml,Binary Search Tree,我刚刚在OCaml中查看了map.ml的源代码: 看来平衡不是一棵红黑树,而且比这简单得多 OCaml中map.ml中平衡中的灵魂是什么?set.ml和map.ml使用树 set.ml和map.ml使用树 let bal l x d r = let hl = match l with Empty -> 0 | Node(_,_,_,_,h) -> h in let hr = match r with Empty -> 0 | Node(_,_,_,_,h

我刚刚在OCaml中查看了
map.ml
的源代码:

看来平衡不是一棵红黑树,而且比这简单得多


OCaml中
map.ml
平衡
中的
灵魂是什么?

set.ml
map.ml
使用树

set.ml
map.ml
使用树

let bal l x d r =
      let hl = match l with Empty -> 0 | Node(_,_,_,_,h) -> h in
      let hr = match r with Empty -> 0 | Node(_,_,_,_,h) -> h in
      if hl > hr + 2 then begin
        match l with
          Empty -> invalid_arg "Map.bal"
        | Node(ll, lv, ld, lr, _) ->
            if height ll >= height lr then
              create ll lv ld (create lr x d r)
            else begin
              match lr with
                Empty -> invalid_arg "Map.bal"
              | Node(lrl, lrv, lrd, lrr, _)->
                  create (create ll lv ld lrl) lrv lrd (create lrr x d r)
            end
      end else if hr > hl + 2 then begin
        match r with
          Empty -> invalid_arg "Map.bal"
        | Node(rl, rv, rd, rr, _) ->
            if height rr >= height rl then
              create (create l x d rl) rv rd rr
            else begin
              match rl with
                Empty -> invalid_arg "Map.bal"
              | Node(rll, rlv, rld, rlr, _) ->
                  create (create l x d rll) rlv rld (create rlr rv rd rr)
            end
      end else
        Node(l, x, d, r, (if hl >= hr then hl + 1 else hr + 1))