-
Notifications
You must be signed in to change notification settings - Fork 127
/
tree-bfs.cpp
63 lines (43 loc) · 897 Bytes
/
tree-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 <bits/stdc++.h>
using namespace std;
void printLevels(vector<int> graph[], int V, int x)
{
int level[V];
bool marked[V];
queue<int> que;
que.push(x);
level[x] = 0;
marked[x] = true;
while (!que.empty()) {
x = que.front();
que.pop();
for (int i = 0; i < graph[x].size(); i++) {
int b = graph[x][i];
if (!marked[b]) {
que.push(b);
level[b] = level[x] + 1;
marked[b] = true;
}
}
}
cout << "Nodes"
<< " "
<< "Level" << endl;
for (int i = 0; i < V; i++)
cout << " " << i << " --> " << level[i] << endl;
}
int main()
{
int V = 8;
vector<int> graph[V];
graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[1].push_back(5);
graph[2].push_back(5);
graph[2].push_back(6);
graph[6].push_back(7);
printLevels(graph, V, 0);
return 0;
}