2006年07月12日 星期三 12:13
为什么我想从大到小打印会错? >>> 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()
2006年07月12日 星期三 13:15
#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
2006年07月12日 星期三 13:35
我换了一下例子,我是写对的.奇怪了,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 > >
2006年07月12日 星期三 13:52
那就注意一下python缩进用的是tab还是空格 在 06-7-12,linda. s<samrobertsmith at gmail.com> 写道: > 我换了一下例子,我是写对的.奇怪了,pythonwin怎么老是给我搞怪啊:-< >
Zeuux © 2025
京ICP备05028076号