I want to add commands to draw a graph to a string. Therefore, I wrote a recursive function.
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
I'm confused. If I use the function like this, my string doesnt change:
>>>a = ''
>>>draw(root,a)
>>>print(a)
>>>a
''
I tried to add a "return string", but in that case my function stops after its done with drawing the root, its left and its right child.
As an example:
- root = 3
- root.left = 2
- root.right = 5
2.left = 1
a='' draw(3,a) a
expected output:
'Commands to draw 3, Commands to draw 2, Commands to draw 5, Commands to draw 1'
2.leftdoesn't make much sense in Python. - PM 2Ring