• 普通二叉树
 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)的更多相关文章

  1. 二叉树可视化--Graphviz

    大家平时写C程序有没有种把内存里的数据结构全给画出来的冲动呢?数据量小的话,画起来还蛮简单,用viso,我前面的文章都用viso画的.之前写红黑树代码的时候,用的是命令行把整个树打印出来,不过只是一些 ...

  2. python 绘图与可视化 Graphviz 二叉树 、 error: Microsoft Visual C++ 14.0 is required

    需要对二叉树的构建过程进行可视化,发现了这个Graphviz软件,他对描绘数据间的关系十分擅长. 下载链接:https://graphviz.gitlab.io/_pages/Download/Dow ...

  3. Graphviz 使用笔记

    官网:Graphviz 最近一直在找如何用写代码的方式就可以实现数据结构可视化.流程图等等,于是发现了她,上手也比较简单,但正当我仅觉得不错的时候,我发现竟然还可以用python来写,瞬间好感度爆满啊 ...

  4. Java数据结构和算法(四)赫夫曼树

    Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...

  5. Python 数据分析中常用的可视化工具

    Python 数据分析中常用的可视化工具 1 Matplotlib 用于创建出版质量图表的绘图工具库,目的是为 Python 构建一个 Matlab 式的绘图接口. 1.1 安装 Anaconada ...

  6. 软件开发:网站&视频&书籍&文章推荐(不断更新)

    利用书籍进行系统学习,凭借博客/新闻等资料开阔眼界,辅之以代码及项目实战,并勤加以总结,方可进步. 常用网站: 找英文电子书网站:gen.lib.rus.ec 和 www.jiumodiary.com ...

  7. [Java算法] -- 1. 常用排序之冒泡排序和选择排序

    使用Java语言实现冒泡排序和选择排序 推荐一个数据结构可视化的网站:http://zh.visualgo.net/zh (暂时访问不了) 对排序不太熟悉的朋友,建议去上面的网站学习一下,你将会发现一 ...

  8. [Java] 集合框架原理之一:基本结构与源码分析

    一.Collection Collection 接口定义了一些基本的方法: int size(); boolean isEmpty(); boolean add(E e); boolean addAl ...

  9. Python Seaborn 笔记

    Seaborn是Python的一个制图工具库,在Matplotlib上构建,支持numpy和pandas的数据结构可视化. 他有多个内置的主题,颜色的主题 可视化单一变量,二维变量用于比较各个变量的分 ...

随机推荐

  1. C# 之多线程(一)

    概述: 1.进程:是操作系统结构的基础:是一个正在执行的程序:计算机中正在运行的程序实例:可以分配给处理器并由处理器执行的一个实体:由单一顺序的执行显示,一个当前状态和一组相关的系统资源所描述的活动单 ...

  2. 二:HTML基础

    一:html语言基础 1.基本结构 <html> <head> <!--元信息:提供额外信息:关键字.作者信息.页面更新时间.设置字符编码--> <meta ...

  3. CodeForces 598A(水)

    还是要注意int和long long的范围,以及double型的问题 pow函数经常会报一个double型的错,参考这篇文章 http://blog.csdn.net/lawrencesgj/arti ...

  4. 浅谈 Linux 下的 SSH1, SSH2

    SSH:Secure Shell .是一种安全协议. 常见的应用场景是远程控制台登陆. SSH1免费,SSH2收费.  其实 SSH 并不只是在 Linux 和 Unix  下使用,他们同样在 Win ...

  5. python学习之老男孩python全栈第九期_day018知识点总结——正则表达式、re模块

    一. 正则表达式 正则表达式本身和python没有什么关系,就是匹配字符串内容的一种规则. 官方定义:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成 ...

  6. jQuery UI简单的讲解

    我们先进入一下问答时间,你都知道多少呢? (1)什么是jQuery UI 呢? 解答:jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互. ...

  7. WinFrom折线图

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. javascript函数中with的介绍

    /*js函数中with函数的用法分析定义 方便用来引用某个对象中已有的属性但是不能用来给对象添加属性 要给对象创建新的属性 必须明确的引用该对象*/代码格式with(object) statement ...

  9. 远景GIS云上线

    没有发布会.没有嘉宾.没有掌声,趁着国庆假期悄悄地将系统部署到服务器上线运行. 远景GIS云(RGIS Cloud)基于自主研发的远景GIS基础平台开发,目前已实现了Shape上传和导出.符号配置.动 ...

  10. 【html/css】模态框的实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...