我正在使用PyGraphviz绘制二进制搜索树。我无法使用PyGraphviz创建重复节点,因为边缘循环回节点。
例如,以下代码仅生成5个节点,而忽略了重复的节点。我尝试使用唯一索引标记每个节点,但这不能解决问题。
import pygraphviz as pgv
tree = pgv.AGraph(directed=True, strict=True)
tree.add_node(2)
tree.add_node(3)
tree.add_node(1)
tree.add_node(7)
tree.add_node(3)
tree.add_node(9)
tree.add_node(2)
tree.write('foo.dot')
image = pgv.AGraph('foo.dot')
image.layout()
image.draw('foo.pdf')
image.close()
我要抽取到BST的代码:
import pygraphviz as pgv
import random
class Node:
insertion_step = []
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def addNode(self, data):
if data < self.data:
if self.left is None:
self.left = Node(data)
self.printSubtree()
else:
self.left.addNode(data) # recursively calling addNode method
else:
if self.right is None:
self.right = Node(data)
self.printSubtree()
else:
self.right.addNode(data)
def printSubtree(self):
if not (self.left is None or self.right is None):
print self.left.data, self.data, self.right.data
self.insertion_step.append((self.left.data, self.data, self.right.data))
elif self.left is None and not self.right is None:
print None, self.data, self.right.data
self.insertion_step.append((None, self.data, self.right.data))
elif not self.left is None and self.right is None:
print self.left.data, self.data, None
self.insertion_step.append((self.left.data, self.data, None))
else:
print None, self.data, None
self.insertion_step.append((None, self.data, None))
def drawTree(self, tree, f):
print self.insertion_step
for step in self.insertion_step:
if not step[0] is None:
tree.add_node(step[0], color='goldenrod2', style='filled')
tree.add_node(step[1], color='goldenrod2', style='filled')
if not step[2] is None:
tree.add_node(step[2], color='goldenrod2', style='filled')
if step[0] is None or step[1] is None or step[2] is None:
tree.add_node('', color='goldenrod1', shape='box', style='filled')
if not step[0] is None:
tree.add_edge(step[1], step[0], color='sienna', style='filled')
else:
tree.add_edge(step[1], '', color='sienna', style='filled')
if not step[2] is None:
tree.add_edge(step[1], step[2], color='sienna', style='filled')
else:
tree.add_edge(step[1], '', color='sienna', style='filled')
tree.write(f)
img = pgv.AGraph(f)
img.layout()
img.draw(f.split('.')[0] + '.pdf')
img.close()
if __name__ == '__main__':
lst = [random.randint(1, 10) for i in range(10)]
print lst
n = Node(lst[0])
n.printSubtree()
for num in lst[1:]:
n.addNode(num)
tree = pgv.AGraph(directed=True, strict=True)
filename = 'tree.dot'
n.drawTree(tree, filename)
从上图中可以看出,由于未创建重复节点,因此边缘处于循环中。请建议我实现这一目标的方法。图中的方框表示空节点。
答案 0 :(得分:10)
节点名称是GraphViz跟踪单个节点的名称,因此具有唯一命名。
但是,您可以自由使用重复的标签。标签将在最终结果中显示,节点的标签默认设置为节点名称。
创建时将标签与节点名称一起设置:
tree.add_node(1, label=2)
tree.add_node(2, label=3)
tree.add_node(3, label=1)
tree.add_node(4, label=7)
tree.add_node(5, label=3)
tree.add_node(6, label=9)
tree.add_node(7, label=2)
请注意,在内部,所有内容都将转换为字符串。
这导致:
您需要重构代码以为您的唯一节点生成唯一ID,然后使用这些ID创建边缘。在这里,我只使用父ID的堆栈遍历您的树:
def drawTree(self, tree, f):
id = 0
nodes = [(None, self)] # queue with nodes to process
while nodes:
parent, node = nodes.pop(0)
tree.add_node(id, label=node.data, color='goldenrod2', style='filled')
if parent is not None:
tree.add_edge(parent, id, color='sienna', style='filled')
if node.left is not None:
nodes.append((id, node.left))
else:
none_id = '{}_left_none'.format(id)
tree.add_node(none_id, label='', color='goldenrod1', shape='box', style='filled')
tree.add_edge(id, none_id, color='sienna', style='filled')
if node.right is not None:
nodes.append((id, node.right))
else:
none_id = '{}_right_none'.format(id)
tree.add_node(none_id, label='', color='goldenrod1', shape='box', style='filled')
tree.add_edge(id, none_id, color='sienna', style='filled')
id += 1
tree.write(f)
img = pgv.AGraph(f)
img.layout(program='dot')
img.draw(f.split('.')[0] + '.pdf')
img.close()
给出:
要使具有相等值的节点之间的边缘变直,您需要尝试向边缘添加权重:
def drawTree(self, tree, f):
id = 0
nodes = [(None, self)] # queue with nodes to process
while nodes:
parent, node = nodes.pop(0)
tree.add_node(id, label=node.data, color='goldenrod2', style='filled')
if parent is not None:
weight = 1
if tree.get_node(parent).attr['label'] == str(node.data):
# same value, increase weight of edge to straighten it.
weight = 10
tree.add_edge(parent, id, color='sienna', style='filled', weight=weight)
if node.left is not None:
nodes.append((id, node.left))
else:
none_id = '{}_left_none'.format(id)
tree.add_node(none_id, label='', color='goldenrod1', shape='box', style='filled')
tree.add_edge(id, none_id, color='sienna', style='filled')
if node.right is not None:
nodes.append((id, node.right))
else:
none_id = '{}_right_none'.format(id)
tree.add_node(none_id, label='', color='goldenrod1', shape='box', style='filled')
tree.add_edge(id, none_id, color='sienna', style='filled')
id += 1
tree.write(f)
img = pgv.AGraph(f)
img.layout(prog='dot')
img.draw(f.split('.')[0] + '.png')
img.close()
导致:
您可以调整确切的权重。
答案 1 :(得分:1)
GraphViz正在为您简化图表。为了阻止它这样做,您可以添加具有相同标签的不同节点,例如添加21和22而不是2并标记它们2.然后您可以单独使用它们。 Label是节点的属性。
我在上面的示例表单中说明了节点和标签的使用。它演示了节点的不同数据类型。有些人有相同的标签。绘制的图形不显示我使用节点ID创建的混乱,但仅显示标签。当然,你会为你的BST选择合理的命名方案,即使它可能只是唯一的数字。
import pygraphviz as pgv
import random
tree = pgv.AGraph(directed=True, strict=True)
tree.add_node("2.1", label='2')
tree.add_node(3.0, label='3')
tree.add_node(3.1, label='3')
tree.add_node(random.randint(1, 1000000), label='7')
tree.add_node(random.randint(1, 1000000), label='7')
tree.add_node(random.randint(1, 1000000), label='7')
tree.add_node("2.2", label='2')
tree.write('foo.dot')
image = pgv.AGraph('foo.dot')
image.layout()
image.draw('foo.pdf')
image.close()
这让我:
编辑:添加了示例代码。