-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS-DFS.cpp
More file actions
48 lines (43 loc) · 929 Bytes
/
BFS-DFS.cpp
File metadata and controls
48 lines (43 loc) · 929 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void dfs(int node ,int parent,vector<int>adj[]){
cout<<node<<" ";
for(auto it:adj[node]){
if(it!=parent){
dfs(it,node,adj);
}
}
}
void bfs(vector<int>adj[]){
queue<pair<int,int>>q;
q.push({1,0});
while(!q.empty()){
auto it = q.front();
int node = it.first;
int parent = it.second;
cout<<node<<" ";
q.pop();
for(int i=0;i<adj[node].size();i++){
if(adj[node][i]!=parent){
q.push({adj[node][i],node});
}
}
}
}
int main(){
int node;
cin>>node;
vector<int>adj[node+1];
for(int i =0;i<node-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<"The BFS on the entered tree is"<<endl;
bfs(adj);
cout<<endl;
cout<<"The depth first search traversal on the input tree is"<<endl;
dfs(1,0,adj);
return 0;
}