使用 类 在 python 中创建图形数据结构
Creating a graph data structure in python using classes
我正在尝试创建某种类型的 class,它足够通用以用于树和图。
class Node:
def __init__(self, value, children=[]):
self.value = value
self.children = children
def add_child(self, child):
self.children.append(child)
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def letterGraph():
a = Node('A')
b = Node('B')
c = Node('C')
d = Node('D')
c = Node('C')
e = Node('E')
f = Node('F')
g = Node('G')
a.add_children([b, c])
b.add_children([a, d, e])
c.add_children([a, d])
d.add_children([b, c, e, g, f])
e.add_children([b, d, g])
f.add_children([d, g])
g.add_children([e, d, f])
return a
它似乎适用于树,但对于图,当它向当前节点添加一个子节点时,它也会将同一个子节点添加到当前节点的子节点。
example:
current_node: a
a.add_children([b,c])
current_node.children: [b,c]
b.children: [b,c]`
我个人根本不会在构造函数中调用它。你没有使用,那你为什么要把它放在那里?
使用 __repr__
也有助于提高测试时的可读性。
class Node:
def __init__(self, value): # Take children out of constructor
self.value = value
self.children = [] # Initialise children in the function
def add_child(self, child):
self.children.append(child)
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def __repr__(self):
return self.value # I am not a machine
def letterGraph():
a = Node('A')
b = Node('B')
c = Node('C')
d = Node('D')
c = Node('C')
e = Node('E')
f = Node('F')
g = Node('G')
a.add_children([b, c])
b.add_children([a, d, e])
c.add_children([a, d])
d.add_children([b, c, e, g, f])
e.add_children([b, d, g])
f.add_children([d, g])
g.add_children([e, d, f])
print(a.children)
print(b.children)
letterGraph()
这就是我在@amadan
的帮助下修复的方法
infinity = float('inf')
class Node:
def __init__(self, value, children = None, depth=None):
self.value = value
self.children = children
self.depth = depth
def add_child(self, child, depth= None):
if self.children is None:
self.children = [child]
self.depth = depth
else:
self.children.append(child)
def add_child_by_value(self, value):
self.children.add_child(Node(value))
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def is_leaf(self):
return self.children is None
我正在尝试创建某种类型的 class,它足够通用以用于树和图。
class Node:
def __init__(self, value, children=[]):
self.value = value
self.children = children
def add_child(self, child):
self.children.append(child)
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def letterGraph():
a = Node('A')
b = Node('B')
c = Node('C')
d = Node('D')
c = Node('C')
e = Node('E')
f = Node('F')
g = Node('G')
a.add_children([b, c])
b.add_children([a, d, e])
c.add_children([a, d])
d.add_children([b, c, e, g, f])
e.add_children([b, d, g])
f.add_children([d, g])
g.add_children([e, d, f])
return a
它似乎适用于树,但对于图,当它向当前节点添加一个子节点时,它也会将同一个子节点添加到当前节点的子节点。
example:
current_node: a
a.add_children([b,c])
current_node.children: [b,c]
b.children: [b,c]`
我个人根本不会在构造函数中调用它。你没有使用,那你为什么要把它放在那里?
使用 __repr__
也有助于提高测试时的可读性。
class Node:
def __init__(self, value): # Take children out of constructor
self.value = value
self.children = [] # Initialise children in the function
def add_child(self, child):
self.children.append(child)
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def __repr__(self):
return self.value # I am not a machine
def letterGraph():
a = Node('A')
b = Node('B')
c = Node('C')
d = Node('D')
c = Node('C')
e = Node('E')
f = Node('F')
g = Node('G')
a.add_children([b, c])
b.add_children([a, d, e])
c.add_children([a, d])
d.add_children([b, c, e, g, f])
e.add_children([b, d, g])
f.add_children([d, g])
g.add_children([e, d, f])
print(a.children)
print(b.children)
letterGraph()
这就是我在@amadan
的帮助下修复的方法infinity = float('inf')
class Node:
def __init__(self, value, children = None, depth=None):
self.value = value
self.children = children
self.depth = depth
def add_child(self, child, depth= None):
if self.children is None:
self.children = [child]
self.depth = depth
else:
self.children.append(child)
def add_child_by_value(self, value):
self.children.add_child(Node(value))
def add_children(self, list_of_children):
for child in list_of_children:
self.add_child(child)
def is_leaf(self):
return self.children is None