Is there an elegant way to non-destructively step through a red black tree?

H. S. Teoh hsteoh at qfbox.info
Fri Mar 21 00:32:03 UTC 2025


On Thu, Mar 20, 2025 at 11:51:46PM +0000, WhatMeWorry via Digitalmars-d-learn wrote:
>     Is there an elegant way, like with foreach, to step through a red black
> tree non-destructively?  This destructive failure is the best I've been able
> to come up with.
> 
>     while(!rbt.empty)
>     {
>         Node e = rbt.front;
>         writeln("e = ", e);
>         rbt.removeFront;
>     }
> 
> 
> I don't suppose there are any gentle tutorials regarding the library
> containers?

Doesn't RedBlackTree have a member function or operator overload that
returns a (non-destructive) range over the container?  IIRC it should be
opSlice(), which means you should be able to do this:

	foreach (Node e; rbt[]) {
		... // whatever you need here
	}


T

-- 
Valentine's Day: an occasion for florists to reach into the wallets of nominal lovers in dire need of being reminded to profess their hypothetical love for their long-forgotten.


More information about the Digitalmars-d-learn mailing list