随手记——数据结构可视化(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的数据结构可视化. 他有多个内置的主题,颜色的主题 可视化单一变量,二维变量用于比较各个变量的分 ...
随机推荐
- RabbitMQ入门教程系列
https://blog.csdn.net/column/details/18247.html
- jQuery事件篇---高级事件
内容提纲: 1.模拟操作 2.命名空间 3.事件委托 4.on.off 和 one 发文不易,转载请注明出处! 一.模拟操作 在事件触发的时候,有时我们需要一些模拟用户行为的操作.例如:当网页加载完毕 ...
- Hadoop worldcount
以前的公司和现在的公司,都用到了hadoop和hdfs.一直没入门,今天照着官网写了一个hadoop worldcount demo 1. hadoop是一个框架,什么是框架,spring是一个框架. ...
- php中怎么导入自己写的类
如果写的类是写在当前php文件内,就直接实例化若你的类写在其他的php文件里,就要先用include或require,将类文件引入<?php include("class.php&qu ...
- HTML5拖拽/拖放(drag & drop)详解
H5中拖拽属性: draggable: auto | true | false 拖动事件: - dragstart 在元素开始被拖动时触发 - dragend 在拖动操作完成时触发 - dra ...
- 解决Cannot read property 'style' of null中样式问题
<script type="text/javascript"> function updateTime(){ var timeNow = new Date(); var ...
- drupal7 获取网站名称
$site_name=variable_get('site_name', 'Drupal');
- win7 远程连接服务器出现身份验证错误,且找不到加密Oracle修正
用远程桌面连接登录服务器,结果,弹出一个错误的提示框:发生身份验证错误,要求的函数不受支持. 然后在网上找了相关的教程,基本上所有的方法都是如下所示: 策略路径:"计算机配置"-& ...
- How do I use the API correctly
1:打开帮助文档2:点击显示,找到索引,看到输入框3:你要学习什么内容,你就在框框里面输入什么内容 举例:Random4:看包 java.lang包下的类在使用的时候是不需要导包的5:看类的描述 Ra ...
- 4 使用Selenium模拟登录csdn,取出cookie信息,再用requests.session访问个人中心(保持登录状态)
代码: # -*- coding: utf-8 -*- """ Created on Fri Jul 13 16:13:52 2018 @author: a " ...