数据结构可视化 Graphviz在Python中的使用 [树的可视化]

简介: 数据结构可视化 Graphviz在Python中的使用 [树的可视化]

1. Graphviz 相关介绍

1.1 安装

安装直接在shell里面pip就好了,代码如下:

pip install graphviz

一个例子:

from graphviz import Digraph, Graph
g = Graph(engine='dot',
         node_attr={'shape': 'egg'},
         )
g.edge('hello1', 'hello2', label='l')
g.edge('hello1', 'hello3', label='r')
g.edge('hello2', 'hello4', label='l')
g.edge('hello2', 'hello5', label='r')
g.view()

1.2 有向图和无向图

有向图:graphviz.Digraph() edge 有向图

无向图:graphviz.Graph() edge 无向图

Digraph和Graph参数都是一样的,其中:

name: graphviz源码的文件名 即name.gv

comment: graphviz源码的comment,在源码的第一行

filename: graphviz源码的文件名 即filename

directory: 保存graphviz源码的文件夹

format: 文件格式 bmp, canon, cgimage, cmap, cmapx, cmapx_np, dot, dot_json, eps, exr, fig, gd, gd2, gif, gtk, gv, ico, imap, imap_np, ismap, jp2, jpe, jpeg, jpg, json, json0, pct, pdf, pic, pict, plain, plain-ext, png, pov, ps, ps2, psd, sgi, svg, svgz, tga, tif, tiff, tk, vml, vmlz, vrml, wbmp, webp, x11, xdot, xdot1.2, xdot1.4, xdot_json, xlib

engine: 布局 circo, dot, fdp, neato, osage, patchwork, sfdp, twopi

encoding: 源码保存的编码

graph_attr: 图属性

node_attr: 点属性

edge_attr: 边属性

1.3 node 属性

node_attr = {‘shape’: ‘box’}

node_attr = {‘fontname’: ‘SimHei’}

  • 黑体:SimHei
  • 宋体:SimSun
  • 新宋体:NSimSun
  • 仿宋:FangSong
  • 楷体:KaiTi

1.4 edge 属性

edge_attr = {‘arrowhead’: ‘rnormal’}

待更新

2. 数据结构可视化

2.1 画树

利用Graphviz画树,代码如下:

from graphviz import Digraph
class Node:
    def __init__(self, number, val=None, name=None, left=None, right=None):
        """number 必须要保证每个结点都是独一无二的,其他属性都可以 可存在可不存在"""
        self.number = number
        self.val = val
        self.name = name
        self.left = left
        self.right = right
def plot_tree(root):
    g = Digraph(
      format='png',
        engine='dot',
        node_attr={
            'shape': 'egg',
            'fontname': 'SimHei'
        },
        edge_attr={'arrowhead': 'normal'},
    )
    def dfs(g, node, parent=None, where=None):
        if not node:
            return
        if node and parent:
          # 如果 有name就以name为标签,没有name就以{number:val}表示标签
            name1 = parent.name if parent.name else str(parent.number) + f':{parent.val}'
            name2 = node.name if node.name else str(node.number) + f':{node.val}'
            # label 表示是否标记左or右
            g.edge(name1, name2, label=where)
        dfs(g, node.left, node, where='l')
        dfs(g, node.right, node, where='r')
    
    dfs(g, root)
    g.view()

使用方法如下:

plot_tree(root)


目录
相关文章
|
4天前
|
前端开发 JavaScript 算法
JavaScript 中实现常见数据结构:栈、队列与树
JavaScript 中实现常见数据结构:栈、队列与树
|
5天前
|
机器学习/深度学习
数据结构-----树的易错点
数据结构-----树的易错点
18 4
|
5天前
|
存储 算法
实验 2:树形数据结构的实现与应用
实验 2:树形数据结构的实现与应用
8 0
|
5天前
|
算法 搜索推荐 C语言
Python实现数据结构与算法
【5月更文挑战第13天】学习数据结构与算法能提升编程能力,解决复杂问题,助你面试成功。从选择资源(如《算法导论》、Coursera课程、LeetCode)到实践编码,逐步学习基本概念,通过Python实现栈、队列和快速排序。不断练习、理解原理,探索高级数据结构与算法,参与开源项目和算法竞赛,持续反思与实践,以提升技术能力。
6 0
|
5天前
|
机器学习/深度学习 算法 数据可视化
Python 数据结构和算法实用指南(四)(4)
Python 数据结构和算法实用指南(四)
10 1
|
5天前
|
机器学习/深度学习 存储 算法
Python 数据结构和算法实用指南(四)(3)
Python 数据结构和算法实用指南(四)
15 1
|
5天前
|
存储 算法 搜索推荐
Python 数据结构和算法实用指南(四)(2)
Python 数据结构和算法实用指南(四)
10 0
|
5天前
|
存储 算法 Serverless
Python 数据结构和算法实用指南(四)(1)
Python 数据结构和算法实用指南(四)
14 0
|
5天前
|
存储 算法 搜索推荐
Python 数据结构和算法实用指南(三)(4)
Python 数据结构和算法实用指南(三)
10 1
|
5天前
|
存储 搜索推荐 算法
Python 数据结构和算法实用指南(三)(3)
Python 数据结构和算法实用指南(三)
10 1