首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在map.ml中实现OCaml中的平衡bst?

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

Stack Overflow用户
提问于 2013-08-02 12:54:41
回答 1查看 230关注 0票数 1

我刚刚看了一下map.ml在OCaml:https://github.com/MassD/ocaml/blob/master/stdlib/map.ml中的源代码

代码语言:javascript
复制
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))

似乎平衡并不是一个red-black-tree,而且比这简单得多。

soulbalancing中的map.ml在OCaml中是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-02 13:26:49

set.mlmap.ml使用AVL树。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18017419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档