-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFS.cpp
63 lines (62 loc) · 1.27 KB
/
BFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <queue>
#include "Node.cpp"
using namespace std;
void BFS(GraphNode node)
{
vector<GraphNode> checkTable;
queue<GraphNode> q;
checkTable.push_back(node);
q.push(node);
cout << endl
<< node.symbol;
for (GraphNode temp : node.linked)
{
q.push(temp);
}
while (!q.empty())
{
node = q.front();
bool exists = false;
for (GraphNode temp : checkTable)
{
if (temp.symbol == node.symbol)
{
exists = true;
break;
}
}
if (!exists)
{
cout << endl
<< node.symbol;
for (GraphNode temp : node.linked)
{
q.push(temp);
}
}
q.pop();
checkTable.push_back(node);
}
}
int main()
{
GraphNode a = GraphNode('a');
GraphNode b = GraphNode('b');
GraphNode c = GraphNode('c');
GraphNode d = GraphNode('d');
GraphNode e = GraphNode('e');
GraphNode f = GraphNode('f');
GraphNode h = GraphNode('h');
GraphNode j = GraphNode('j');
b.Link(f);
h.Link(j);
h.Link(d);
e.Link(c);
d.Link(c);
a.Link(b);
a.Link(c);
BFS(a);
return 0;
}