Python论坛  - 讨论区

标题:[python-chinese] 次序

2006年07月12日 星期三 12:13

linda.s samrobertsmith at gmail.com
Wed Jul 12 12:13:28 HKT 2006

为什么我想从大到小打印会错?
>>> t.printRevSortedTree()
Traceback (most recent call last):
  File "", line 1, in ?
  File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
    self.right.printRevSortedTree()
  File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
    self.right.printRevSortedTree()
AttributeError: 'NoneType' object has no attribute 'printRevSortedTree'
>>>

用如下代码,
       >>> t=BinaryTree('Capricorn')
       >>> t.addNode('Aquarius')
       >>> t.addNode('Pices')
       >>> t.addNode('Cancer')


class BinaryTree:

 def __init__(self, key, left=None, right=None, parent=None):
     self.key = key
     self.left = left
     self.right = right
     self.parent = parent

    def addNode(self,key):
        if key < self.key:
            if self.left:
                self.left.addNode(key)
            else:
                self.left = BinaryTree(key, parent=self)
        elif key > self.key:
            if self.right:
                self.right.addNode(key)
            else:
                self.right = BinaryTree(key, parent=self)

    def printRevSortedTree(self):
        if self.right:
            self.right.printRevSortedTree()
        print self.key
        if self.left:
            self.left.printRevSortedTree()

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月12日 星期三 13:15

lubiao lubiao.py at gmail.com
Wed Jul 12 13:15:00 HKT 2006

#linda.py
class BinaryTree:
  def __init__(self, key, left=None, right=None, parent=None):
    self.key = key
    self.left = left
    self.right = right
    self.parent = parent

  def addNode(self,key):
    """Add a node in the proper location."""
    if key < self.key:
      if self.left:
         self.left.addNode(key)
      else:
         self.left = BinaryTree(key, parent=self)
    elif key > self.key:
      if self.right:
         self.right.addNode(key)
      else:
         self.right = BinaryTree(key, parent=self)

  def printTree(self):
    print self.key
    if self.left:
      self.left.printTree()
    if self.right:
      self.right.printTree()


  def printRevSortedTree(self):
    if self.right:
      self.right.printRevSortedTree()
    print self.key
    if self.left:
      self.left.printRevSortedTree()


if __name__ == '__main__':
   t=BinaryTree('Capricorn')
   t.addNode('Aquarius')
   t.addNode('Pices')
   t.addNode('Cancer')
   t.printRevSortedTree()
输出结果

E:\>linda.py
Pices
Capricorn
Cancer
Aquarius
E:\>

未见错误

On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
>
> 为什么我想从大到小打印会错?
> >>> t.printRevSortedTree()
> Traceback (most recent call last):
> File "", line 1, in ?
> File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
>    self.right.printRevSortedTree()
> File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
>    self.right.printRevSortedTree()
> AttributeError: 'NoneType' object has no attribute 'printRevSortedTree'
> >>>
>
> 用如下代码,
>       >>> t=BinaryTree('Capricorn')
>       >>> t.addNode('Aquarius')
>       >>> t.addNode('Pices')
>       >>> t.addNode('Cancer')
>
>
> class BinaryTree:
>
> def __init__(self, key, left=None, right=None, parent=None):
>     self.key = key
>     self.left = left
>     self.right = right
>     self.parent = parent
>
>    def addNode(self,key):
>        if key < self.key:
>            if self.left:
>                self.left.addNode(key)
>            else:
>                self.left = BinaryTree(key, parent=self)
>        elif key > self.key:
>            if self.right:
>                self.right.addNode(key)
>            else:
>                self.right = BinaryTree(key, parent=self)
>
>    def printRevSortedTree(self):
>        if self.right:
>            self.right.printRevSortedTree()
>        print self.key
>        if self.left:
>            self.left.printRevSortedTree()
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060712/93d2f6de/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月12日 星期三 13:35

linda.s samrobertsmith at gmail.com
Wed Jul 12 13:35:42 HKT 2006

我换了一下例子,我是写对的.奇怪了,pythonwin怎么老是给我搞怪啊:-<

On 7/11/06, lubiao <lubiao.py at gmail.com> wrote:
>
> #linda.py
> class BinaryTree:
>   def __init__(self, key, left=None, right=None, parent=None):
>     self.key = key
>     self.left = left
>     self.right = right
>     self.parent = parent
>
>
>   def addNode(self,key):
>     """Add a node in the proper location."""
>
>     if key < self.key:
>       if self.left:
>          self.left.addNode(key)
>       else:
>          self.left = BinaryTree(key, parent=self)
>     elif key > self.key:
>       if self.right:
>          self.right.addNode(key)
>       else:
>          self.right = BinaryTree(key, parent=self)
>
>   def printTree(self):
>
>     print self.key
>     if self.left:
>       self.left.printTree()
>     if self.right:
>       self.right.printTree()
>
>
>
>
>   def printRevSortedTree(self):
>     if self.right:
>       self.right.printRevSortedTree()
>     print self.key
>     if self.left:
>       self.left.printRevSortedTree()
>
>
>
> if __name__ == '__main__':
>
>    t=BinaryTree('Capricorn')
>    t.addNode('Aquarius')
>    t.addNode('Pices')
>    t.addNode('Cancer')
>    t.printRevSortedTree()
>
> 输出结果
>
> E:\>linda.py
> Pices
> Capricorn
> Cancer
> Aquarius
> E:\>
>
> 未见错误
>
> On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
> >
> 为什么我想从大到小打印会错?
> >>> t.printRevSortedTree()
> Traceback (most recent call last):
> File "", line 1, in ?
> File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
>    self.right.printRevSortedTree()
> File "C:\NewFolder\py\trees.py", line 117, in printRevSortedTree
>    self.right.printRevSortedTree ()
> AttributeError: 'NoneType' object has no attribute 'printRevSortedTree'
> >>>
>
> 用如下代码,
>       >>> t=BinaryTree('Capricorn')
>       >>> t.addNode('Aquarius')
>       >>> t.addNode('Pices')
>       >>> t.addNode('Cancer')
>
>
> class BinaryTree:
>
> def __init__(self, key, left=None, right=None, parent=None):
>     self.key = key
>     self.left = left
>     self.right = right
>     self.parent = parent
>
>    def addNode(self,key):
>        if key < self.key:
>            if self.left:
>                self.left.addNode(key)
>            else:
>                self.left = BinaryTree(key, parent=self)
>        elif key > self.key:
>            if self.right:
>                self.right.addNode(key)
>            else:
>                self.right = BinaryTree(key, parent=self)
>
>    def printRevSortedTree(self):
>        if self.right:
>            self.right.printRevSortedTree()
>        print self.key
>        if self.left:
>            self.left.printRevSortedTree()
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月12日 星期三 13:52

FireBird ygonic at gmail.com
Wed Jul 12 13:52:15 HKT 2006

那就注意一下python缩进用的是tab还是空格

在 06-7-12,linda. s<samrobertsmith at gmail.com> 写道:
> 我换了一下例子,我是写对的.奇怪了,pythonwin怎么老是给我搞怪啊:-<
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号