python可视化树结构
import networkx as nx
import matplotlib.pyplot as plt
# 构造一颗树结构
tree = nx.Graph()
tree.add_edge('A', 'B')
tree.add_edge('A', 'C')
tree.add_edge('C', 'D')
tree.add_edge('C', 'E')
tree.add_edge('E', 'F')
tree.add_edge('E', 'G')
# 可视化树结构
pos = nx.spring_layout(tree)
nx.draw(tree, pos, with_labels=True, node_size=500, font_size=15)
plt.show()