二叉树可视化--Graphviz
大家平时写C程序有没有种把内存里的数据结构全给画出来的冲动呢?数据量小的话,画起来还蛮简单,用viso,我前面的文章都用viso画的。之前写红黑树代码的时候,用的是命令行把整个树打印出来,不过只是一些小片段,很丑,还得自己手动再在纸上画一遍,非常麻烦,要是用程序就能把图画出来那就爽了。好了,推荐使用 Graphviz这个东东,so 一下就知道啦,功能挺丰富的,也提供了很多库(c,php,python等),下面画一颗简单的红黑树(红黑树代码参考我前面的代码,下面只写画树的代码)直接看效果图:
这树怎么画的呢?参考官网 http://www.graphviz.org/Documentation.php
1.编写规则文件(可以用Graphviz自带的lib,如cgraph等生成,或者直接就用fprintf生成,可以简单看下中文的翻译 简单的dot规则)
2.用Graphviz自带的一些工具例如dot等解析一下规则文件,然后生成图片(ps,jpg,png等)或者其它格式的文件。
下面是实现。
t.c//测试代码,用到的函数参考我前面写的红黑树文章的代码。
void rbtree_fprint_node(rbtree_node * t,FILE *fp){
if(t->color == RB_BLACK){
fprintf(fp,"node[shape=record,style=filled,color=black,fontcolor=white];\n");
}else{
fprintf(fp,"node[shape=record,style=filled,color=red,fontcolor=white];\n");
}
fprintf(fp,"%d[label=\"<f0> | <f1> %d | <f2> \"];\n",t->key.value,t->key.value);
}
void rbtree_fprint_tree(rbtree_node * t,FILE *fp){
if(rb_nil == t) return;
if(t->parent == rb_nil){//root
rbtree_fprint_node(t,fp);
}
if(t->left != rb_nil){
rbtree_fprint_node(t->left,fp);
fprintf(fp,"%d:f0:sw->%d:f1;\n",t->key.value,t->left->key.value);
}
if(t->right != rb_nil){
rbtree_fprint_node(t->right,fp);
fprintf(fp,"%d:f2:se->%d:f1;\n",t->key.value,t->right->key.value);
}
rbtree_fprint_tree(t->left,fp);
rbtree_fprint_tree(t->right,fp);
}
int main(int argc,char *argv[]){
rbtree_node *t,*p,*max,*min;
int arr[] = {9,8,11,18,2,5,16,1,7,999,234,7662,387,928,454};
int i;
rbtree_init_nil();
t = rbtree_create(15);
t->color = RB_BLACK;
for(i=0;i<15;i++){
rbtree_add(&t,arr[i]);
}
//rb tree print
FILE *fp;
fp = fopen("g2.dot","w+");
fprintf(fp,"digraph G{\n");
rbtree_fprint_tree(t,fp);
fprintf(fp,"}");
fclose(fp);
}
最终生成的是 一个 dot文件,dot文件的语法上官网查,并不是很复杂。
digraph G{
node[shape=record,style=filled,color=black,fontcolor=white];
9[label="<f0> | <f1> 9 | <f2> "];
node[shape=record,style=filled,color=black,fontcolor=white];
5[label="<f0> | <f1> 5 | <f2> "];
9:f0:sw->5:f1;
node[shape=record,style=filled,color=red,fontcolor=white];
18[label="<f0> | <f1> 18 | <f2> "];
9:f2:se->18:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
2[label="<f0> | <f1> 2 | <f2> "];
5:f0:sw->2:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
8[label="<f0> | <f1> 8 | <f2> "];
5:f2:se->8:f1;
node[shape=record,style=filled,color=red,fontcolor=white];
1[label="<f0> | <f1> 1 | <f2> "];
2:f0:sw->1:f1;
node[shape=record,style=filled,color=red,fontcolor=white];
7[label="<f0> | <f1> 7 | <f2> "];
8:f0:sw->7:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
15[label="<f0> | <f1> 15 | <f2> "];
18:f0:sw->15:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
999[label="<f0> | <f1> 999 | <f2> "];
18:f2:se->999:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
11[label="<f0> | <f1> 11 | <f2> "];
15:f0:sw->11:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
16[label="<f0> | <f1> 16 | <f2> "];
15:f2:se->16:f1;
node[shape=record,style=filled,color=red,fontcolor=white];
387[label="<f0> | <f1> 387 | <f2> "];
999:f0:sw->387:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
7662[label="<f0> | <f1> 7662 | <f2> "];
999:f2:se->7662:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
234[label="<f0> | <f1> 234 | <f2> "];
387:f0:sw->234:f1;
node[shape=record,style=filled,color=black,fontcolor=white];
928[label="<f0> | <f1> 928 | <f2> "];
387:f2:se->928:f1;
node[shape=record,style=filled,color=red,fontcolor=white];
454[label="<f0> | <f1> 454 | <f2> "];
928:f0:sw->454:f1;
}
二叉树可视化--Graphviz的更多相关文章
- python 绘图与可视化 Graphviz 二叉树 、 error: Microsoft Visual C++ 14.0 is required
需要对二叉树的构建过程进行可视化,发现了这个Graphviz软件,他对描绘数据间的关系十分擅长. 下载链接:https://graphviz.gitlab.io/_pages/Download/Dow ...
- 二叉树的python可视化和常用操作代码
二叉树是一个重要的数据结构, 本文基于"二叉查找树"的python可视化 pybst 包, 做了一些改造, 可以支持更一般的"二叉树"可视化. 关于二叉树和二叉 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 【LeetCode】208. Implement Trie (Prefix Tree) 实现 Trie (前缀树)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,20 ...
- 1 python使用networkx或者graphviz,pygraphviz可视化RNN(recursive)中的二叉树
代码地址https://github.com/vijayvee/Recursive-neural-networks-TensorFlow 代码实现的是结构递归神经网络(Recursive NN,注意, ...
- 用 Graphviz+pvtrace 可视化函数调用
最近在想怎么把一个程序的函数调用关系快速的用流程图的方式画出来,之后看到了这个一篇文章“用 Graphviz 可视化函数调用”(http://www.ibm.com/developerworks/cn ...
- keras可视化pydot graphviz问题
Keras中提供了一个神经网络可视化的函数plot,并可以将可视化结果保存在本地.plot使用方法如下: from keras.utils.visualize_util import plot plo ...
- 安装doxygen(一个自动文档生成工具)+Graphviz图形可视化软件
参考文章: http://www.fmddlmyy.cn/text21.html http://www.cnblogs.com/duguguiyu/archive/2008/06/29/1231852 ...
- 利用Graphviz 可视化GO 数据库
GO是一个同源蛋白的数据库,按照三大类别BP(生物学过程), MF(分子功能), CC(细胞组分) 对基因的产物-蛋白质进行了分类: 在GO数据库中,本质上是一个有向无环图的数据结构,在三大类别之下, ...
随机推荐
- Android ListView(Selector 背景图片 全选 Checkbox等按钮)
list_item.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...
- 134. Gas Station
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- Python和C|C++的混编(一):Python调用C、C++---Boost库
不使用boost.python库来直接构建dll的话比较繁琐,下面实例是借助boost库实现python对C.C++的调用 1 首先确定已经安装python和boost库,本例测试环境是python2 ...
- mingw32 下编译 zlib
cp win32/makefile.gcc makefile.gcc make -f makefile.gcc make install -f Makefile.gcc INCLUDE_PATH=/m ...
- wildfly9 配置SSL单向认证/https
D:\>keytool -genkey -keystore cdi-init.keystore -alias cdi-init -keyalg RSA -keysize 2048 -validi ...
- Sending data to USB printer in C#?
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Run ...
- maven安装步骤
第一步:配置maven环境 maven3 安装: 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版本是 3.0.4,我用的也是这个. ...
- bootstrap-内联表单 水平(横向)表单 响应式图片 辅助类 [转]
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C# 隐藏和覆盖
1.在C#里面,只有声明vitual.abstract和带override的方法能够被子类重写(override). 2.如果父类方法没有以上关键字,子类又有和父类一样的方法,那么,就默认隐藏父类方法 ...
- 【原】日志处理-Spark
日志信息如下所示: 1.1.1.1 - - [21/Jul/2014:10:00:00 -0800] "GET /majihua/article/284234 HTTP/1.1" ...