Skip to content

Commit 1236960

Browse files
authored
Merge pull request #13 from neha629/tree-algos
trees
2 parents 8b3cc65 + 83c2f46 commit 1236960

12 files changed

+296
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
void bottomView(Node *root)
2+
{
3+
// Your Code Here
4+
if(root==NULL)
5+
return;
6+
map<int,int> m;
7+
int hd=0;
8+
queue<pair<Node*,int>> q;
9+
q.push({root,0});
10+
while(!q.empty())
11+
{
12+
pair<Node*,int> temp=q.front();
13+
q.pop();
14+
hd=temp.second;
15+
Node *node=temp.first;
16+
m[hd]=node->data;
17+
if(node->left!=NULL)
18+
q.push({node->left,hd-1});
19+
if(node->right!=NULL)
20+
q.push({node->right,hd+1});
21+
}
22+
for(auto it=m.begin();it!=m.end();it++)
23+
{
24+
cout<<it->second<<" ";
25+
}
26+
}
27+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
void func(Node* node,int d,map<int,int> &m)
2+
{
3+
if(node==NULL)
4+
return;
5+
m[d]+=node->data;
6+
func(node->left,d+1,m);
7+
func(node->right,d,m);
8+
}
9+
void diagonalSum(Node* root)
10+
{
11+
// Add your code here
12+
map<int,int> m;
13+
func(root,0,m);
14+
for(auto it=m.begin();it!=m.end();it++)
15+
{
16+
cout<<it->second<<" ";
17+
}
18+
cout<<endl;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Computes the diameter of binary tree with given root. */
2+
int dia(Node* node,int &ans)
3+
{
4+
if(node==NULL)
5+
return 0;
6+
int lh=dia(node->left,ans);
7+
int rh=dia(node->right,ans);
8+
ans = max(ans,1+lh+rh);
9+
return 1+max(lh,rh);
10+
}
11+
int diameter(Node* node)
12+
{
13+
// Your code here
14+
int ans = INT_MIN;
15+
dia(node,ans);
16+
return ans;
17+
}
18+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Output will be V space separated integers where the ith integer denote the shortest distance of ith vertex from given source vertex.
2+
int minkey(int V,bool mst[],int key[])
3+
{
4+
int min=INT_MAX,minin;
5+
for(int i=0;i<V;i++)
6+
{
7+
if(!mst[i] && key[i]<min)
8+
min=key[i],minin=i;
9+
}
10+
return minin;
11+
}
12+
void dijkstra(vector<vector<int>> graph, int src, int V)
13+
{
14+
// Your code here
15+
bool mst[V];
16+
int key[V];
17+
for(int i=0;i<V;i++)
18+
mst[i]=false,key[i]=INT_MAX;
19+
key[src]=0;
20+
for(int i=0;i<V-1;i++)
21+
{
22+
int u=minkey(V,mst,key);
23+
mst[u]=true;
24+
for(int v=0;v<V;v++)
25+
{
26+
if(graph[u][v] && !mst[v] && key[u]+graph[u][v]<key[v])
27+
key[v]=key[u]+graph[u][v];
28+
}
29+
}
30+
for(int i=0;i<V;i++)
31+
cout<<key[i]<<" ";
32+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// No of strongly connected components
2+
void fill(vector<int> adj[],int vis[],stack<int> &s,int i)
3+
{
4+
vis[i]=1;
5+
for(auto it:adj[i])
6+
{
7+
if(!vis[it])
8+
fill(adj,vis,s,it);
9+
}
10+
s.push(i);
11+
}
12+
void dfs(vector<int> g[],int vis[],int i)
13+
{
14+
vis[i]=1;
15+
for(auto it:g[i])
16+
{
17+
if(!vis[it])
18+
dfs(g,vis,it);
19+
}
20+
}
21+
int kosaraju(int V, vector<int> adj[])
22+
{
23+
// Your code here
24+
int vis[V],c=0;
25+
stack<int> s;
26+
memset(vis,0,sizeof(vis));
27+
for(int i=0;i<V;i++)
28+
{
29+
if(!vis[i])
30+
fill(adj,vis,s,i);
31+
}
32+
vector<int> g[V+1];
33+
for(int i=0;i<V;i++)
34+
{
35+
for(auto it:adj[i])
36+
g[it].push_back(i);
37+
}
38+
memset(vis,0,sizeof(vis));
39+
while(!s.empty())
40+
{
41+
int x=s.top();
42+
s.pop();
43+
if(!vis[x])
44+
{
45+
dfs(g,vis,x);
46+
c++;
47+
}
48+
}
49+
return c;
50+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
void leftViewUtil(Node *root,int lvl,int &maxlvl)
2+
{
3+
if(root==NULL)
4+
return;
5+
if(lvl>maxlvl)
6+
{
7+
cout<<root->data<<" ";
8+
maxlvl=lvl;
9+
}
10+
leftViewUtil(root->left,lvl+1,maxlvl);
11+
leftViewUtil(root->right,lvl+1,maxlvl);
12+
}
13+
void leftView(Node *root)
14+
{
15+
// Your code here
16+
int maxlvl=-1;
17+
leftViewUtil(root,0,maxlvl);
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Node* LCA(Node *root, int n1, int n2)
2+
{
3+
//Your code here
4+
if(root==NULL)
5+
return root;
6+
if(root->data>n1 && root->data>n2)
7+
return LCA(root->left,n1,n2);
8+
if(root->data<n1 && root->data<n2)
9+
return LCA(root->right,n1,n2);
10+
return root;
11+
}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Node * lca(Node* root ,int n1 ,int n2 )
2+
{
3+
//Your code here
4+
if(root==NULL)
5+
return NULL;
6+
if(root->data==n1 || root->data==n2)
7+
return root;
8+
Node *l=lca(root->left,n1,n2);
9+
Node *r=lca(root->right,n1,n2);
10+
if(l && r)
11+
return root;
12+
return (l!=NULL)?l:r;
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Function to construct and print MST for
2+
// a graph represented using adjacency
3+
// matrix representation, with V vertices.
4+
// graph[i][j] = weight if edge exits else INT_MAX
5+
int minKey(int key[],bool mst[],int V)
6+
{
7+
int min=INT_MAX,minin=0;
8+
for(int i=0;i<V;i++)
9+
{
10+
if(mst[i]==false && key[i]<min)
11+
min=key[i],minin=i;
12+
}
13+
return minin;
14+
}
15+
int spanningTree(int V,int E,vector<vector<int> > graph)
16+
{
17+
// code here
18+
int parent[V],key[V],s=0;
19+
bool mst[V];
20+
for(int i=0;i<V;i++)
21+
key[i]=INT_MAX,mst[i]=false;
22+
key[0]=0,parent[0]=-1;
23+
for(int i=0;i<V-1;i++)
24+
{
25+
int u=minKey(key,mst,V);
26+
mst[u]=true;
27+
for(int v=0;v<V;v++)
28+
{
29+
if(graph[u][v] && mst[v]==false && graph[u][v]<key[v])
30+
parent[v]=u,key[v]=graph[u][v];
31+
}
32+
}
33+
for(int i=0;i<V;i++)
34+
s+=key[i];
35+
return s;
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
void topView(struct Node *root)
2+
{
3+
//Your code here
4+
if(root==NULL)
5+
return;
6+
map<int,int> m;
7+
int hd=0;
8+
queue<pair<Node*,int>> q;
9+
q.push({root,0});
10+
while(!q.empty())
11+
{
12+
pair<Node*,int> temp=q.front();
13+
q.pop();
14+
hd=temp.second;
15+
Node *node=temp.first;
16+
if(m[hd]==0)
17+
m[hd]=node->data;
18+
if(node->left!=NULL)
19+
q.push({node->left,hd-1});
20+
if(node->right!=NULL)
21+
q.push({node->right,hd+1});
22+
}
23+
for(auto it=m.begin();it!=m.end();it++)
24+
{
25+
cout<<it->second<<" ";
26+
}
27+
}
28+

0 commit comments

Comments
 (0)