2006年07月12日 星期三 21:15
如果我想要t更新,不要w,怎么办? On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > 糊涂了,我忘记其实新的tree产生了,是w不是t. > >>> w=t.list2Tree(test[1:]) > >>> w > <__main__.BinaryTree instance at 0x00BA78F0> > >>> w.printTree() > Capricorn > Aquarius > Cancer > Pices > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > 我加了个list2tree,结果只跑出Capricorn,list2tree什么错吗? > > >>> test=['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > >>> t=BinaryTree(test[0]) > > >>> t.list2Tree(test[1:]) > > >>> t.printTree() > > Capricorn > > > > 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 list2Tree(self,ls): > > nt=BinaryTree(self.key) > > for node in ls: > > nt.addNode(node) > > return nt > > > > 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) > > >
2006年07月13日 星期四 09:06
def list2Tree(self,ls):
for node in ls:
self.addNode(node)
return self
这样将 ls 所有元素 添加到 树上
On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
>
> 如果我想要t更新,不要w,怎么办?
>
> On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
> > 糊涂了,我忘记其实新的tree产生了,是w不是t.
> > >>> w=t.list2Tree(test[1:])
> > >>> w
> > <__main__.BinaryTree instance at 0x00BA78F0>
> > >>> w.printTree()
> > Capricorn
> > Aquarius
> > Cancer
> > Pices
> >
> > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
> > > 我加了个list2tree,结果只跑出Capricorn,list2tree什么错吗?
> > > >>> test=['Capricorn', 'Aquarius', 'Cancer', 'Pices']
> > > >>> t=BinaryTree(test[0])
> > > >>> t.list2Tree(test[1:])
> > > >>> t.printTree()
> > > Capricorn
> > >
> > > 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 list2Tree(self,ls):
> > > nt=BinaryTree(self.key)
> > > for node in ls:
> > > nt.addNode(node)
> > > return nt
> > >
> > > 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)
> > >
> >
>
> _______________________________________________
> 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/20060713/73b8a513/attachment.html
Zeuux © 2025
京ICP备05028076号