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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:41:10+00:00 2026-05-30T08:41:10+00:00

I’ve created a method of a TreeNode class that I’m wanting to return a

  • 0

I’ve created a method of a TreeNode class that I’m wanting to return a flat list of an in order tree traversal

My sample tree is:

Sample tree data

The in order traversal output should be: [1, 1, 0, 2, 1, 3, 1, 1, 0]

but I’m getting: [2, 1, 1, 0, 1, 3, 1, 1, 0]

Here is my code:

def in_order_list(self, r = []):
    hasLeft = self.left is not None
    hasRight = self.right is not None
    if self is None:
        return
    else:
        if hasLeft:
            self.left.in_order_list(r)
        r.append(self.value)
        if hasRight:
            self.right.in_order_list(r)
    return r

Would anyone be able to give me a clue as to why this is happening?

Thanks
Alex

  • 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-30T08:41:11+00:00Added an answer on May 30, 2026 at 8:41 am

    Instead of calling self.left/right.in_order_list(), you’re calling self.left/right.pre_order_list().

    To accomplish what you want to do a generator function might be better (less memory-consuming and more pythonic) than to accumulate the values in a list:

    class Tree(object):
        ...
        def in_order(self):
            if self.left is not None:
                for value in self.left.in_order():
                    yield value
            yield self.value  #  <-- yielding the value of the node, not the node itself
            if self.right is not None:
                for value in self.right.in_order():
                    yield value
    
    ...
    
    tree = Tree(...)
    
    in_order_values = list(tree.in_order())
    

    That way, you don’t have to create a list if you only want to iterate over the values:

    for value in tree.in_order():
        ...
    

    The algorithm works like this: the generator first descends recursively along the left branch of every node until it hits one with no left sub-node. Then it yields the value of the current node. After that it does the same on the right sub-node, but starting at the current node, not the root node. If we assume there are no cycles in the tree and no infinite branches, then there will definitely be leaf nodes, i.e. nodes with no left or right sub-node. IOW nodes, for which both base cases (self.left/right is None) are reached. Therefore the recursive calls will return, hopefully before we’re out of memory or hit the stack limit.

    The loop over self.left/right.in_order() is necessary due to the fact that what the call to in_order() returns is a generator, hence the name generator function. The returned generator must be exhausted somehow, e.g. through a loop. In the body of the loop we re-yield the values up one level, where they’re re-yielded again, until they reach top level. There we use the values.

    If you want to retrieve the nodes themself instead of only their value fields, you could do it like this:

    class Tree(object):
        ...
        def in_order(self):
            if self.left is not None:
                for node in self.left.in_order():
                    yield node
            yield self  #  <-- yielding the node itself
            if self.right is not None:
                for node in self.right.in_order():
                    yield node
    

    You probably want to do this, because not only can you still access the values of the nodes:

    for node in tree.in_order():
        do_something_with(node.value)
    

    but also you can do whatever you want with the nodes:

    for node in tree.in_order():
        whatever(node.some_other_attribute)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in

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.