Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:36:04+00:00 2026-05-31T06:36:04+00:00

I have a hierarchical organization that is a tree where the node is a

  • 0

I have a hierarchical organization that is a tree where the node is a child if the parent sponsors the child. It seems I can traverse the tree with this code

def get_team(self, person, team):
    firstline = User.query(User.sponsor == person.key).fetch(99999999)
    if firstline:
        for person in firstline:
            team.append(person)
            newdownline = self.downline(person, team)        
    return team

Using the above I can get a user’s organization just by

downline=user.get_team(user, [])

But is there a more efficient way since I have to do this many times for a single request and that much recursion might be ineffecient? Or will the code be fine since it can traverse the tree correctly? In my first version I used three variables and I found I could rearrange the code to only two variables instead of this:

def downline(self, person, team, teamlist):
    firstline = User.query(User.sponsor == person.key).fetch(99999999)
    if firstline:
        for person in firstline:
            teamlist.append(person)
            newdownline = self.downline(person, team, teamlist)        
            team.append(newdownline)
    return teamlist 

I found that the teamlist variable wasn’t really needed so I removed it. The way I did it was first with one variable too many:

people = user.downline(user, [], [])

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:36:05+00:00Added an answer on May 31, 2026 at 6:36 am

    Yes, there is a more efficient way to do it, depending on your computational tradeoffs.

    You are currently doing a depth-first traversal of your tree, a perfectly fine approach. You could add some speed by caching results at the expense of some RAM usage, so:

    if person.id in downline_cache:
          team.append(downline_cache[person.id])
    else:
          downline_cache[person.id] = results
          team.append(results)
    

    If the tree is fairly small, you could just cache the whole thing upfront, once per thread. That takes more RAM than what you’re doing, but is much faster than doing a depth-first traversal every time you care what the results are. A lot depends on your usage patterns and the amount of data you’re storing.

    If you use a cache, you must make sure you have some way to deal with the underlying data changing, either with timeouts and ‘eventually correct’ type guarantees, or keeping track of when and how you must wipe the cache, or elements of the cache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchical structure like this: class Node { Node Parent; string Name;
I have some hierarchical data that I need display as a table, can I
I have a hierarchical data that goes like this: +----------------------+-------+ | name | depth
I have an issue. I have hierarchical XML data such as: <Tree> <Node Text=Stuff
I have this project that displays hierarchical data with huge amounts of text, and
I have a hierarchical JPA mapping that is several Classes deep. Something like this
I have a class that contains hierarchical data. I want to present this data
I have some hierarchical data that I need to display in a series of
I'm working on a project where I have some hierarchical data that I want
I have an hierarchical structure of classes like this public class Class1 { internal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.