zoukankan      html  css  js  c++  java
  • 随手记——数据结构可视化(graphviz)

    • 普通二叉树
     void writedot(BTree tree, FILE* fw)
        {
            if (tree == NULL)
                return;
            else{
                fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> " ];
    ", tree->data, tree->data);
            }
            if (tree->Left){
                fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = red];
    ", tree->Left->data, tree->Left->data);
                fprintf(fw, "%d:f0:sw -> %d:f1;
    ", tree->data, tree->Left->data);
            }
            if (tree->Right){
                fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = blue];
    ", tree->Right->data, tree->Right->data);
                fprintf(fw, "%d:f2:se -> %d:f1;
    ", 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(0);
            }
            fprintf(fw, "digraph
    ");
            fprintf(fw, "{
    ");
            fprintf(fw, "node[shape = Mrecord, color = black]; 
    "); 
            writedot(tree, fw);
            fprintf(fw, "}");
            fclose(fw);
        }
    View Code
    •  完全二叉树(下标从1开始)
    void writedot(Bheap H,int index, FILE* fw)
    {
        if (index > H->count)return;    
            fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> " ];
    ", H->data[index], H->data[index]);
            if (index*2 <= H->count) {
                fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = red];
    ", H->data[index*2], H->data[index*2]);
                fprintf(fw, "%d:f0:sw -> %d:f1;
    ", H->data[index], H->data[index*2]);
            }
            if (index*2+1 <= H->count) {
                fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = red];
    ", H->data[index * 2+1], H->data[index * 2+1]);
                fprintf(fw, "%d:f2:sw -> %d:f1;
    ", H->data[index], H->data[index * 2+1]);
            }
            writedot(H,index*2,fw);
            writedot(H, index * 2 + 1, fw);
    }
    void BHeapVisual(Bheap tree, char* filename) {
        FILE *fw;
        if (NULL == (fw = fopen(filename, "w"))) {
            printf("open file error");
            exit(0);
        }
        fprintf(fw, "digraph
    ");
        fprintf(fw, "{
    ");
        fprintf(fw, "node[shape = Mrecord, color = black]; 
    ");
        writedot(tree,1, fw);
        fprintf(fw, "}");
        fclose(fw);
    }
    View Code

     完全二叉树(下标从0开始)

    void writedot(int* H, int index, FILE* fw,int length)
    {
        if (index > length-1)return;
        fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> " ];
    ", H [index], H [index]);
        if (index * 2+1 <= length-1) {
            fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = red];
    ", H[index * 2 + 1], H[index * 2 + 1]);
            fprintf(fw, "%d:f0:sw -> %d:f1;
    ", H [index], H [index * 2+1]);
        }
        if (index * 2 + 2 <= length-1) {
            fprintf(fw, "%d [label = "<f0> | <f1> %d | <f2> ",color = blue];
    ", H [index * 2 + 2], H [index * 2 + 2]);
            fprintf(fw, "%d:f2:sw -> %d:f1;
    ", H [index], H [index * 2 + 2]);
        }
        writedot(H, index * 2 + 1, fw,length);
        writedot(H, index * 2 + 2, fw,length);
    }
    void BHeapVisual(int* tree, char* filename,int length) {
        FILE *fw;
        if (NULL == (fw = fopen(filename, "w"))) {
            printf("open file error");
            exit(0);
        }
        fprintf(fw, "digraph
    ");
        fprintf(fw, "{
    ");
        fprintf(fw, "node[shape = Mrecord, color = black]; 
    ");
        writedot(tree, 0, fw,length);
        fprintf(fw, "}");
        fclose(fw);
    }
    View Code
    •  链表
    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(0);
        }
        fprintf(fw, "digraph
    ");
        fprintf(fw, "{
    rankdir=LR;
    ");
        fprintf(fw, "node[shape =box, color = blue]; 
    ");
        writedot1(L.head, fw);
        fprintf(fw, "
    }");
        fclose(fw);
    }
    View Code
    • 有向连通图:
    #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 = 0) {
            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 = 0; 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->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 = 0; 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" ];
    ", p->data, p->data);
                }
                fprintf(fw, "%d -> %d;
    ", n->data, p->data);
            }
        }
    
    }
    void GraphVisual(Graph g, char* filename) {
        FILE *fw;
        if (NULL == (fw = fopen(filename, "w"))) {
            printf("open file error");
            exit(0);
        }
        fprintf(fw, "digraph
    ");
        fprintf(fw, "{
    ");
        fprintf(fw, "node[shape = circle, color = black]; 
    ");
        writedot1(g, fw);
        fprintf(fw, "}");
        fclose(fw);
    }
    View Code
    •  邻接表实现的有向图:
    #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 = 0; 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->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->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(0);
        }
        fprintf(fw, "digraph
    ");
        fprintf(fw, "{
    ");
        fprintf(fw, "node[shape = circle, color = black]; 
    ");
        writedot1(g, fw);
        fprintf(fw, "}");
        fclose(fw);
    }
    View Code
  • 相关阅读:
    C/C++ 子集生成算法整理
    C++ 求枚举排列的两种方法
    uva1600 Patrol Robot(不同的BFS最短路)
    C++ 已知二叉树两种遍历序列,构建二叉树
    优先队列 C++
    离散化方法
    基本模运算
    C++ 快速幂运算
    病毒通过445端口
    最新勒索软件WannaCrypt病毒感染前后应对措施
  • 原文地址:https://www.cnblogs.com/czc1999/p/10303456.html
Copyright © 2011-2022 走看看