2011-04-05 77 views
41

嘿你们都,我试图简化我的一个作业问题,并使代码更好一点。我正在使用的是二叉搜索树。现在我在我的Tree()类中有一个函数,它可以找到所有元素并将它们放入列表中。使用self.xxxx作为默认参数 - Python

tree = Tree() 
#insert a bunch of items into tree 

然后我使用我的makeList()函数从树中获取所有节点并将它们放入一个列表中。 要调用makeList()函数,我需要tree.makeList(tree.root)。对我来说,这似乎有点重复。我已经使用tree.调用了树形对象,因此tree.root只是一点点输入的浪费。

眼下makeList功能是:

def makeList(self, aNode): 
     if aNode is None: 
      return [] 
     return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild) 

我想作阳极输入一个默认的参数,如aNode = self.root(不工作),这样,我可以用这个运行功能,tree.makeList()

第一个问题是,为什么不工作?
第二个问题是,有没有办法可以工作?正如你所看到的,makeList()函数是递归的,所以我不能在函数的开头定义任何东西,或者我得到一个无限循环。

编辑 这里是要求所有代码:

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 
+1

你想在模块级别的方法中使用'self'吗?这绝对没有感觉。如果makeList2()是一个类的方法,那么请提供正确的代码,而不是没有上下文的片段。 – 2011-04-05 16:53:53

+0

makeList2()假设是makeList(),我编辑它 – crh878 2011-04-05 18:06:19

+0

这是怎么回事?我试图使用我的makeList()函数更简单,通过使用树的根的默认参数而不必调用它。 – crh878 2011-04-05 18:40:47

回答

31

larsmans answered你的第一个问题

关于第二个问题,你能简单地三思,以避免递归过吗?

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 
26

它不起作用,因为默认参数在函数定义时计算,而不是在通话时间:

def f(lst = []): 
    lst.append(1) 
    return lst 

print(f()) # prints [1] 
print(f()) # prints [1, 1] 

常用策略是使用默认参数None。如果None是一个有效的价值,使用单哨兵:

NOTHING = object() 

def f(arg = NOTHING): 
    if arg is NOTHING: 
     # no argument 
    # etc. 
+1

你可以省略'Sentinel'。只需使用'NOTHING = object()'。保证产生一个独特的单身人士,你可以通过is检查。 – delnan 2011-04-05 17:02:48

+0

@delnan:好点,删除'哨兵'。 – 2011-04-05 17:05:13

+0

我强烈建议不要让'foo(1,None)'和'foo(1)'做不同的事情。 – 2011-04-05 17:13:34