- 普通二叉树
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);
}