[Date Prev][Date Next][Thread Prev][Thread Next]
[Author Index] [Date Index] [Thread Index]
[SQR-USERS Info] [SQRUG Home Page]

Re: Finding Children in a nonbinary tree-- a recursive puzzle?



Thanks, Tony!!!!!!!!!!!!!

Your tree algorithm showed me how to solve the puzzle in an iterative
fashion. Here it is (names are changed to protect the innocent............)

!***************************************************************************
*
!
! PROCEDURE MAIN:
!
!***************************************************************************
*

BEGIN-PROCEDURE MAIN

move 240278 to #serialno !171054
show 'Original ' #serialno edit {serialnoedit}
DO FindAncestorA
! You have to start at the top of the tree to get all the children
show 'ancestor is ' #oldest edit {serialnoedit}
let #relative_max= {max_relative}-1
move 0 to #thisrow
move 0 to #counter

! Set up first row of the array
move 0 to #relative_start
put #relative_start #oldest into relative (0) parent child
! First row reads: parent:0 child:192071(or whatever number you entered)
do FillUpArray
End-Procedure MAIN
!***************************************************************************
*
!
! PROCEDURE FillUpArray: Fills array with parents and children
! Parameters: none
!
!***************************************************************************
*
BEGIN-PROCEDURE FillUpArray

WHILE #thisrow <= #counter
      and #thisrow < {max_relative}

   let #lastchild = relative.child(#thisrow)
   do populate-array
   add 1 to #thisrow     !If populate-array fails, #thisrow will become >
#counter

END-WHILE

END-PROCEDURE FillUpArray
!***************************************************************************
*
!
! PROCEDURE Populate-Array:
!***************************************************************************
*
BEGIN-PROCEDURE Populate-Array
BEGIN-SELECT
serialno
  add 1 to #counter
  if #counter > {max_relative} -1
    exit-select
  end-if
  let relative.parent(#counter)=#lastchild
  let relative.child(#counter)=&serialno

FROM thetable m
WHERE m.relateid IN ( SELECT relateid
                          FROM thetable
                         WHERE serialno = #lastchild
                         AND code=0)  !only relations where #lastchild is
parent
AND code=1          !1=child, 0=parent
AND m.serialno <> #lastchild

ORDER BY serialno

END-SELECT

END-PROCEDURE Populate-Array
!***************************************************************************
*
!
! PROCEDURE PrintResults
!
!***************************************************************************
*
BEGIN-PROCEDURE PrintResults
move 1 to #I  !The first element of the array shows the oldest ancestor

WHILE #I <= #thisrow - 1         ! When you added 1 to #thisrow, it stopped
the loop
    let $parent= relative.parent(#I)
    let $child=relative.child(#i)
    show #I edit 9999 ' Parent:' $parent edit {serialnoedit}  ' child '
$child edit {serialnoedit}
    add 1 to #I
END-WHILE

END-PROCEDURE PrintResults


The key to your program was the WHILE loop.......I would have never thought
of that.

:) Kristin

> -----Original Message-----
> From: Tony DeLia [SMTP:tdelia@EROLS.COM]
> Sent: Tuesday, October 05, 1999 11:12 AM
> To:   Multiple recipients of list SQR-USERS
> Subject:      Re: Finding Children in a nonbinary tree-- a recursive
> puzzle?
>
> Hi Kristin,
>    I posted something similar on my website... http://www.sqrtools.com
> (OR http://www.sqrtools.com/tdsvc.htm for direct access to the page)
>
>