threaded red-black tree

Hi, No one responded to my question several weeks ago about a purely functional implementation of a threaded, red-black tree. My message was sent about the same time as that flurry of silly emails about "how to respond to homework questions". Was my message not responded to because it was classified as a homework question? I assure you this is officework, not homework. I ended up porting Okasaki's red-black tree implementation; hacking it apart with a bunch of mutation for the threading of the list through the tree. However, I'm still missing a deletion function and I haven't been able to find a prototype (Okasaki's red-black tree module lacks delete). My study of the RB-tree deletion routine in CLR hasn't yet yielded an adaptation for a functional environment. Any advice would be much appreciated. Thanks, Lex -- Lex Stein http://www.eecs.harvard.edu/~stein/ stein@eecs.harvard.edu cell: 617-233-0246

On Sun, 2003-09-14 at 21:48, Lex Stein wrote:
Hi, No one responded to my question several weeks ago about a purely functional implementation of a threaded, red-black tree.
I'm not sure what you mean by "threaded". By simply ignoring that word, I come up with the following solution :-) There is a purely functional implementation of a red-black tree in the MetaPRL system (www.metaprl.org), written in OCaml. For the latest CVS version of this red-black tree code, go to http://cvs.metaprl.org:12000/cvsweb/metaprl/libmojave/stdlib/ (look at lm_map.ml and lm_set.ml). Carl Witty

Carl Witty writes: | On Sun, 2003-09-14 at 21:48, Lex Stein wrote: | > Hi, No one responded to my question several weeks ago about a purely | > functional implementation of a threaded, red-black tree. | | I'm not sure what you mean by "threaded". By simply ignoring that word, | I come up with the following solution :-) IIRC a threaded tree (in an imperative language) is one where 'unused' child pointers are made to point to the next (right child pointer) or previous (left child pointer) node in an in-order traversal. The pointers have to be flagged to show whether they're normal or threading. For example, if this tree R / \ L S \ M were threaded, the unused pointers would be pressed into service thus: L's left: still nil, because L is the least element M's left: thread to L (predecessor) M's right: thread to R (successor) S's left: thread to R (predecessor) S's right: still nil, because S is the greatest element A benefit of threading is that you can make an in-order traversal in constant space, because you don't have to remember the whole path from the root to your current position. You *could* translate it into a purely functional representation, along these lines data TRBT a = Empty | Node Colour (Ptr a) a (Ptr a) data Colour = Red | Black data Ptr a = Child (TRBT a) | Thread (TRBT a) but you'd have to do some tricky stuff http://haskell.org/hawiki/TyingTheKnot to deal with the circular nature of the data structure (e.g. in the above tree, R points to L, L points to M, and M points to R). Also note that every insert or delete is O(n) instead of O(log n), because *every* node in the old tree can see the part you change. I suggest you (Lex) either go imperative (with STRef or IORef) or do without threading, unless you're sure that you'll be doing many more lookups and traversals than inserts and deletes. Regards, Tom

this might help: http://www.cs.kent.ac.uk/people/staff/smk/redblack/Untyped.hs andrew Lex Stein said:
Hi, No one responded to my question several weeks ago about a purely functional implementation of a threaded, red-black tree. My message was sent about the same time as that flurry of silly emails about "how to respond to homework questions". Was my message not responded to because it was classified as a homework question? I assure you this is officework, not homework. I ended up porting Okasaki's red-black tree implementation; hacking it apart with a bunch of mutation for the threading of the list through the tree. However, I'm still missing a deletion function and I haven't been able to find a prototype (Okasaki's red-black tree module lacks delete). My study of the RB-tree deletion routine in CLR hasn't yet yielded an adaptation for a functional environment. Any advice would be much appreciated.
Thanks, Lex
-- Lex Stein http://www.eecs.harvard.edu/~stein/ stein@eecs.harvard.edu cell: 617-233-0246
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
andrew cooke
-
Carl Witty
-
Lex Stein
-
Tom Pledger