【发布时间】:2013-10-20 05:31:52
【问题描述】:
我正在尝试编写一个节点类来创建树结构。好像有 当我尝试使用“addChild”方法将子节点添加到根节点时出现一些问题,因为子节点似乎在其子列表中。我不知道为什么,所以任何帮助表示赞赏。
class node(object):
def __init__(self, name, x, y, children = []):
self.name = name
self.children = children
self.x = x
self.y = y
def addChild(self):
b=node('b', 5, 5)
self.children.append(b)
return
root=node('a',0.,0.)
print root.children
root.addChild()
print root.children
print root.children[0].children
产量:
[<__main__.node object at 0x7faca9f93e50>]
[<__main__.node object at 0x7faca9f93e50>]
而第二个“打印”行应该返回一个空数组。
【问题讨论】:
-
每个节点都引用同一个列表。见stackoverflow.com/questions/1132941/…
标签: python arrays recursion tree append