I'm trying to simplify one of my homework problems and make the code a little better. What I'm working with is a binary search tree. Right now I have a function in my Tree()
class that finds all the elements and puts them into a list.
tree = Tree() #insert a bunch of items into tree
then I use my makeList() function to take all the nodes from the tree and puts them in a list. To call the makeList()
function, I do tree.makeList(tree.root)
. To me this seems a little repetitive. I'm already calling the tree object with tree.
so the tree.root
is just a waste of a little typing.
Right now the makeList function is:
def makeList(self, aNode): if aNode is None: return [] return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)
I would like to make the aNode input a default parameter such as aNode = self.root
(which does not work) that way I could run the function with this, tree.makeList()
.
First question is, why doesn't that work?
Second question is, is there a way that it can work? As you can see the makeList()
function is recursive so I cannot define anything at the beginning of the function or I get an infinite loop.
EDIT Here is all the code as requested:
class Node(object): def __init__(self, data): self.data = data self.lChild = None self.rChild = None class Tree(object): def __init__(self): self.root = None def __str__(self): current = self.root def isEmpty(self): if self.root == None: return True else: return False def insert (self, item): newNode = Node (item) current = self.root parent = self.root if self.root == None: self.root = newNode else: while current != None: parent = current if item < current.data: current = current.lChild else: current = current.rChild if item < parent.data: parent.lChild = newNode else: parent.rChild = newNode def inOrder(self, aNode): if aNode != None: self.inOrder(aNode.lChild) print aNode.data self.inOrder(aNode.rChild) def makeList(self, aNode): if aNode is None: return [] return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild) def isSimilar(self, n, m): nList = self.makeList(n.root) mList = self.makeList(m.root) print mList == nList
Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.
The calling process is automatic while the receiving process is not (its explicit). This is the reason the first parameter of a function in class must be the object itself. Writing this parameter as self is merely a convention. It is not a keyword and has no special meaning in Python.
Use self when:you define an instance method, since it is passed automatically as the first parameter when the method is called; you reference a class or an instance attribute from inside an instance method; you want to refer to instance variables and methods from other instance methods.
No, self is not automatically passed to the object, because it has not been defined within the class block.
larsmans answered your first question
For your second question, can you simply look before you leap to avoid recursion?
def makeList(self, aNode=None): if aNode is None: aNode = self.root treeaslist = [aNode.data] if aNode.lChild: treeaslist.extend(self.makeList(aNode.lChild)) if aNode.rChild: treeaslist.extend(self.makeList(aNode.rChild)) return treeaslist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With