caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Yoriyuki Yamagata <yoriyuki.y@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Balancing algorithm of Set/Map implementation
Date: Fri, 14 May 2010 17:13:40 +0200	[thread overview]
Message-ID: <87k4r6h5y3.fsf@frosties.localdomain> (raw)
In-Reply-To: <AANLkTinsY7jolgDDbHvhduK52-d7u21nVLJfPb5eNbUq@mail.gmail.com> (Yoriyuki Yamagata's message of "Fri, 14 May 2010 15:17:23 +0900")

Yoriyuki Yamagata <yoriyuki.y@gmail.com> writes:

> Hi, list.
>
> When I read the balancing function of stdlib's Set/Map several years ago, I
> thought I have understand how it works.  But now, I read it again and I'm less
> confident now.  Could someone answer my questions?  Here is the snippet of the
> code.
>
>  let bal l v 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 "Set.bal"
>         | Node(ll, lv, lr, _) ->
>             if height ll >= height lr then
>               create ll lv (create lr v r)
>             else begin
>               match lr with
>
>                 Empty -> invalid_arg "Set.bal"
>               | Node(lrl, lrv, lrr, _)->
>                   create (create ll lv lrl) lrv (create lrr v r)
>             end
>       end else if hr > hl + 2 then begin
>
>         match r with
>           Empty -> invalid_arg "Set.bal"
>         | Node(rl, rv, rr, _) ->
>             if height rr >= height rl then
>               create (create l v rl) rv rr
>             else begin
>
>               match rl with
>                 Empty -> invalid_arg "Set.bal"
>               | Node(rll, rlv, rlr, _) ->
>                   create (create l v rll) rlv (create rlr rv rr)
>             end
>
>       end else
>         Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
>
> I have two question.
>
>         | Node(ll, lv, lr, _) ->
>             if height ll >= height lr then
>               create ll lv (create lr v r)
>
>             else begin
>
> Is this code right?  If r is Empty and lr and ll are huge trees,
> doesn't it create a massively unbalanced tree?

If r is empty then lr and ll can not be huge. Otherwise the tree was
massively unbalance beforehand. The balancing prevents this from
hapening.

> Another question is that why OCaml implementation allows
> a balancing factor up to *2*, which is usually allowed only up to 1?

Probably avoids having to do 2 balancings in a single operation. Or
weighs the number of balancing done on average against a slightly
unbalanced tree, i.e. turns out to be faster to be more unbalanced in
practice.

> Maybe my question is naive one, but I would appreciate if your could comment it.
>
> Regards,

MfG
        Goswin


  parent reply	other threads:[~2010-05-14 15:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-14  6:17 Yoriyuki Yamagata
     [not found] ` <AANLkTikNr91FmeqOAXT-MOS0yDgH652WMvsg0a-WopKN@mail.gmail.com>
2010-05-14  8:09   ` Fwd: [Caml-list] " Julien Signoles
2010-05-14 13:02 ` Daniel Bünzli
2010-05-14 15:13 ` Goswin von Brederlow [this message]
2010-05-14 18:48 ` "Stanisław T. Findeisen"

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k4r6h5y3.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=yoriyuki.y@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).