#include "stdafx.h"
#include
#include
using namespace std;
/*图的遍历
深度优先遍历(前序遍里):邻接矩阵(无向图,对称)
广度优先遍历(分层遍历)
*/
/*struct Node
{
int elem;
Node *link;
};*/
class Node
{
public:
Node(char data = 0);
char m_data;
bool m_visited ;
};
Node::Node(char data )
{
m_data = data;
m_visited = false;
}
class Map
{
public:
Map(int size);
~Map();
bool addNode(Node *pNode); //添加节点
void resetNode(); //重置结点
bool setdirectmatrix(int row, int col, int val = 1); //有向图邻接矩阵
bool setnodirectmatrix(int row, int col, int val = 1); //无向图邻接矩阵
void printMtrix();
//深度优先搜索
void depthfirsttraverse(int nodeindex);
//广度优先搜索
void breadthfirsttraverse(int nodeindex);
void breadthfirsttraverseimpl(vector
private:
int m_size; //容量
int m_length; //现有容量
Node *Nodearray; // 顶点矩阵
int *m_matrix; //邻接矩阵
bool getvalue(int row, int col, int &val); //矩阵中获取值
};
Map::Map(int size)
{
m_size = size;
m_length = 0;
Nodearray = new Node[m_size];
m_matrix = new int[m_size*m_size];
memset(m_matrix, 0, m_size*m_size*sizeof(int));
}
Map::~Map()
{
delete []Nodearray;
delete []m_matrix;
}
bool Map::addNode(Node *pNode)
{
Nodearray[m_length].m_data = pNode->m_data; //放进去时对应的容量值即为索引
m_length++;
return true;
}
void Map::resetNode()
{
for (int i = 0; i < m_length; i++)
{
Nodearray[i].m_visited = false;
}
}
bool Map::setdirectmatrix(int row, int col, int val ) //横向、纵向维度,val代表权值
{
m_matrix[row*m_size + col] = val;
return true;
}
bool Map::setnodirectmatrix(int row, int col, int val)
{
m_matrix[row*m_size + col] = val;
m_matrix[row + col*m_size] = val;
return true;
}
void Map::printMtrix()
{
for (int i = 0; i < m_size; i++)
{
for (int j = 0; j < m_size; j++)
{
cout << m_matrix[i*m_size + j] << " ";
}
cout << endl;
}
}
bool Map::getvalue(int row, int col, int &val)
{
val = m_matrix[row*m_size + col];
return true;
}
void Map::depthfirsttraverse(int nodeindex)
{
int value = 0;
cout << Nodearray[nodeindex].m_data << " ";
Nodearray[nodeindex].m_visited = true;
for (int i = 0; i < m_size; i++)
{
getvalue(nodeindex, i, value);
if (value == 1)
{
if (Nodearray[i].m_visited)
{
continue;
}
else
{
depthfirsttraverse(i);
}
}
else
{
continue;
}
}
}
void Map::breadthfirsttraverse(int nodeindex)
{
cout << Nodearray[nodeindex].m_data << " ";
Nodearray[nodeindex].m_visited = true;
vector
curvec.push_back(nodeindex);
breadthfirsttraverseimpl(curvec);
}
void Map::breadthfirsttraverseimpl(vector
{
int value = 0;
vector
for (int i = 0; i < (int)prevec.size(); i++)
{
for (int j = 0; j < m_size; j++)
{
getvalue(prevec[i], j, value);
if (value != 0)
{
if (Nodearray[j].m_visited)
{
continue;
}
else
{
cout << Nodearray[j].m_data << " ";
Nodearray[j].m_visited = true;
curvec.push_back(j);
}
}
}
}
if (curvec.size() == 0)
{
return;
}
else
{
breadthfirsttraverseimpl(curvec);
}
}
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->setnodirectmatrix(0, 1);
pMap->setnodirectmatrix(0, 3);
pMap->setnodirectmatrix(1, 2);
pMap->setnodirectmatrix(1, 5);
pMap->setnodirectmatrix(3, 6);
pMap->setnodirectmatrix(3, 7);
pMap->setnodirectmatrix(6, 7);
pMap->setnodirectmatrix(2, 4);
pMap->setnodirectmatrix(4, 5);
pMap->printMtrix();
cout << endl;
pMap->resetNode();
pMap->depthfirsttraverse(0);
cout << endl;
pMap->resetNode();
pMap->breadthfirsttraverse(0);
cout << endl;
system("pause");
return 0;
}