caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: caml-list@pauillac.inria.fr
Cc: Richard Jones <rich@annexia.org>, Brian Hurt <bhurt@spnz.org>
Subject: Re: [Caml-list] Re: Set and Map question
Date: Mon, 20 Sep 2004 14:54:36 +0200	[thread overview]
Message-ID: <16718.54028.37113.319426@gargle.gargle.HOWL> (raw)
In-Reply-To: <Pine.GSO.4.61.0409200836210.12686@slinky.cs.nyu.edu>


Igor Pechtchanski writes:
 > On Mon, 20 Sep 2004, Richard Jones wrote:
 > 
 > > On Sun, Sep 19, 2004 at 11:02:44PM -0500, Brian Hurt wrote:
 > > > The code is actually fairly easy.  Take the length of the list.  Subtract
 > > > one for the root node.  The first (n-1)/2 elements are in the left hand
 > > > subtree, the last n-1-((n-1)/2) elements are in the right subtree.
 > >
 > > Wouldn't you have to iterate over the list when implementing this?
 > > What I mean to say is that this would work if you had a pre-sorted
 > > Array, but not a linked List.  ?
 > 
 > Did the question mention anything about the space used by the algorithm?
 > If one is allowed O(n) temp space, then simply converting the sorted
 > linked List into a temp array as the first step will keep the algorithm
 > O(n) (constant factors aside).  One would need a constant-time-access data

There is no need converting the list into an array. Once the length is
computed (with a single traversal of the list), it is possible to
build the tree with only another traversal of the list. Here is for
instance how to build a red-black tree from a (reverse) sorted list of
elements:

===========================================================================
  (*s Building a red-black tree from a sorted list in reverse order.
      The result is a complete binary tree, where all nodes are black, 
      except the bottom line which is red.  *)

  let log2 n = truncate (log (float n) /. log 2.)

  let of_list sl = 
    let rec build sl n k =
      if k = 0 then
	if n = 0 then 
	  Empty, sl 
	else match sl with
	  | [] -> 
	      assert false
	  | x :: sl  -> 
	      Red (Empty, x, Empty), sl
      else
	let n' = (n - 1) / 2 in
	match build sl n' (k - 1) with
	  | _, [] -> 
	      assert false
	  | l, x :: sl -> 
	      let r, sl = build sl (n - n' - 1) (k - 1) in
	      Black (r, x, l), sl
    in
    let n = List.length sl in
    fst (build sl n (log2 n))
===========================================================================

The key idea is to return the tree together with the list of unused
elements in the list. 

regards,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2004-09-20 12:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-20  2:37 [Caml-list] " Seth Fogarty
2004-09-20  3:14 ` [Caml-list] " Seth Fogarty
2004-09-20  3:56   ` skaller
2004-09-20  4:18     ` Seth Fogarty
2004-09-20  6:43       ` skaller
2004-09-20  8:32       ` Diego Olivier Fernandez Pons
2004-09-20  4:02   ` Brian Hurt
2004-09-20  8:17     ` Richard Jones
2004-09-20 12:41       ` Igor Pechtchanski
2004-09-20 12:54         ` Jean-Christophe Filliatre [this message]

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=16718.54028.37113.319426@gargle.gargle.HOWL \
    --to=jean-christophe.filliatre@lri.fr \
    --cc=bhurt@spnz.org \
    --cc=caml-list@pauillac.inria.fr \
    --cc=rich@annexia.org \
    /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).