comp.lang.ada
 help / color / mirror / Atom feed
* Search trees
@ 2005-05-12  6:58 VBTricks.de.vu Webmaster
  2005-05-12 16:28 ` Robert A Duff
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: VBTricks.de.vu Webmaster @ 2005-05-12  6:58 UTC (permalink / raw)


Hello,

I'm currently working with search-trees, more exactly different methods 
of listings the items in the search-tree (postfix/postorder, 
infix/inorder, prefix/preorder).

Doing this recursively is no problem, but for an exercise I now need to 
implement these functions iteratively (meaning not recursively). The 
only hint I got was to save which children trees I have not walked 
through. But after four days of thinking I still don't get it.

That's my structure for the nodes of the tree:

    type node;
    type tree is access node;
    type node is record
       value : integer;
       left : tree;
       right : tree;
    end record;

and here a sample for a recursive infix-walkthrough:

procedure Infix(t: tree) is
begin
   if t /= null then
     Infix(t.left);
     Put(t.value);
     Infix(t.right);
   end if;
end;

What is the clue? How do I implement it iteratively? Please help me!


Thanks in advance,

Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12  6:58 Search trees VBTricks.de.vu Webmaster
@ 2005-05-12 16:28 ` Robert A Duff
  2005-05-12 17:51   ` VBTricks.de.vu Webmaster
  2005-05-13  2:00 ` Jeffrey Carter
  2005-05-14 14:57 ` VBTricks.de.vu Webmaster
  2 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2005-05-12 16:28 UTC (permalink / raw)


"VBTricks.de.vu Webmaster" <vbtricks@gmx.net> writes:

> Doing this recursively is no problem, but for an exercise I now need to
> implement these functions iteratively (meaning not recursively). The
> only hint I got was to save which children trees I have not walked
> through. But after four days of thinking I still don't get it.

Do you understand how recursion works internally?  
Whenever you call a procedure, the machine code pushes
some information on a stack (return address, local variables).
If you want to avoid recursion, you can simulate this process
by writing explicit stack-management code.

- Bob



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12 16:28 ` Robert A Duff
@ 2005-05-12 17:51   ` VBTricks.de.vu Webmaster
  2005-05-12 17:58     ` Robert A Duff
  0 siblings, 1 reply; 9+ messages in thread
From: VBTricks.de.vu Webmaster @ 2005-05-12 17:51 UTC (permalink / raw)


Hello Bob,

no I do not know how recursion works internally. But I think there must 
be another way. Can you help me?


Thanks in advance,

Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12 17:51   ` VBTricks.de.vu Webmaster
@ 2005-05-12 17:58     ` Robert A Duff
  2005-05-12 18:07       ` VBTricks.de.vu Webmaster
  0 siblings, 1 reply; 9+ messages in thread
From: Robert A Duff @ 2005-05-12 17:58 UTC (permalink / raw)


"VBTricks.de.vu Webmaster" <vbtricks@gmx.net> writes:

> no I do not know how recursion works internally. But I think there must
> be another way. Can you help me?

Use a stack to keep track of which tree nodes have not yet been fully
processed.

- Bob




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12 17:58     ` Robert A Duff
@ 2005-05-12 18:07       ` VBTricks.de.vu Webmaster
  2005-05-12 23:32         ` Georg Bauhaus
  0 siblings, 1 reply; 9+ messages in thread
From: VBTricks.de.vu Webmaster @ 2005-05-12 18:07 UTC (permalink / raw)


Hello Robert,

but how do I implement this? I have very little knowledge of Ada95 (at 
least none of stack programming).


Thanks in advance,

Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12 18:07       ` VBTricks.de.vu Webmaster
@ 2005-05-12 23:32         ` Georg Bauhaus
  2005-05-13  2:02           ` Jeffrey Carter
  0 siblings, 1 reply; 9+ messages in thread
From: Georg Bauhaus @ 2005-05-12 23:32 UTC (permalink / raw)


VBTricks.de.vu Webmaster wrote:
> Hello Robert,
> 
> but how do I implement this? I have very little knowledge of Ada95 (at 
> least none of stack programming).

Not sure what you mean by "stack programming",
but what may be meant here is that you write some helper
subprograms.  Calling them will move nodes onto a "stack" and move them
back when necessary when you simulate recursive Infix. "Stack" here is
something that you program, it doesn't yet exist in the system.

It might help to pay attention to the imaginary steps you take
while executing Infix in your head.
  How do you remember nodes when you follow a call of Infix from
within Infix? How many nodes will you have to remember befor Put is
called for the first time? Why do you have to remember nodes, anyway?
How long will they have to be remembered?

The way a stack package can be written is likely described in
every book about programming in Ada. Which one do you have?


-- Georg





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12  6:58 Search trees VBTricks.de.vu Webmaster
  2005-05-12 16:28 ` Robert A Duff
@ 2005-05-13  2:00 ` Jeffrey Carter
  2005-05-14 14:57 ` VBTricks.de.vu Webmaster
  2 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2005-05-13  2:00 UTC (permalink / raw)


VBTricks.de.vu Webmaster wrote:

> I'm currently working with search-trees, more exactly different methods 
> of listings the items in the search-tree (postfix/postorder, 
> infix/inorder, prefix/preorder).
> 
> Doing this recursively is no problem, but for an exercise I now need to 
> implement these functions iteratively (meaning not recursively). The 
> only hint I got was to save which children trees I have not walked 
> through. But after four days of thinking I still don't get it.

Sounds like homework to me.

One way to avoid recursion is to use a non-recursive searchable 
structure, such as a skip list.

-- 
Jeff Carter
"I would never want to belong to any club that
would have someone like me for a member."
Annie Hall
41



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12 23:32         ` Georg Bauhaus
@ 2005-05-13  2:02           ` Jeffrey Carter
  0 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2005-05-13  2:02 UTC (permalink / raw)


Georg Bauhaus wrote:

> Not sure what you mean by "stack programming",
> but what may be meant here is that you write some helper
> subprograms.  Calling them will move nodes onto a "stack" and move them
> back when necessary when you simulate recursive Infix. "Stack" here is
> something that you program, it doesn't yet exist in the system.

Unless he uses one of the fine libraries available for Ada, such as the 
PragmAda Reusable Components (PragmARC.Stack_Unbounded, for example). 
But that's probably not acceptable for homework.

-- 
Jeff Carter
"I would never want to belong to any club that
would have someone like me for a member."
Annie Hall
41



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Search trees
  2005-05-12  6:58 Search trees VBTricks.de.vu Webmaster
  2005-05-12 16:28 ` Robert A Duff
  2005-05-13  2:00 ` Jeffrey Carter
@ 2005-05-14 14:57 ` VBTricks.de.vu Webmaster
  2 siblings, 0 replies; 9+ messages in thread
From: VBTricks.de.vu Webmaster @ 2005-05-14 14:57 UTC (permalink / raw)


Hello again,

well, now I know what a stack is and I programmed my solution using a 
stack. Works perfectly thanks (only problem was to put out the nodes at 
the right position for infix-walkthrough).


But now it works,

Thanks,

Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-05-14 14:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-12  6:58 Search trees VBTricks.de.vu Webmaster
2005-05-12 16:28 ` Robert A Duff
2005-05-12 17:51   ` VBTricks.de.vu Webmaster
2005-05-12 17:58     ` Robert A Duff
2005-05-12 18:07       ` VBTricks.de.vu Webmaster
2005-05-12 23:32         ` Georg Bauhaus
2005-05-13  2:02           ` Jeffrey Carter
2005-05-13  2:00 ` Jeffrey Carter
2005-05-14 14:57 ` VBTricks.de.vu Webmaster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox