文字列にグラフを描画するコマンドを追加したいと思います。したがって、私は再帰関数を書いた。Pythonで文字列に再帰的に行を追加する
def draw(root,string):
string += 'Commands to draw the root'
if root.left != None:
string += 'Commands to draw a left child'
draw(root.left,string)
if root.right != None:...#same as for the left child
私は混乱しています。私はこのような機能を使用している場合は、私の文字列doesntの変更は:
>>>a = ''
>>>draw(root,a)
>>>print(a)
>>>a
''
は、私が「戻り文字列」を追加しようとしましたが、その場合には、私の機能は、そのその左と右のルートを、図面で行われた後に停止します子。例として
:
- ルート= 3
- root.left = 2
- root.right = 5
2.left = 1
a='' draw(3,a) a
予想される出力:
'Commands to draw 3, Commands to draw 2, Commands to draw 5, Commands to draw 1'
私はそれは一例ですが、 '' 2.left Pythonであまり意味がありません知っています。 –