2

I have a standard Binary Tree that reads like
1
/ \
2 3
/ \ / \
4 5 6 7

I need to read the binary tree by importing my tree class, creating this tree, and then using a for loop to read it like: [4, 2, 5, 1, 6, 3, 7]

I have already made the tree and my program will generate a like tree with any amount of numbers. My problem is in the method.

def iter(self):

So far I have:

def __iter__(self):
    if self.left:
        self.left.__iter__()
    yield self
    if self.right:
        self.right.__iter__()

But when I run a for loop on the tree object like:

for item in tree: print("{}: {}").format(item.name, item.height())

It only prints the first node in my try with a correct height.

My question is basically, how to print this binary tree using recursion?

  • I was just doing some research about the same thing, and I found this new binarytree module. It already comes with different methods to list the elements or even print the tree strucutre. [This is the link where I found the module.](https://pypi.org/project/binarytree/) I hope it helps somehow. – Diego Casso Sep 14 '19 at 05:23

3 Answers3

9

In Python >= 3.3, one can use the yield from x syntax for recursive iterators:

def __iter__(self):
    if self.left:
        yield from self.left
    yield self
    if self.right:
        yield from self.right

Edit: Python 3.3 support confirmed.

Jashandeep Sohi
  • 4,903
  • 2
  • 23
  • 25
0

Not tried, but probably rather something like:

def __iter__(self):
    if self.left:
        for k in self.left:
            yield k
    yield self
    if self.right:
        for k in self.right:
            yield k

But check it first.

Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
  • Presumably, `for k in self.left` would never enter the body if `self.left` were falsy, making the `if` check redundant. – chepner Nov 11 '15 at 20:09
0

Next element from the collection should be returned by __next__(): What is the interface for python iterators?

__iter__() should return an iterator, an object that defines __next__() method. yield is used in generator which is quite different concept than iterators, but you can use generator function (because it is an iterator), i.e.:

class A:
    def __init__(self, val, a=None, b=None):
        self.left = a
        self.right = b
        self.val = val

    def __iter__(self):
        def generator():
            if self.left:
                for leaf in self.left:
                    yield leaf
            yield self
            if self.right:
                for leaf in self.right:
                    yield leaf
        return generator()

for i in A(3, A(5, A(2), A(1)), A(4)):
    print(i.val)
Community
  • 1
  • 1
myaut
  • 11,174
  • 2
  • 30
  • 62