Skip to content

Commit 3e1954a

Browse files
Merge pull request #16 from ghost/master
added tree traversals and c++ snippets
2 parents bd52a29 + 22c6073 commit 3e1954a

File tree

8 files changed

+648
-0
lines changed

8 files changed

+648
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define int long long int
5+
#define ll long long
6+
#define quickie ios_base::sync_with_stdio(false); cin.tie(NULL);
7+
#define rep(i, a, b) for(int i=a; i<b; i++)
8+
#define rep1(i, a, b) for(int i=a; i<=b; i++)
9+
#define repp(i, a, b) for(int i=b-1; i>=a; i--)
10+
#define pb push_back
11+
#define mp make_pair
12+
#define fi first
13+
#define se second
14+
#define SZ(x) ((int)(x).size())
15+
#define db double
16+
#define mi map<int, int>
17+
#define vi vector<int>
18+
#define qi queue<int>
19+
#define MI(x) power(x, mod-2)
20+
#define test int t; cin >> t;
21+
#define all(a) a.begin(),a.end()
22+
#define mod 1000000007
23+
#define pi 3.141592653589
24+
#define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod) ;
25+
int power(int x, int y) ;
26+
int gcd(int a, int b) ;
27+
28+
// #include <ext/pb_ds/assoc_container.hpp>
29+
// #include <ext/pb_ds/tree_policy.hpp>
30+
// using namespace __gnu_pbds;
31+
// template<typename T>
32+
// using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
33+
// template<typename T>
34+
// using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
35+
//(*****FOR USING ORDERED SET CHANGE INT*******)
36+
37+
// struct chash {
38+
// const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count();
39+
// static unsigned long long hash_f(unsigned long long x) {
40+
// x += 0x9e3779b97f4a7c15;
41+
// x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
42+
// x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
43+
// return x ^ (x >> 31);
44+
// }
45+
// static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; }
46+
// int operator()(int x) const { return hash_f(x)^RANDOM; }
47+
// };
48+
49+
int power(int x, int y) ;
50+
int gcd(int a,int b) ;
51+
52+
vi v[100001] ;
53+
bool vis[100001] ;
54+
int dis[100001] ;
55+
56+
void bfs(int root) {
57+
qi q ;
58+
q.push(root) ;
59+
vis[root] = true ;
60+
dis[root] = 0 ;
61+
while(!q.empty()) {
62+
int cur = q.front() ;
63+
q.pop() ;
64+
for(auto i : v[cur]) {
65+
if(!vis[i]) {
66+
q.push(i) ;
67+
dis[i] = dis[cur]+1 ;
68+
vis[i] = true ;
69+
}
70+
}
71+
}
72+
}
73+
74+
signed main() {
75+
quickie
76+
int n, m, root ;
77+
cin >> n >> m >> root ;
78+
rep(i, 0, m) {
79+
int a, b ;
80+
cin >> a >> b ;
81+
v[a].pb(b) ;
82+
v[b].pb(a) ;
83+
}
84+
bfs(root) ;
85+
rep(i, 1, n+1) cout << dis[i] << " " ;
86+
cout << "\n" ;
87+
}
88+
89+
int power(int x, int y) {
90+
int res = 1; x %= mod;
91+
while (y > 0) {
92+
if (y & 1)
93+
res = (res*x) % mod;
94+
y = y>>1;
95+
x = (x*x) % mod;
96+
}
97+
return res%mod;
98+
}
99+
100+
int gcd(int a,int b) {
101+
if(a==0) return b;
102+
return gcd(b%a,a);
103+
}
104+

Graphs1/DFS.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
public:
3+
Graph(int V);
4+
void addEdge(int src, int dest);
5+
void DFS(int vertex);
6+
};
7+
8+
// Initialize graph
9+
Graph::Graph(int vertices) {
10+
numVertices = vertices;
11+
adjLists = new list<int>[vertices];
12+
visited = new bool[vertices];
13+
}
14+
15+
// Add edges
16+
void Graph::addEdge(int src, int dest) {
17+
adjLists[src].push_front(dest);
18+
}
19+
20+
// DFS algorithm
21+
void Graph::DFS(int vertex) {
22+
visited[vertex] = true;
23+
list<int> adjList = adjLists[vertex];
24+
25+
cout << vertex << " ";
26+
27+
list<int>::iterator i;
28+
for (i = adjList.begin(); i != adjList.end(); ++i)
29+
if (!visited[*i])
30+
DFS(*i);
31+
}
32+
33+
int main() {
34+
Graph g(4);
35+
g.addEdge(0, 1);
36+
g.addEdge(0, 2);
37+
g.addEdge(1, 2);
38+
g.addEdge(2, 3);
39+
40+
g.DFS(2);
41+
42+
return 0;
43+
}

Graphs1/Kruskal MST.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
class UnionFind //To check for a cycle in the graph.
5+
{
6+
private: vector<int> p,rank;
7+
public:
8+
UnionFind(int N)
9+
{
10+
rank.assign(N,0);
11+
p.assign(N,0);
12+
13+
for(int i=0;i<N;i++)
14+
p[i]=i;
15+
}
16+
17+
int findSet(int i) {return (p[i]==i) ? i : (p[i]=findSet(p[i]));}
18+
19+
bool isSameSet(int a, int b) {return findSet(a)==findSet(b);}
20+
21+
void unionSet(int a,int b)
22+
{
23+
if(!isSameSet(a,b))
24+
{
25+
int x=findSet(a);
26+
int y=findSet(b);
27+
28+
if(rank[x]>rank[y]) {p[y]=x; rank[x]++;}
29+
else
30+
{
31+
p[x]=y;
32+
if(rank[x]==rank[y]) rank[y]++;
33+
}
34+
}
35+
}
36+
};
37+
38+
int main()
39+
{
40+
vector<pair< int, pair<int,int> > > EdgeList;
41+
cout<<"Enter the number of vertices: \n";
42+
int V;
43+
cin>>V;
44+
cout<<"Enter the number of edges: \n";
45+
int E;
46+
cin>>E;
47+
cout<<"Enter the edges with their weights: (Vertex 1 Vertex 2 Weight)\n";
48+
for(int i=0;i<E;i++)
49+
{
50+
int u,v,w;
51+
cin>>u>>v>>w;
52+
EdgeList.push_back(make_pair(w,make_pair(u,v)));
53+
}
54+
55+
sort(EdgeList.begin(),EdgeList.end());
56+
57+
int weight=0;
58+
59+
UnionFind UF(V);
60+
61+
for(int i=0;i<E;i++)
62+
{
63+
pair< int, pair<int,int> > front=EdgeList[i];
64+
if(!UF.isSameSet(front.second.first, front.second.second))
65+
{
66+
weight+=front.first;
67+
UF.unionSet(front.second.first, front.second.second);
68+
}
69+
}
70+
cout<<"Final Weight of the MST is: "<<weight;
71+
return 0;
72+
}

Graphs1/Prims MST.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
vector<int> taken;
5+
vector<vector<pair<int,int> > > AdjList;
6+
priority_queue<pair<int,int> > pq; //Max-Heap in C++ STL
7+
8+
void process(int vtx)
9+
{
10+
taken[vtx]=1;
11+
for(int j=1; j<(int)AdjList[vtx].size(); j++)
12+
{
13+
pair<int,int> v=AdjList[vtx][j];
14+
if(!taken[v.first]) pq.push(make_pair(-v.second,-v.first));
15+
}
16+
}
17+
18+
int main()
19+
{
20+
cout<<"Enter the number of vertices: ";
21+
int V;
22+
cin>>V;
23+
vector<pair<int,int> > temp(1,make_pair(-1,-1));
24+
for(int i=0;i<V;i++) AdjList.push_back(temp);
25+
cout<<"Enter the number of Edges: ";
26+
int E;
27+
cin>>E;
28+
cout<<"Enter the edges with their weights: ";
29+
for(int i=0; i<E; i++)
30+
{
31+
int u,v,w;
32+
cin>>u>>v>>w;
33+
AdjList[u].push_back(make_pair(v,w));
34+
AdjList[v].push_back(make_pair(u,w));
35+
}
36+
taken.assign(V,0);
37+
int weight=0;
38+
39+
process(0);
40+
while(!pq.empty())
41+
{
42+
pair<int,int> p=pq.top();
43+
pq.pop();
44+
int u=-p.second;
45+
int w=-p.first;
46+
47+
if(!taken[u])
48+
weight+=w, process(u);
49+
}
50+
51+
cout<<"The weight of the MST is: "<<weight;
52+
53+
return 0;
54+
}

Trees/Inorder Traversal.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
#include<iostream>
3+
using namespace std;
4+
5+
struct Node
6+
{
7+
int data;
8+
struct Node* left, *right;
9+
Node(int data)
10+
{
11+
this->data = data;
12+
left = right = NULL;
13+
}
14+
};
15+
16+
void printInorder(struct Node* node)
17+
{
18+
if (node == NULL)
19+
return;
20+
21+
printInorder(node->left);
22+
cout << node->data << " ";
23+
printInorder(node->right);
24+
}
25+
26+
int main()
27+
{
28+
struct Node *root = new Node(1);
29+
root->left = new Node(2);
30+
root->right->left = new Node(3);
31+
root->left->left = new Node(4);
32+
root->left->right = new Node(5);
33+
34+
cout << "\nInorder traversal of binary tree is \n";
35+
printInorder(root);
36+
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)