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 5997027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:12:16+00:00 2026-05-23T00:12:16+00:00

I have a hard time with tree traversal, and so avoid it like the

  • 0

I have a hard time with tree traversal, and so avoid it like the plague… normally.

I have a class that’s sort-of (slightly simplified version here, but functionally the same) like:

class Branch(object):
    def __init__(self, title, parent=None):
        self.title = title
        self.parent = parent

I have a dictionary of a bunch of Branch instances, the titles of each as the keys:

tree = {'Foo Branch': foo, 'Sub-Foo Branch': sub_foo, 'Bar Branch': bar}

Now, I know that there are complex algorithms for making traversal efficient (e.g. MPTT, et al), particularly for use with database-driven projects where efficiency matters the most. I’m not using the database at all, only simple in-memory objects.

Given the title of a Branch, I need to get a list of all descendants of that branch (children, children’s children, so-on) from tree, so:

  1. Would you still recommended using a complicated (for my algo-less brain 🙂 algorithm like MPTT for efficiency in my case, or is there a simple way to achieve this in a single function?
  2. If so, which one would you recommend, knowing I’m not using a database?
  3. Can you provide an example, or is this much larger than I’m thinking?

Note: This isn’t a homework assignment. I’m not in school. I’m really just this bad at algorithms. I’ve used Django MPTT for a project which required DB-stored trees… but still don’t understand it very well.

  • 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-23T00:12:17+00:00Added an answer on May 23, 2026 at 12:12 am

    http://en.wikipedia.org/wiki/Depth-first_search

    http://en.wikipedia.org/wiki/Tree_traversal

    You traverse as follows in two passes:

    • First pass: Search for the query node with the appropriate key. (This step is unnecessary if you have a hashmap of all the nodes in the entire tree; you have this (good) so this step is not necessary.)

    • Second pass: Call a modified version of the algorithm on the query node, but this time, whenever you visit a node, yield it (or append it to a nonlocal accumulator variable).

    However your situation is a bit odd, because normally trees have pointers to children as well, sort of like a double-linked list. Unfortunately we don’t have that information… but fortunately it’s easy to add that information:

    nodes = tree.values()
    for node in nodes:
        if node.parent:
            if not hasattr(node.parent, 'children'):
                node.parent.children = []
            node.parent.children +=[ node ]
    

    Now we can proceed with our example:

    def traverse(root, callback):
        """
            Peform callback on all nodes in depth-first order
            e.g. traverse(root, lambda x:print(x))
        """
        yield root, callback(root)
        for child in root.children:
            traverse(child)
    
    def getAllDescendents(title):
        queryNode = titlesToNodes[title]  #what you call 'tree'
        for node,blah in traverse(queryNode, lambda x:None):
            yield node
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree like structure roughly like this: class Node < ActiveRecord::Base belongs_to
I am having a hard time in manipulating strings while writing module for linux.
I'm kind of new to Haskell and I'm having a hard time understanding what
I'm working on a DotNetNuke module that includes a tree-style navigation menu. So far,
I have been trying for several hours now to get sorl-thumbnail working, but it
I'm implementing a dynamic language that will compile to C#, and it's implementing its
When I click .getdata , I want to go from .getdata to name=top and
Can someone point to me or explain if this is true, you must not
I'm seeing strange behavior when it comes to focus and keyboard navigation. In the
How do you optimize ActiveRecord calls in your ASP.NET MVC 2 web applications ?

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.