0

Here is the Class named BNode,

class BNode:
 def __init__(self, value=None, left=None, right=None):
 self.value = value
 self.left = left
 self.right = right
 def __repr__(self):
 return '' % (self.value, self.left, self.right)

and print out below

>>> root = BNode('root')
>>> root.left = BNode('left') 
>>> root.right = BNode('right')
>>> root.left.left = BNode('left-left')
>>> root.left.right = BNode('left=right')
>>> print root
root (
left (
left-left (
None
None)
left-right (
None
None))
right (
None
None))

Q) Modify the Class, so that the result like below could be made.

root (
 left (
 left-left (
 None
 None)
 left-right (
 None
 None))
 right (
 None
 None))

and my answer is the following one.

class BNode:
 def __init__(self, value=None, left=None, right=None):
 self.value = value
 self.left = left
 self.right = right
 def __repr__(self, level=0):
 R = ''
 for ele in range(level):
 R += '\t'
 R += str(self.value)
 R += ' ('
 R += '\n'
 if isinstance(self.left, BNode):
 R += BNode.__repr__(self.left, level+1)
 else:
 for ele in range(level+1):
 R += '\t'
 R += str(self.left)
 R += '\n'
 if isinstance(self.right, BNode):
 R += BNode.__repr__(self.right, level+1)
 else:
 for ele in range(level+1):
 R += '\t'
 R += str(self.right)
 R += ')'
 return R

But I want to know if it is the best solution for this question. I think there is much nicer one... Is there some efficient way?

Thanks in advance ~ :)

mr2ert
5,1821 gold badge23 silver badges33 bronze badges
asked Aug 6, 2013 at 12:09

2 Answers 2

1

First, I'd kill the kwarg on __repr__ - as a dundermethod, it's code path should be exclusively for handling repr() calls, etc. Just move the current code to a .format_node() (or w/e) method that has the kwarg, with __repr__ kicking it off with level=0.

Also, check out the textwrap stdlib module - with the right mix of subsequent_indent, drop_whitespace, and super-large width kwargs, it might be able to do what you need, but the source is among the more accessible of the stdlib modules, check it out for some ideas on the general approach.

http://docs.python.org/2/library/textwrap.html#textwrap.TextWrapper http://hg.python.org/cpython/file/80e9cb6163b4/Lib/textwrap.py

answered Aug 6, 2013 at 12:32
Sign up to request clarification or add additional context in comments.

Comments

0

You're using a recursive method, which is good.

However, you're not using methods correctly - you don't need to call them as BNode.__repr__(self.left, level+1). Instead, you can do: self.left.__repr__(level+1)

Likewise, to create a sequence filled with the same item, instead of an explicit loop, you can use *: '\t'*level

In general, python has a number of string manipulation facilities which you'll find useful: http://docs.python.org/2/library/string.html

Finally, you might like to create a separate tree-oriented map method to do the traversal, and separately write the function which does the work. See: http://rosettacode.org/wiki/Tree_traversal#Python

You can and should clean up your code with these changes.

answered Aug 6, 2013 at 12:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.