I am trying to traverse a Binary Tree which is created in the following code. to be precise, the Binary Tree is a class and should include an iterator calling another function namely inorder(). this method should be a recursive generator and yield the value of nodes in every iteration.I tried to create a dictionary to follow the nodes but when I try to call the inorder() method, it doesn't work. Is there any missing point that I don't know? I used while and it creates the dictionary of left side of tree (it is a clumsy way). please help me accomplish this code.
d=[]
# A binary tree class.
class Tree(object):
def __init__(self, label, left=None, right=None):
self.label = label
self.left = left
self.right = right
self.d=dict()
def __repr__(self, level=0, indent=" "):
s = level * indent + self.label
if self.left:
s = s + "\n" + self.left.__repr__(level + 1, indent)
if self.right:
s = s + "\n" + self.right.__repr__(level + 1, indent)
return s
def traverse(self):
if self.left:
lastLabel=self.label
self.left.traverse()
if self.right:
lastLabel=self.label
d.append(lastLabel)
self.right.traverse()
else:
d.append(self.label)
return d
def __iter__(self):
return inorder(self)
# Create a Tree from a list.
def tree(sequence):
n = len(sequence)
if n == 0:
return []
i = n / 2
return Tree(sequence[i], tree(sequence[:i]), tree(sequence[i+1:]))
# A recursive generator that generates Tree labels in in-order.
def inorder(t):
for i in range(len(d)):
yield d[i]
def test(sequence):
# Create a tree.
t = tree(sequence)
# Print the nodes of the tree in in-order.
result = []
for x in t:
result.append(x)
print x
print
result_str = ''.join(result)
# Check result
assert result_str == sequence
del d[:]
def main():
# Third test
test("0123456789")
print 'Success! All tests passed!'
if __name__ == '__main__':
main()
I changed my code again I accomplished the code but I'm sure it is not the best way to traverse a Binary tree. I defined a method -traverse()- in my class and returned a list of nodes in order now (which at first wasn't ordered, so I used sort() method.) then I made a loop over this list in my generator, inorder() function, to yield the element of it. All your comments are very welcome to optimize the code. please recommend a proper solution based on the specific Tree class in this code.