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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:38:46+00:00 2026-06-14T08:38:46+00:00

I have been trying to implement a simple tree structure in Python. A tree

  • 0

I have been trying to implement a simple tree structure in Python. A tree begins at a single “root” node which has children, each of its children may have own children and so forth.

Now, I want to print the names of all nodes of the tree, that is I want to convert it to a list. I sought to employ recursiveness but unfortunately yielding recursively returns a sub-tree of generator objects which I cannot convert to nodes.

Could somebody help me and point out what I am doing wrong here please?

class Node:

  def __init__(self,name):
    self.name = name
    self.children = []
    self.parent = None


  def appendChild(self,child):
    self.children.append(child)
    if child is not None:
      child.parent = self


  def listChildren(self):
    yield self
    for child in self.children:
      yield child.listChildren()
    raise StopIteration

# test
r = Node("root")

n = Node("name")
r.appendChild(n)
n.appendChild(Node("name2"))
n.appendChild(Node("name3"))

c = Node("child")
n.appendChild(c)
c.appendChild(Node("child2"))
c.appendChild(Node("child3"))

r.appendChild(Node("name4"))
r.appendChild(Node("name5"))
r.appendChild(Node("name6"))

for child in r.listChildren():
    print child.name

Output:

Traceback (most recent call last):
  File "C:/Users/User/Documents/TreeNode.py", line 40, in <module>
    print child.name
AttributeError: 'generator' object has no attribute 'name'

A generator is supposed to be called when it is being iterated over, but in my case every child in r.listChildren() is, in turn, a generator object. If this is a design flaw, then I would have to look for another way of generating a list of node names.

Thank you in advance!

  • 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-14T08:38:47+00:00Added an answer on June 14, 2026 at 8:38 am

    child.listChildren() will return a generator object and not the actual children. So you probably want to do something like:

    def listChildren(self):
      yield self
      for child in self.children:
        for c in child.listChildren():
          yield c
      raise StopIteration # PS: you don't need to do that explicitly
    

    Alternatively if you use Python 3.3 you could do:

    def listChildren(self):
      yield self
      for child in self.children:
        yield from child.listChildren()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to implement an upgrade plan for my Android app which
Alright, so I've been trying to implement a simple binary search tree that uses
I am trying to implement something seemingly very simple, and I have been beating
I have been trying to implement Bubble Sort using simple integer array in java.
I have been trying to figure out how to implement a simple xml rpc
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
I have been trying to implement a GlassPane for my games' in-game HUD, but
Hello guys, I have been trying to implement the DSUM function but failed to
I have been trying for the past two days now to implement Admob ads
I have been having this annoying problem when trying to implement a picture gallery

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.