2006年07月12日 星期三 22:27
我想把 [11, 12, 13, 14, 15, 16, 17, 18, 19] 变成 [15,12,17,11,13,16,18,14,19]; 也就是每一次取中值,左右再取中值,一直到取完. 不知道怎么样和原来邮件里的tree结合起来? On 7/11/06, imcs ee <imcsee at gmail.com> wrote: - Hide quoted text - > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > 如果把ruturn改成yield,应该就差不多了吧。 > 我写的tree2list函数是 > def tree2list(self): > yield self.key > if self.left: > for foo in self.left.tree2list(): > yield foo > if self.right: > for foo in self.right.tree2list(): > yield foo > 调用代码是 > test = [item for item in t.tree2list()] > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > 运行结果是 > Capricorn > Aquarius > Cancer > Pices > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > 用如下代码, > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > >>> t.addNode('Aquarius') > > > > > > > >>> t.addNode('Pices') > > > > > > > >>> t.addNode('Cancer') > > > > > > > >>> t.printTree() > > > > > > > Capricorn > > > > > > > Aquarius > > > > > > > Cancer > > > > > > > Pices > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > 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()
2006年07月13日 星期四 08:31
看起来像层次遍历 在06-7-12,linda. s <samrobertsmith at gmail.com> 写道: > > 我想把 > [11, 12, 13, 14, 15, 16, 17, 18, 19] > 变成 > [15,12,17,11,13,16,18,14,19]; > 也就是每一次取中值,左右再取中值,一直到取完. > 不知道怎么样和原来邮件里的tree结合起来? > > > On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > > - Hide quoted text - > > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > > 如果把ruturn改成yield,应该就差不多了吧。 > > 我写的tree2list函数是 > > def tree2list(self): > > yield self.key > > if self.left: > > for foo in self.left.tree2list(): > > yield foo > > if self.right: > > for foo in self.right.tree2list(): > > yield foo > > 调用代码是 > > test = [item for item in t.tree2list()] > > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > > 运行结果是 > > Capricorn > > Aquarius > > Cancer > > Pices > > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > 用如下代码, > > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > > >>> t.addNode('Aquarius') > > > > > > > > >>> t.addNode('Pices') > > > > > > > > >>> t.addNode('Cancer') > > > > > > > > >>> t.printTree() > > > > > > > > Capricorn > > > > > > > > Aquarius > > > > > > > > Cancer > > > > > > > > Pices > > > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > > > 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() > > _______________________________________________ > 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/baf1249b/attachment-0001.html
2006年07月13日 星期四 10:11
def buildMidTree(self): klen = len(self.key) assert(0 != klen) if len(self.key[:klen / 2]): self.left = BinaryTree(self.key[:klen / 2]) self.left.buildMidTree() if len(self.key[klen / 2 + 1:]): self.right= BinaryTree(self.key[klen / 2 + 1:]) self.right.buildMidTree() self.key = self.key[klen/2] if __name__ == '__main__': t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19]) t.buildMidTree() t.printTree() 输出结果 15 13 12 11 14 18 17 16 19 因为 printTree 是按深度优先遍历的,如果按 广度优先遍历,就可以得到你要的结果, On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > 我想把 > [11, 12, 13, 14, 15, 16, 17, 18, 19] > 变成 > [15,12,17,11,13,16,18,14,19]; > 也就是每一次取中值,左右再取中值,一直到取完. > 不知道怎么样和原来邮件里的tree结合起来? > > > On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > > - Hide quoted text - > > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > > 如果把ruturn改成yield,应该就差不多了吧。 > > 我写的tree2list函数是 > > def tree2list(self): > > yield self.key > > if self.left: > > for foo in self.left.tree2list(): > > yield foo > > if self.right: > > for foo in self.right.tree2list(): > > yield foo > > 调用代码是 > > test = [item for item in t.tree2list()] > > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > > 运行结果是 > > Capricorn > > Aquarius > > Cancer > > Pices > > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > 用如下代码, > > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > > >>> t.addNode('Aquarius') > > > > > > > > >>> t.addNode('Pices') > > > > > > > > >>> t.addNode('Cancer') > > > > > > > > >>> t.printTree() > > > > > > > > Capricorn > > > > > > > > Aquarius > > > > > > > > Cancer > > > > > > > > Pices > > > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > > > 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() > > _______________________________________________ > 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/7168c7b8/attachment-0001.htm
2006年07月13日 星期四 16:12
怎么样广度优先遍历? On 7/12/06, lubiao <lubiao.py at gmail.com> wrote: > > def buildMidTree(self): > klen = len(self.key) > assert(0 != klen) > > if len(self.key[:klen / 2]): > self.left = BinaryTree(self.key[:klen / 2]) > self.left.buildMidTree() > if len( self.key[klen / 2 + 1:]): > self.right= BinaryTree(self.key[klen / 2 + 1:]) > self.right.buildMidTree() > > self.key = self.key[klen/2] > > > > if __name__ == '__main__': > t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19]) > t.buildMidTree() > t.printTree() > > 输出结果 > 15 > 13 > 12 > 11 > 14 > 18 > 17 > 16 > 19 > > 因为 printTree 是按深度优先遍历的,如果按 广度优先遍历,就可以得到你要的结果, > > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > > 我想把 > [11, 12, 13, 14, 15, 16, 17, 18, 19] > 变成 > [15,12,17,11,13,16,18,14,19]; > 也就是每一次取中值,左右再取中值,一直到取完. > 不知道怎么样和原来邮件里的tree结合起来? > > > On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > > - Hide quoted text - > > > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > > 如果把ruturn改成yield,应该就差不多了吧。 > > 我写的tree2list函数是 > > def tree2list(self): > > yield self.key > > if self.left: > > for foo in self.left.tree2list(): > > yield foo > > if self.right: > > for foo in self.right.tree2list(): > > yield foo > > 调用代码是 > > test = [item for item in t.tree2list()] > > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > > 运行结果是 > > Capricorn > > Aquarius > > Cancer > > Pices > > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > > > On 7/12/06, linda. s < samrobertsmith at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > On 7/11/06, limodou < limodou at gmail.com> wrote: > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > On 7/11/06, limodou < limodou at gmail.com> wrote: > > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > 用如下代码, > > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > > >>> t.addNode('Aquarius') > > > > > > > > >>> t.addNode('Pices') > > > > > > > > >>> t.addNode('Cancer') > > > > > > > > >>> t.printTree() > > > > > > > > Capricorn > > > > > > > > Aquarius > > > > > > > > Cancer > > > > > > > > Pices > > > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > > > 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() > > _______________________________________________ > > 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月13日 星期四 17:23
On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote: > 怎么样广度优先遍历? def printTree2(self): ls = [self] while (ls): node = ls.pop(0) print node.key if node.left: ls.append(node.left) if node.right: ls.append(node.right) 需要一个队列(代码中是 ls) 来实现 if __name__ == '__main__': t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19]) t.buildMidTree() t.printTree() print '***' t.printTree2() 输出结果 E:\linda.py 15 13 12 11 14 18 17 16 19 *** 15 13 18 12 14 17 19 11 16 跟你要的结果有些不同 [11, 12, 13, 14] 4 / 2 = 2 所以 当链表里有 4 个元素时, 认为中间的是 index 为2 的那个,就是 第3个元素。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/f639a315/attachment.htm
2006年07月13日 星期四 18:26
如果要知道多深(几层)呢? On 7/13/06, lubiao <lubiao.py at gmail.com> wrote: > > > On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote: > > 怎么样广度优先遍历? > > > def printTree2(self): > ls = [self] > while (ls): > node = ls.pop(0) > print node.key > if node.left: > ls.append(node.left) > if node.right: > ls.append(node.right ) > > > 需要一个队列(代码中是 ls) 来实现 > > if __name__ == '__main__': > t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19]) > t.buildMidTree() > t.printTree() > print '***' > t.printTree2() > > 输出结果 > > > E:\linda.py > > 15 > 13 > 12 > 11 > 14 > 18 > 17 > 16 > 19 > *** > 15 > 13 > 18 > 12 > 14 > 17 > 19 > 11 > 16 > > 跟你要的结果有些不同 > [11, 12, 13, 14] > 4 / 2 = 2 > 所以 当链表里有 4 个元素时, 认为中间的是 index 为2 的那个,就是 第3个元素。 > > _______________________________________________ > 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月13日 星期四 20:35
def getDepth(self): lDepth = 0 rDepth = 0 if self.left: lDepth = self.left.getDepth() if self.right: rDepth = self.right.getDepth() return max(lDepth, rDepth) + 1 On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote: > > 如果要知道多深(几层)呢? > > On 7/13/06, lubiao <lubiao.py at gmail.com> wrote: > > > > > > On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote: > > > 怎么样广度优先遍历? > > > > > > def printTree2(self): > > ls = [self] > > while (ls): > > node = ls.pop(0) > > print node.key > > if node.left: > > ls.append(node.left) > > if node.right: > > ls.append(node.right ) > > > > > > 需要一个队列(代码中是 ls) 来实现 > > > > if __name__ == '__main__': > > t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19]) > > t.buildMidTree() > > t.printTree() > > print '***' > > t.printTree2() > > > > 输出结果 > > > > > > E:\linda.py > > > > 15 > > 13 > > 12 > > 11 > > 14 > > 18 > > 17 > > 16 > > 19 > > *** > > 15 > > 13 > > 18 > > 12 > > 14 > > 17 > > 19 > > 11 > > 16 > > > > 跟你要的结果有些不同 > > [11, 12, 13, 14] > > 4 / 2 = 2 > > 所以 当链表里有 4 个元素时, 认为中间的是 index 为2 的那个,就是 第3个元素。 > > > > _______________________________________________ > > 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 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/337e42eb/attachment-0001.html
Zeuux © 2025
京ICP备05028076号