数据结构--图--c++实现

node.h

#ifndef node_h
#define node_h

class node
{
public:
    node(char data = 0);
    char _data;
    bool _isVisited;
};

#endif

node.cpp

#include "node.h"
#include <iostream>
using namespace std;

node::node(char data)
{
    _data = data;
    _isVisited = false;
}

map.h

#ifndef map_h
#define map_h
#include <vector>
using namespace std;
#include "node.h"
class map
{
public:
    map(int capacity);
    ~map();
    bool addNode(node *pNode);
    void resetNode();
    bool setValueToMatrixForDirectedGraph(int row,int col,int val = 1);//为有向图设置邻接矩阵
    bool setValueToMatrixForUndirectedGraph(int row,int col,int val = 1);//为无向图设置邻接矩阵
    void printMatrix();//打印邻接矩阵
    void depthFirstTraverse(int nodeIndex);//深度优先遍历
    void breadthFirstTraverse(int nodeIndex);//广度优先遍历
private:
    bool getValueFromMatrix(int row,int col,int &val);//从矩阵中获取权值
    void breadthFirstTraverseImpl(vector<int>preVec);//广度优先遍历实现函数
    int _capacity;//图中最多可以容纳的顶点数
    int _nodeCount;//已经添加的顶点个数
    node *_nodeArray;//用来存放顶点数组
    int *_matrix;//用来存放邻接矩阵
};
#endif

map.cpp

#include"map.h"
#include<vector>
#include <iostream>
using namespace std;


map::map(int capacity){
    _capacity = capacity;
    _nodeCount = 0;
    _nodeArray = new node[capacity];
    _matrix = new int[_capacity*_capacity];
    memset(_matrix,0,_capacity*_capacity*sizeof(int));
}

map::~map(){
    delete []_nodeArray;
    delete _matrix;
}

bool map::addNode(node *pNode){
    _nodeArray[_nodeCount]._data = pNode->_data;
    _nodeCount++;
    return true;
}

void map::resetNode(){
    for (int i = 0;i<_nodeCount;i++)
    {
        _nodeArray[i]._isVisited = false;
    }
}

bool map::setValueToMatrixForDirectedGraph(int row,int col,int val){
    if (row<0||row>=_capacity)
    {
        return false;
    } 
    if (col<0||col>=_capacity)
    {
        return false;
    }
    _matrix[row*_capacity+col] = val;
    return true;
}

bool map::setValueToMatrixForUndirectedGraph(int row,int col,int val){
    if (row<0||row>=_capacity)
    {
        return false;
    } 
    if (col<0||col>=_capacity)
    {
        return false;
    }
    _matrix[row*_capacity+col] = val;
    _matrix[col*_capacity+row] = val;
    return true;
}

void map::printMatrix(){
    for (int i = 0;i<_capacity;i++)
    {
        for (int j =0;j<_capacity;j++)
        {
            cout<<_matrix[i*_capacity+j]<<" ";
        }cout<<endl;
    }
}

void map::depthFirstTraverse(int nodeIndex){
    int value = 0;
    cout<<_nodeArray[nodeIndex]._data<<" ";
    _nodeArray[nodeIndex]._isVisited = true;
    for (int i = 0;i<_capacity;i++)
    {
        getValueFromMatrix(nodeIndex,i,value);
        if (value == 1)
        {
            if (_nodeArray[i]._isVisited)
            {
                continue;
            }else
            {
                depthFirstTraverse(i);
            }
        }else
        {
            continue;
        }
    }
}

void map::breadthFirstTraverse(int nodeIndex){
    cout<<_nodeArray[nodeIndex]._data<<" ";
    _nodeArray[nodeIndex]._isVisited = true;
    vector<int>vec;
    vec.push_back(nodeIndex);
    breadthFirstTraverseImpl(vec);
}

bool map::getValueFromMatrix(int row,int col,int &val){
    if (row<0||row>=_capacity)
    {
        return false;
    } 
    if (col<0||col>=_capacity)
    {
        return false;
    }
    val = _matrix[row*_capacity+col];
    return true;
}

void map::breadthFirstTraverseImpl(vector<int>preVec){
    int value = 0;
    vector<int>vec;
    for (int j = 0;j<(int)preVec.size();j++)
    {
        for (int i = 0;i<_capacity;i++)
        {
            getValueFromMatrix(preVec[j],i,value);
            if (value!=0)
            {
                if (_nodeArray[i]._isVisited)
                {
                    continue;
                }else
                {
                    cout<<_nodeArray[i]._data<<" ";
                    _nodeArray[i]._isVisited = true;    
                    vec.push_back(i);
                }
            }
        }
    }
    if (vec.size() == 0)
    {
        return;
    }else
    {
        breadthFirstTraverseImpl(vec);
    }
}

demo.cpp

#include "map.h"
#include <iostream>
using namespace std;


int main(){
    map *pMap = new map(8);
    node *pNodeA = new node('A');
    node *pNodeB = new node('B');
    node *pNodeC = new node('C');
    node *pNodeD = new node('D');
    node *pNodeE = new node('E');
    node *pNodeF = new node('F');
    node *pNodeG = new node('G');
    node *pNodeH = new node('H');

    pMap->addNode(pNodeA);
    pMap->addNode(pNodeB);
    pMap->addNode(pNodeC);
    pMap->addNode(pNodeD);
    pMap->addNode(pNodeE);
    pMap->addNode(pNodeF);
    pMap->addNode(pNodeG);
    pMap->addNode(pNodeH);

    pMap->setValueToMatrixForUndirectedGraph(0,1);
    pMap->setValueToMatrixForUndirectedGraph(0,3);
    pMap->setValueToMatrixForUndirectedGraph(1,2);
    pMap->setValueToMatrixForUndirectedGraph(1,5);
    pMap->setValueToMatrixForUndirectedGraph(3,6);
    pMap->setValueToMatrixForUndirectedGraph(3,7);
    pMap->setValueToMatrixForUndirectedGraph(6,7);
    pMap->setValueToMatrixForUndirectedGraph(2,4);
    pMap->setValueToMatrixForUndirectedGraph(4,5);

    pMap->printMatrix();cout<<endl;
    pMap->resetNode();
    pMap->depthFirstTraverse(0);cout<<endl;

    pMap->resetNode();
    pMap->breadthFirstTraverse(0);cout<<endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43150428/article/details/82556461