0

So I have the following:

class Tree(object):
 def__init__(self):
 self.right = None
 self.left = None
 self.val = None

I populate the tree with stuff in my code.

I'd like to write a function of the form

def mult(newVal, node)
 newVal = diff(node.left, newVal, var)
 newVal.append('*')
 newval.append(next(node.right))
 newVal.append('+')
 newVal = diff(node.left, newVal, var)
 newVal.append('*')
 newVal.append(next(node.left))

Where next is simply a function that traverses to the next node in the tree, and diff is a recursive function of the following form:

def diff(node, newVal, var):
...
...
 elif(node.val == '*'):
 newVal = diff(node.left, newVal, var)
 newVal.append('*')
 newval.append(next(node.right))
 newVal.append('+')
 newVal = diff(node.left, newVal, var)
 newVal.append('*')
 newVal.append(next(node.left))
...
...

and my "main" is

 node = Tree()
 newEquation = []
 pos = 0
 pos, newTree = buildTree(node, equation, pos)
 newEquation = diff(newTree, newEquation, variable)
 newEquation = ''.join(newEquation)
 print newEquation

How would I write the def mult() function to accept a tree node as a parameter, and then call the diff() function again?

K Mehta
10.6k4 gold badges50 silver badges80 bronze badges
asked Apr 14, 2012 at 2:33
2
  • What's wrong with how you've written it? Commented Apr 14, 2012 at 2:37
  • it's fixed, a syntax error was throwing a strange error message. Thanks! Commented Apr 14, 2012 at 2:43

1 Answer 1

1

Python is not typed, so if you call mult with:

mult(newEquation, node)

That should work just fine.

answered Apr 14, 2012 at 2:45
Sign up to request clarification or add additional context in comments.

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.