问题是:根的子对象本身就是子对象。我在Python中构建数据结构是新的,所以我编写代码就像我写C++一样。这是我的密码:
class node:
key = None
value = None
parent = None
children = []
def __init__(self,key,val):
self.key = key
self.val = val
if __name__ == "__main__":
root = node(50,50)
child = node(20,20)
child.parent = root
root.children.append(child)
print(root)
print(child)
print(root.children)
print(child.parent)
print("Why does child have itself as a child??!!")
print(child.children)
我似乎不知道问题出在哪里
您使用的是类属性,而您应该使用实例属性
您可以修改
__init__
以简化树的构建相关问题 更多 >
编程相关推荐