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

  • Home
  • SEARCH
  • 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 8838515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:01:00+00:00 2026-06-14T10:01:00+00:00

I have a recursive method that I’m using to walk over a red black

  • 0

I have a recursive method that I’m using to walk over a red black tree, and store various node information (in the list storage).

def _walk (self, storage, func, starting_node) :
    if starting_node is not self._nil :
        self._walk(storage, func, starting_node.left)
        storage.append(func(starting_node))
        self._walk(storage, func, starting_node.right)

However, I’d like to re-implement this method so that it builds a generator (from what I understand this should save both time and memory). What’s the “best” way of doing that?

  • 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-06-14T10:01:01+00:00Added an answer on June 14, 2026 at 10:01 am

    Begin by decoupling the action from the walking

    def _walk (self, starting_node) :
        if starting_node is not self._nil :
            for x in self._walk(starting_node.left):
                yield x
            yield starting_node
            for x in self._walk(starting_node.right):
                yield x
    
    def traverse(self):
        starting_node = ???     # maybe these are passed as
        func = ???              # parameters to traverse
        for node in self._walk(starting_node):
            yield func(node)
    

    traverse is roughly equivalent to

    imap(func, self._walk(starting_node))
    

    or this generator expression

    (func(x) for x in self._walk(starting_node))
    

    You can reduce the amount of stack used by manually optimising the tail recursion

    def _walk (self, starting_node) :
        while starting_node is not self._nil :
            for x in self._walk(starting_node.left):
                yield x
            yield starting_node
            starting_node = starting_node.right
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Welcome! I have a recursive public static method named less that takes a tree
I have a tree structure that I am trying to use a recursive method
I have a recursive method that builds a tree-like structure of a resource and
I have a recursive method on the base form that takes in a control
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I have a recursive method that returns categories, and checks for its sub categories.
I have a problem with a recursive method that fills a structure LinkedHashMap<String,LinkedHashMap<String, Integer>>
I have a recursive call to a method that throws a stack overflow exception.
Often, I have a recursive routine within a method that is only called by
I have a recursive method that reversed a string (HW assignment, has to be

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.