Re: hacker challenge - traverse a list



On Feb 29, 12:56 pm, c...@xxxxxxxx (Richard Harter) wrote:
Here is a little challenge - print the contents of a binary tree
in order only using O(1) additional memory. In other words you
can't use recursion or simulate it with a stack. As far as I
know you can't do this without some kind of hack. Also AFAIK
there is no way to do this that should ever be used in real code.
Rules? We don't need no steenking rules! Use whatever language
you like, though I suppose this is a natural for C based
languages. Tag bit solutions (i.e. mark a link as visited when
we go down it and unmark it when we come back up) don't count
because (a) everyone thinks of it, and (b) it (debatably) uses
O(log n) memory. Platform dependencies are okay - this a hack,
after all.

I think this was done 25 years ago:

G. Lindstrom. Scanning list structures without stacks or tag bits.
Information Processing
Letters, 2(2):47-51, June 1973.

Even if you use the classical Deutsch-Schorr-Waite algorithm, which
needs 2 tag bits per node, you can steal them from the low order bits
of the child pointers by insisting that nodes are allocated on 2-byte
boundaries, which most modern systems already do...

.



Relevant Pages

  • Re: Is it possible to use the value of the PROGRAM ID within the source code?
    ... > Now you claim the language's 'hiding' features make the stack invisible to ... EXIT, RETN, and the setting of the RCW on ENTR are independent of language. ... Moreover, precisely because memory *is* dynamically allocated, there is no ... any Unisys MCP customer is welcome to file a New Feature Suggestion ...
    (comp.lang.cobol)
  • Re: hacker challenge - traverse a list
    ... in order only using Oadditional memory. ... can't use recursion or simulate it with a stack. ... Scanning list structures without stacks or tag bits. ... of the child pointers by insisting that nodes are allocated on 2-byte ...
    (comp.programming)
  • Re: hacker challenge - traverse a list
    ... in order only using Oadditional memory. ... can't use recursion or simulate it with a stack. ...  We don't need no steenking rules! ...  Use whatever language ...
    (comp.programming)
  • Re: pop quiz for Forth novices
    ... the memory allocation words that Forth provides. ... That doesn't require an addition to the language ... least one C implementation that didn't use a stack. ... Many C programs have their own memory allocator. ...
    (comp.lang.forth)
  • Re: Class Instances
    ... type "newClass" on the stack. ... No - its value is unassigned, at least as far as the C# language is ... contents of the memory until it's been definitely assigned anyway. ... But only, so that the debugger ...
    (microsoft.public.dotnet.languages.csharp)