You haven't really given enough detail to provide a specific answer so
here's a generic one...

Let's say you want to get the power of 2 to some number N and you want to do
this recursively. Yes, I know that there is a PWR() function but this is
just for illustration.

First we need a program to set things up:

PROGRAM CALL.RECUR
CRT 'Enter a number >= 1':
INPUT N
IF NUM(N) ELSE STOP
IF N < 1 THEN STOP
RESULT = 1
CALL RECUR(N, RESULT)
CRT RESULT

Now let's recursively CALL the subroutine N times:

SUBROUTINE RECUR(N, RESULT)
* Recursive power of 2
IF N THEN
  RESULT *= 2
  N -= 1
  CALL RECUR(N, RESULT)
END
RETURN

You'll notice that there is a terminating condition that allows the 'stack'
to unwind. This is very important otherwise you will end up aborting with a
stack overflow error.

Here's another example that uses COMMON instead of subroutine arguments to
keep track of things. Let's say we want to traverse a tree in 'postorder'
(1. traverse the left subtree, 2. traverse the right subtree, 3. visit the
root). A simple graphic example would be (you need a fixed size font to see
this accurately):

            1
           / \
          2   3
         / \ / \
         4 5 6 7

such that the nodes would be output in the following order:

    4,5,2,6,7,3,1

The data structure for this would be:

    ID: Node ID
    <1> left link ] right link

where ']' is a VM ( 0xFD ).

Although this is a binary tree, the algorithm works for an m-way tree with
any number of subtrees, ie

    ID: Node ID
    <1> link1 ] link2 ] link3 ] ... ] linkX

and is also not restricted to 'balanced' trees where all leaves must be a
the same level.

SUBROUTINE VISIT (ROOT)
COMMON TREE, LEVEL
READ LINKS FROM TREE, ROOT THEN
    LINK.CNT = DCOUNT(LINKS,@VM)
    LEVEL += 1
    FOR VAL = 1 TO LINK.CNT
        CALL VISIT(LINKS<1,VAL>)
    NEXT VAL
    LEVEL -= 1
    CRT "Root = ":ROOT:" at level ":LEVEL
END
RETURN

We need a program to start things off:

COMMON TREE, LEVEL
OPEN "TREE" TO TREE ELSE STOP
LEVEL = 0
ID = 1; * Set the root node
CALL VISIT (ID)

This would produce the following output:

Root = 4 at level 2
Root = 5 at level 2
Root = 2 at level 1
Root = 6 at level 2
Root = 7 at level 2
Root = 3 at level 1
Root = 1 at level 0

To change this to do a 'preorder' traversal, simply move the 'CRT' line just
before the 'LEVEL += 1' line. The output produced would then be:

Root = 1 at level 0
Root = 2 at level 1
Root = 4 at level 2
Root = 5 at level 2
Root = 3 at level 1
Root = 6 at level 2
Root = 7 at level 2

'Inorder' traversal is a bit more complex and I won't go into it here.

Be aware that jBASE has a 'stack' limit as to the number of levels you can
recursively call and I believe the limit is platform dependent.

Performance considerations aside, recursion is generally good for when you
don't know how many levels are involved.

Dan

"The work will teach you how to do it." -- Estonian Proverb


On Tue, Mar 3, 2009 at 2:41 AM, Dhaya <[email protected]> wrote:

>
> Hi
>
>  I am using jbase 4.1 release with T24. I have a query regarding the
> usage of 'recursive return" statement
>  in Jbase programming language. I understood we can recursive return
> to come out of subroutine to calling program. I have a requirement
> where when  the recursive return is executed, program control should
> not come out
> the subroutine used. It is that is there any way to code such as
>
>  PROGRAM.ABORT:
>
>    RETURN TO (PROGRAM.ABORT - 1)
>
> Because, i want this recursivee return to be executed 1 level down.
> Can i use (PROGRAM.ABORT - 1) in jbasic
> or Is there any alternate way to use.
>
> Thanks
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
Please read the posting guidelines at: 
http://groups.google.com/group/jBASE/web/Posting%20Guidelines

IMPORTANT: Type T24: at the start of the subject line for questions specific to 
Globus/T24

To post, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/jBASE?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to