随手记——数据结构可视化(graphviz)
- 普通二叉树
void writedot(BTree tree, FILE* fw)
{
if (tree == NULL)
return;
else{
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \" ];\n", tree->data, tree->data);
}
if (tree->Left){
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = red];\n", tree->Left->data, tree->Left->data);
fprintf(fw, "%d:f0:sw -> %d:f1;\n", tree->data, tree->Left->data);
}
if (tree->Right){
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = blue];\n", tree->Right->data, tree->Right->data);
fprintf(fw, "%d:f2:se -> %d:f1;\n", tree->data, tree->Right->data);
}
writedot(tree->Left, fw);
writedot(tree->Right, fw);
}
void BTreeVisual(BTree tree, char* filename){
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))){
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\n");
fprintf(fw, "node[shape = Mrecord, color = black]; \n");
writedot(tree, fw);
fprintf(fw, "}");
fclose(fw);
}
- 完全二叉树(下标从1开始)
void writedot(Bheap H,int index, FILE* fw)
{
if (index > H->count)return;
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \" ];\n", H->data[index], H->data[index]);
if (index* <= H->count) {
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = red];\n", H->data[index*], H->data[index*]);
fprintf(fw, "%d:f0:sw -> %d:f1;\n", H->data[index], H->data[index*]);
}
if (index*+ <= H->count) {
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = red];\n", H->data[index * +], H->data[index * +]);
fprintf(fw, "%d:f2:sw -> %d:f1;\n", H->data[index], H->data[index * +]);
}
writedot(H,index*,fw);
writedot(H, index * + , fw);
}
void BHeapVisual(Bheap tree, char* filename) {
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))) {
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\n");
fprintf(fw, "node[shape = Mrecord, color = black]; \n");
writedot(tree,, fw);
fprintf(fw, "}");
fclose(fw);
}
完全二叉树(下标从0开始)
void writedot(int* H, int index, FILE* fw,int length)
{
if (index > length-)return;
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \" ];\n", H [index], H [index]);
if (index * + <= length-) {
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = red];\n", H[index * + ], H[index * + ]);
fprintf(fw, "%d:f0:sw -> %d:f1;\n", H [index], H [index * +]);
}
if (index * + <= length-) {
fprintf(fw, "%d [label = \"<f0> | <f1> %d | <f2> \",color = blue];\n", H [index * + ], H [index * + ]);
fprintf(fw, "%d:f2:sw -> %d:f1;\n", H [index], H [index * + ]);
}
writedot(H, index * + , fw,length);
writedot(H, index * + , fw,length);
}
void BHeapVisual(int* tree, char* filename,int length) {
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))) {
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\n");
fprintf(fw, "node[shape = Mrecord, color = black]; \n");
writedot(tree, , fw,length);
fprintf(fw, "}");
fclose(fw);
}
- 链表
void writedot1(LNode *L, FILE* fw)//单链表
{
if (L == NULL)
return;
if (L->next) {
fprintf(fw, "%d ->", L->data);
}
else {
fprintf(fw, "%d;", L->data);
}
writedot1(L->next, fw);
}
void LinkListVisual(LinkList L, char* filename) {
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))) {
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\nrankdir=LR;\n");
fprintf(fw, "node[shape =box, color = blue]; \n");
writedot1(L.head, fw);
fprintf(fw, "\n}");
fclose(fw);
}
- 有向连通图:
#pragma once
#include "top.h" using namespace std;
class node;
class edge {
public:
int weight;
node *from, *to;
edge(node *f, node *t, int weight = ) {
from = f;
to = t;
this->weight = weight;
}
};
class node {
public:
int data;//该节点的数据
vector<node *>next;
vector<edge *>edge;
node(int data) {
this->data = data;
}
};
class Graph {
public:
map<int, node *>Nodes;
set<edge *>Edges;
Graph(int en) {
while (en--) {
int from, to;
cin >> from >> to;
if (!Nodes[from]) {
Nodes[from] = new node(from);
}
if (!Nodes[to]) {
Nodes[to] = new node(to);
} node *p = Nodes[from], *q = Nodes[to];
edge *e = new edge(p, q);
Edges.insert(e);
p->next.push_back(q);
p->edge.push_back(e);
}
}
void BFS() {
map<int, node* >::iterator it = Nodes.begin();
node *n = it->second;
queue<node *>q;
set<node *>s;
q.push(n); s.insert(n);
while (!q.empty()) {
node *n = q.front(); q.pop();
cout << n->data << " ";
for (int i = ; i < n->next.size(); i++) {
node *p = n->next[i];
if (s.find(p) == s.end()) {
s.insert(p);
q.push(p);
}
}
} }
}; void writedot1(Graph g, FILE* fw)
{
map<int, node* >::iterator it = g.Nodes.begin();
node *n = it->second;
fprintf(fw, "%d [label = \"%d\" ];\n", n->data, n->data);
set<node *>s;
queue<node *>q;
s.insert(n);
q.push(n);
while (!q.empty()) {
n = q.front(); q.pop();
for (int i = ; i < n->next.size(); i++) {
node *p = n->next[i];
if (s.find(p) == s.end()) {
s.insert(p);
q.push(p);
fprintf(fw, "%d [label = \"%d\" ];\n", p->data, p->data);
}
fprintf(fw, "%d -> %d;\n", n->data, p->data);
}
} }
void GraphVisual(Graph g, char* filename) {
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))) {
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\n");
fprintf(fw, "node[shape = circle, color = black]; \n");
writedot1(g, fw);
fprintf(fw, "}");
fclose(fw);
}
- 邻接表实现的有向图:
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std; class node {
public:
int data;
node* next;
node(int d) {
data = d;
next = NULL;
}
}; class AdjList {
public:
map<int, node*> listhead;
int vexnum, edgenum; // 顶点数,边数
AdjList(int vn, int en) {
vexnum = vn;
edgenum = en;
for (int i = ; i < en; i++) {
int from, to;
cin >> from >> to;
if (!listhead[from]) {
listhead[from] = new node(from);
}
node *s = new node(to);
//这里是为了有向图,某节点出度为0时,打印邻接表的时候会输出它
if (!listhead[to]) {
listhead[to] = new node(to);//这里和s不能用同一个
}
s->next = listhead[from]->next;
listhead[from]->next = s;
}
} void BFS() {
queue<node *>q;
set<int>s;
map<int, node*>::iterator it = listhead.begin();
while (it != listhead.end()) {
if (s.find(it->second->data) != s.end()) {
it++;
continue;
}
node *n = it->second;
q.push(n);
s.insert(n->data);
cout << n->data << " ";
while (!q.empty()) {
node *p = q.front(); q.pop();
while (p) {
if (s.find(p->data) == s.end()) {
cout << p->data << " ";
s.insert(p->data);
q.push(listhead[p->data]);
}
p = p->next;
}
}
cout << endl;
}
}
};
typedef AdjList* Graph; void print(Graph g) {
map<int, node *>::iterator it = g->listhead.begin();
while (it != g->listhead.end()) {
node *p = it->second->next;
cout << it->second->data << " :";
while (p) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
it++;
}
} void writedot1(Graph g, FILE* fw){
map<int, node* >::iterator it = g->listhead.begin();
while (it != g->listhead.end()) {
node *n = it->second;
fprintf(fw, "%d [label = \"%d\" ];\n", n->data, n->data);
it++;
}
it= g->listhead.begin();
while (it != g->listhead.end()) {
node *n = it->second;
node *p = n->next;
while (p) {
fprintf(fw, "%d -> %d;\n", n->data, p->data);
p = p->next;
} it++;
} }
void GraphVisual(Graph g, char* filename) {
FILE *fw;
if (NULL == (fw = fopen(filename, "w"))) {
printf("open file error");
exit();
}
fprintf(fw, "digraph\n");
fprintf(fw, "{\n");
fprintf(fw, "node[shape = circle, color = black]; \n");
writedot1(g, fw);
fprintf(fw, "}");
fclose(fw);
}
随手记——数据结构可视化(graphviz)的更多相关文章
- 二叉树可视化--Graphviz
大家平时写C程序有没有种把内存里的数据结构全给画出来的冲动呢?数据量小的话,画起来还蛮简单,用viso,我前面的文章都用viso画的.之前写红黑树代码的时候,用的是命令行把整个树打印出来,不过只是一些 ...
- python 绘图与可视化 Graphviz 二叉树 、 error: Microsoft Visual C++ 14.0 is required
需要对二叉树的构建过程进行可视化,发现了这个Graphviz软件,他对描绘数据间的关系十分擅长. 下载链接:https://graphviz.gitlab.io/_pages/Download/Dow ...
- Graphviz 使用笔记
官网:Graphviz 最近一直在找如何用写代码的方式就可以实现数据结构可视化.流程图等等,于是发现了她,上手也比较简单,但正当我仅觉得不错的时候,我发现竟然还可以用python来写,瞬间好感度爆满啊 ...
- Java数据结构和算法(四)赫夫曼树
Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...
- Python 数据分析中常用的可视化工具
Python 数据分析中常用的可视化工具 1 Matplotlib 用于创建出版质量图表的绘图工具库,目的是为 Python 构建一个 Matlab 式的绘图接口. 1.1 安装 Anaconada ...
- 软件开发:网站&视频&书籍&文章推荐(不断更新)
利用书籍进行系统学习,凭借博客/新闻等资料开阔眼界,辅之以代码及项目实战,并勤加以总结,方可进步. 常用网站: 找英文电子书网站:gen.lib.rus.ec 和 www.jiumodiary.com ...
- [Java算法] -- 1. 常用排序之冒泡排序和选择排序
使用Java语言实现冒泡排序和选择排序 推荐一个数据结构可视化的网站:http://zh.visualgo.net/zh (暂时访问不了) 对排序不太熟悉的朋友,建议去上面的网站学习一下,你将会发现一 ...
- [Java] 集合框架原理之一:基本结构与源码分析
一.Collection Collection 接口定义了一些基本的方法: int size(); boolean isEmpty(); boolean add(E e); boolean addAl ...
- Python Seaborn 笔记
Seaborn是Python的一个制图工具库,在Matplotlib上构建,支持numpy和pandas的数据结构可视化. 他有多个内置的主题,颜色的主题 可视化单一变量,二维变量用于比较各个变量的分 ...
随机推荐
- Emacs快速入门
Emacs 快速入门 Emacs 启动: 直接打emacs, 如果有X-windows就会开视窗. 如果不想用X 的版本, 就用 emacs -nw (No windows)起动. 符号说明 C-X ...
- Axios的详细配置和相关使用
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. Features 从浏览器中创建 XMLHttpRequests 从 node.js 创建 http ...
- Cheatsheet: 2018 03.01 ~ 2018 03.31
Docker A Developer's Guide to Docker: A Gentle Introduction The Advantages of Using Kubernetes and D ...
- [C#]跨模块的可选参数与常量注意事项
假设某个DLL里有这么一个类: // Lib.dll public class Lib { public const string VERSION = "1.0"; public ...
- 安装并使用Oracle SQL Developer访问Oracle
---问题 如何安装并使用Oracle SQL Developer访问Oracle. ---步骤 Oracle SQL Developer是Oracle官方出品的免费图形化开发工具,相对SQL*Plu ...
- MyBatis学习(三)---MyBatis和Spring整合
想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302. ...
- jquery 提示语淡入效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- csharp: Gets a files formatted size.
/* ASP.NET 默认上传文件是4M ,可以修改服务配置.. <system.web> <!-- 指示 ASP.NET 支持的最大文件上载大小. 该限制可用于防止因用户将大量文件 ...
- VS2010项目转换成VS2008
声明:本篇文章不是本人原创,但是网站的地址没有记下来,所以不能贴出来.但此方法本人亲自验证有效. 一.将.sln文件中的 Microsoft Visual Studio Solution File, ...
- Android dialog圆角显示及解决出现的黑色棱角
最近在开发一个天气预报的app,看到一个比较不错友情提示,如下: 怎么样,看起来比原始的dialog好看吧.好了,做法也许有很多,我介绍下我的做法吧, 首先,我第一个想到 ...