Results 1 to 10 of 10
  1. #1
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126

    Tree traversing with recursive code ???

    I have MANUALLY made a family tree i Excel like this:

    Click image for larger version. 

Name:	tree.jpg 
Views:	21 
Size:	51.3 KB 
ID:	32102

    Never mind you can't see the details - you can see a sort of columns representing generations.

    The problem is to find of how many generations I need to have and which generation each person
    should belong to.

    The structure behind the tree is this table:

    Click image for larger version. 

Name:	table.jpg 
Views:	20 
Size:	98.6 KB 
ID:	32103

    Where BarnID is an ID for a person and F1, F2 ... is id's for the parents of that person.
    Sometimes a person has 1, 2 or 3 or more parents.
    If a person has no parents he is NOT listed in the table at all.



    I have this recursive code to traverse the tree from right to left starting with an Id.

    Code:
            
    Function findG(tb As DAO.Recordset, Id As Long) As Integer
        ' Using RECURSIVE call in this function
        ' If Id is not found -1 is returned
        Dim F As Long, n As Integer
        Dim G As Integer
        Dim bm As Variant
        G = -1
        With tb
            .Index = "PrimaryKey"
            .Seek "=", Id
            If .NoMatch = False Then
                bm = .Bookmark
                F = 1
                Do
                    Id = .Fields("F" & F) ' now take THIS parentId
                    n = findG(tb, Id)
                    G = G + 1
                    If n > G Then G = n
                    F = F + 1
                    .Bookmark = bm
                Loop Until .Fields("F" & F) = 0
            End If
            findG = G
        End With
    End Function
    The code is working fine BUT it's returning WRONG generation numbers !

    Take the person in the upper right corner - the code runs through all the supposed
    branches it should - including the long one which is defining the generation
    number - the upper right person should have the generation number 7.

    The code is returning 5 because it runs through a "shorter" branch as the
    last run !

    I know there is much more to do for EACH person to have the rcorrect generation
    number but in the first place I need to find the "longest path".

    How can I change the code so it can remember the LONGEST PATH through
    the anchesters ?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I suggest you post a table rather than a jpg of a table.
    It would also help readers if you showed the code you are working with; the steps to reproduce what you have and a clear sample of what you want to achieve based on the sample data.

    Readers are not familiar with your data.

    ?? Please explain 3 parents.
    There is no BarnID 52???
    Does BarnID 97 have only 1 parent???

  3. #3
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    3 parents .... biological and social mother/father ... maybe merried several times ;-))

    I haven't got the data on many of the persons yet ... therefore some parents are missing

    Person 52 is not yet registred with parent.

    The code will run directly on the table in the immidiate window ... if I only could upload the table

    I tried to upload the table as a TXT-file ... but how ?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Upload file same way you have in other threads such as https://www.accessforums.net/showthread.php?t=68958
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Do you have test data to get actual results??
    I look at BarnID 26 and see f1(52) f2 (53) f3 (107)

    but there is no BarnId 52 in the table???
    How is one suppose to read and interpret these f1/f2/f3... values?

  6. #6
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    3 parents .... biological and social mother/father ... maybe merried several times ;-))

    I haven't got the data on many of the persons yet ... therefore some parents are missing

    I tried to upload the table as a TXT-file ... but how ?

  7. #7
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    Here are the table as TXT attached

    The last column is the WRONG calculated generation numbers
    Attached Files Attached Files

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I am unsure how to call your function. I have loaded your data into a table.
    Please provide instructions on procedure for setting up to use your function.

  9. #9
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    Here is another function to open the table and keep record to minimize the overhead for opening tables.

    Code:
    Function GetTable(Name As String, _
                      Optional Index As String) As DAO.Recordset
        ' Maintains a collection of open tables (mcolTables) in order to avoid the
        ' overhead in re-opening the same table at each function call.
        ' is the table in the collection already?
        For Each GetTable In mcolTables
            If GetTable.Name = Name Then GetTable.Index = Index: Exit Function
        Next
        ' if not, open the table, and add it to the collection
        Set GetTable = CurrentDb(Name).OpenRecordset
        GetTable.Index = Index
        mcolTables.add GetTable, GetTable.Name
    End Function
    Set up the table with primary key on the first field and give it a name MYTABLE.

    Insert the 2 functions and compile.

    In the immidiate window you can now type

    ? findG(GetTable("MYTABLE", "PrimaryKey"),26)

    and it will return 4 as the generation number - and it's correct ... but by chance !

    Try 97 which should be 7, but it returns 5 because it CAN'T remember THE LONGEST PATH
    through the tree - that's my original question: What's wrong with my recursive function
    since it CAN'T remember that path ?

    Be aware that it's very, very frustrating to step-analyze recursive code BECAUSE it's recursive ;-))

  10. #10
    ksor's Avatar
    ksor is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Feb 2017
    Posts
    126
    Oh, I think I have an error in the TABLE .... Shit !

    What I MANUALLY made up in the tree structure is NOT mirrored in the TABLE data - that's why !

    So sorry for taken your time !

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 01-06-2018, 12:02 PM
  2. Is a recursive query possible?
    By nigelbloomy in forum Queries
    Replies: 3
    Last Post: 09-10-2015, 04:04 PM
  3. Replies: 6
    Last Post: 05-27-2012, 01:39 PM
  4. Recursive Report!
    By Rawb in forum Reports
    Replies: 7
    Last Post: 01-26-2011, 04:00 PM
  5. Recursive Tables!
    By Rawb in forum Database Design
    Replies: 1
    Last Post: 01-26-2011, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums