将孩子添加到树结构并打印
adding childern to tree stucture and print
我正在尝试基于此 post 实现一个 n-arry 树:
[这里][1]
并且在尝试定义添加 children:
的函数时出现错误
class node(object):
def __init__(self, value, children = []):
self.value = value
self.children = children
def __str__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__str__(level+1)
return ret
# trying to implement this method si that I can get rid of
# calling root.children[0].children
def add_child(self, obj):
self.children.append(obj)
def __repr__(self):
return '<tree node representation>'
root = node('grandmother')
root.children = [node('daughter'), node('son')]
root.children[0].children = [node('granddaughter'), node('grandson')]
root.children[1].children = [node('granddaughter'), node('grandson')]
root.add_child([node((1)), node(2)]) # error
print (root)
我希望能够创建一棵树并将其打印出来。
[1]: Printing a Tree data structure in Python
您使用整个列表对象调用 add_child
。在 add_child
中,您使用方法 list.append
将整个列表对象添加到列表本身。
方案一:直接指定节点调用add_child
:
root.add_child(node((1))
root.add_child(node((2))
解决方案 2:使用 list.extend
而不是 list.append
更改 add_child
的实现。前者将提供的参数中的每个元素添加到列表中,而后者将整个参数添加到列表中。
def add_child(self, obj):
self.children.extend(obj)
如果你命名一个方法add_child
,它应该添加一个child,而不是children。如果您添加 children,您应该 extend
列表,而不仅仅是在其末尾附加给定列表。
工作示例:
class Node(object):
def __init__(self, value, children=None):
if children is None:
children = []
self.value = value
self.children = children
def __str__(self, level=0):
ret = "\t" * level + repr(self.value) + "\n"
for child in self.children:
ret += child.__str__(level + 1)
return ret
def add_children(self, obj):
self.children.extend(obj)
root = Node('grandmother')
root.children = [Node('daughter'), Node('son')]
root.children[0].children = [Node('granddaughter'), Node('grandson')]
root.children[1].children = [Node('granddaughter'), Node('grandson')]
root.add_children([Node(1), Node(2)])
print(root)
输出:
'grandmother'
'daughter'
'granddaughter'
'grandson'
'son'
'granddaughter'
'grandson'
1
2
我正在尝试基于此 post 实现一个 n-arry 树: [这里][1] 并且在尝试定义添加 children:
的函数时出现错误 class node(object):
def __init__(self, value, children = []):
self.value = value
self.children = children
def __str__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__str__(level+1)
return ret
# trying to implement this method si that I can get rid of
# calling root.children[0].children
def add_child(self, obj):
self.children.append(obj)
def __repr__(self):
return '<tree node representation>'
root = node('grandmother')
root.children = [node('daughter'), node('son')]
root.children[0].children = [node('granddaughter'), node('grandson')]
root.children[1].children = [node('granddaughter'), node('grandson')]
root.add_child([node((1)), node(2)]) # error
print (root)
我希望能够创建一棵树并将其打印出来。 [1]: Printing a Tree data structure in Python
您使用整个列表对象调用 add_child
。在 add_child
中,您使用方法 list.append
将整个列表对象添加到列表本身。
方案一:直接指定节点调用add_child
:
root.add_child(node((1))
root.add_child(node((2))
解决方案 2:使用 list.extend
而不是 list.append
更改 add_child
的实现。前者将提供的参数中的每个元素添加到列表中,而后者将整个参数添加到列表中。
def add_child(self, obj):
self.children.extend(obj)
如果你命名一个方法add_child
,它应该添加一个child,而不是children。如果您添加 children,您应该 extend
列表,而不仅仅是在其末尾附加给定列表。
工作示例:
class Node(object):
def __init__(self, value, children=None):
if children is None:
children = []
self.value = value
self.children = children
def __str__(self, level=0):
ret = "\t" * level + repr(self.value) + "\n"
for child in self.children:
ret += child.__str__(level + 1)
return ret
def add_children(self, obj):
self.children.extend(obj)
root = Node('grandmother')
root.children = [Node('daughter'), Node('son')]
root.children[0].children = [Node('granddaughter'), Node('grandson')]
root.children[1].children = [Node('granddaughter'), Node('grandson')]
root.add_children([Node(1), Node(2)])
print(root)
输出:
'grandmother'
'daughter'
'granddaughter'
'grandson'
'son'
'granddaughter'
'grandson'
1
2