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;
}