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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:54:50+00:00 2026-05-10T20:54:50+00:00

What is your preferred method of traversing a tree data structure, since recursive method

  • 0

What is your preferred method of traversing a tree data structure, since recursive method calls can be pretty inefficient in some circumstances. I am simply using a generator like the one above. Do you have any hints to make it faster?

def children(self):     stack = [self.entities]     while stack:          for e in stack.pop():             yield e             if e.entities:                 stack.append(e.entities)  

Here is some test data. The first one is recursive, the second uses the generator:

s = time.time() for i in range(100000):     e.inc_counter() print time.time() - s  s = time.time() for i in range(100000):     for e in e.children():         e.inc_counter_s() print time.time() - s 

Results:

0.416000127792 0.298999786377 

Test code:

import random  class Entity():     def __init__(self, name):         self.entities = []         self.name = name         self.counter = 1         self.depth = 0      def add_entity(self, e):         e.depth = self.depth + 1         self.entities.append(e)      def inc_counter_r(self):         for e in self.entities:             e.counter += 1             e.inc_counter_r()      def children(self):         stack = [self.entities]         while stack:             for e in stack.pop():                 yield e                 if e.entities:                     stack.append(e.entities)  root = Entity('main') def fill_node(root, max_depth):     if root.depth <= max_depth:         for i in range(random.randint(10, 15)):             e = Entity('node_%s_%s' % (root.depth, i))             root.add_entity(e)             fill_node(e, max_depth) fill_node(root, 3)  import time s = time.time() for i in range(100):     root.inc_counter_r() print 'recursive:', time.time() - s  s = time.time() for i in range(100):     for e in root.children():         e.counter += 1 print 'generator:',  time.time() - s 
  • 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. 2026-05-10T20:54:51+00:00Added an answer on May 10, 2026 at 8:54 pm

    I can’t think of any big algorithmic improvements, but a simple microoptimisation you can make is to bind frequently called methods (such as stack.append / stack.pop) to locals (this saves a dictionary lookup)

    def children(self):     stack = [self.entities]     push = stack.append     pop = stack.pop     while stack:          for e in pop():             yield e             if e.entities:                 push(e.entities) 

    This gives a small (~15%) speedup by my tests (using 100 traversals of an 8-deep tree with 4 children at each node gives me the below timings:)

    children     :  5.53942348004 children_bind:  4.77636131253 

    Not huge, but worth doing if speed is important.

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

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Don't serialise local classes, because (a) the docs say so… May 11, 2026 at 12:53 pm
  • added an answer Easy with Boost.Filesystem: create_directories #include <boost/filesystem.hpp> //... boost::filesystem::create_directories('/tmp/a/b/c'); Returns: true… May 11, 2026 at 12:53 pm
  • added an answer Navigation history is one area in which WPF is sorely… May 11, 2026 at 12:53 pm

Related Questions

What is your preferred method for reading through the contents of zipped directories with
What is your preferred way of keeping controls centered on its parent when the
What is your best practical user-friendly user-interface design or principle? Please submit those practices
What is your favorite, lossless image format for games (namely 2d games)? And why?
When presented with a simple table view (or even a list view I suppose)
What is your preferred method/datatype for storing passwords in a database (preferably SQL Server
This is also a question that I asked in a comment in one of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.