forked from srbcheema1/Algo_Ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
srbcheema1#20.Generating a PowerSet using java
- Loading branch information
1 parent
f17c9e5
commit 5116155
Showing
54 changed files
with
3,862 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
struct tree{ | ||
int data; | ||
struct tree *left; | ||
struct tree *right; | ||
}*root=NULL,*newn; | ||
|
||
struct tree* create(int n) | ||
{ | ||
newn=(struct tree*)malloc(sizeof(struct tree)); | ||
newn->data=n; | ||
newn->left=NULL; | ||
newn->right=NULL; | ||
return newn; | ||
} | ||
|
||
struct tree* ins(struct tree*r,int dat) | ||
{ | ||
if(r==NULL) | ||
return create(dat); | ||
if(dat < r->data) | ||
r->left=ins(r->left,dat); | ||
else if(dat > r->data) | ||
r->right=ins(r->right,dat); | ||
return r; | ||
} | ||
|
||
int max=-10000,min=10000; | ||
|
||
void preorder(struct tree* n) | ||
{ | ||
if(n==NULL) | ||
return; | ||
else | ||
{ | ||
preorder(n->left); | ||
preorder(n->right); | ||
printf("Deleting Node:%d\n",n->data); | ||
free(n); | ||
} | ||
} | ||
int flag=0; | ||
|
||
void main() | ||
{ | ||
int choice=1,t,ne, exit; | ||
printf("Enter Root Node:\n"); | ||
scanf("%d",&ne); | ||
root=ins(root,ne); | ||
while(choice!=0) | ||
{ | ||
printf("Enter Value:\n"); | ||
scanf("%d",&t); | ||
ins(root,t); | ||
printf("Want to Continue the tree:1 or 0...?\n"); | ||
scanf("%d",&choice); | ||
} | ||
preorder(root); | ||
scanf("%d", exit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
struct tree{ | ||
int data; | ||
struct tree* left; | ||
struct tree* right; | ||
}; | ||
|
||
struct tree* create(int dat) | ||
{ | ||
struct tree* n=(struct tree*)malloc(sizeof(struct tree)); | ||
n->data=dat; | ||
n->left=NULL; | ||
n->right=NULL; | ||
return n; | ||
} | ||
|
||
int height(struct tree* n) | ||
{ | ||
int l,r; | ||
if(n==NULL) | ||
return 0; | ||
else | ||
{ | ||
l=height(n->left); | ||
r=height(n->right); | ||
if(l>r) | ||
return(l+1); | ||
else | ||
return (r+1); | ||
} | ||
} | ||
|
||
int count=0; | ||
|
||
int node(struct tree* n) | ||
{ | ||
if(n==NULL) | ||
return count; | ||
else | ||
{ | ||
node(n->left); | ||
node(n->right); | ||
count++; | ||
} | ||
|
||
} | ||
|
||
void main() | ||
{ | ||
struct tree* root; | ||
int ans, exit; | ||
root=create(30); | ||
ans=height(root); | ||
printf("Height of tree after first insertion: %d\n",ans); | ||
root->left=create(20); | ||
root->right=create(40); | ||
root->left->left=create(60); | ||
ans=height(root); | ||
printf("Height of given tree: %d\n",ans); | ||
ans=node(root); | ||
printf("No. of nodes: %d\n",ans); | ||
scanf("%d", exit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/****************************************** | ||
* AUTHOR : VIVEK SHAH * | ||
* INSTITUTION : NIT SURAT * | ||
******************************************/ | ||
|
||
#include <bits/stdc++.h> | ||
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL) | ||
#define ll long long int | ||
#define mod 1000000007 | ||
#define rep(i,a,b) for (ll i = a; i<b; ++i) | ||
|
||
using namespace std; | ||
|
||
struct node | ||
{ | ||
int data; | ||
struct node* left; | ||
struct node* right; | ||
}; | ||
|
||
struct node* getNode(int data){ | ||
struct node* tmp = new node(); | ||
tmp->data = data; | ||
tmp->left = NULL; | ||
tmp->right = NULL; | ||
return tmp; | ||
} | ||
|
||
struct node* insert(struct node* root,int data){ | ||
if (root==NULL) | ||
{ | ||
root = getNode(data); | ||
} | ||
else if (data <= root->data) | ||
{ | ||
root->left = insert(root->left,data); | ||
} | ||
else{ | ||
root->right = insert(root->right,data); | ||
} | ||
return root; | ||
} | ||
|
||
int search(struct node* root, int data){ | ||
if (root==NULL) | ||
{ | ||
return 0; | ||
} | ||
else if (root->data = data) | ||
{ | ||
return 1; | ||
} | ||
else if (data<=root->data) | ||
{ | ||
return search(root->left,data); | ||
} | ||
else{ | ||
return search(root->right,data); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
boost; | ||
struct node* root = NULL; | ||
root = insert(root,15); | ||
root = insert(root,10); | ||
root = insert(root,20); | ||
if(search(root,15)){ | ||
cout<<"Found"; | ||
} | ||
else cout<<"Not Found"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
struct tree{ | ||
int data; | ||
struct tree *left; | ||
struct tree *right; | ||
}*root=NULL,*newn; | ||
struct tree* create(int n) | ||
{ | ||
newn=(struct tree*)malloc(sizeof(struct tree)); | ||
newn->data=n; | ||
newn->left=NULL; | ||
newn->right=NULL; | ||
return newn; | ||
} | ||
struct tree* ins(struct tree*r,int dat) | ||
{ | ||
if(r==NULL) | ||
return create(dat); | ||
if(dat < r->data) | ||
r->left=ins(r->left,dat); | ||
else if(dat > r->data) | ||
r->right=ins(r->right,dat); | ||
return r; | ||
} | ||
int max=-10000,min=10000; | ||
void Preo(struct tree* n) | ||
{ | ||
if(n==NULL) | ||
return; | ||
if(n->data>max) | ||
max=n->data; | ||
if(n->data<min) | ||
min=n->data; | ||
//printf("%d ",n->data); | ||
Preo(n->left); | ||
Preo(n->right); | ||
} | ||
void main() | ||
{ | ||
int choice=1,t,ne, exit; | ||
printf("Enter Root Node:\n"); | ||
scanf("%d",&ne); | ||
root=ins(root,ne); | ||
while(choice!=0) | ||
{ | ||
printf("Enter Data:\n"); | ||
scanf("%d",&t); | ||
ins(root,t); | ||
printf("Want to Continue the tree:1 or 0...?\n"); | ||
scanf("%d",&choice); | ||
} | ||
Preo(root); | ||
printf("Minimum:%d\n",min); | ||
printf("Maximum:%d\n",max); | ||
scanf("%d", exit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include<stdio.h> | ||
#define N 4 | ||
|
||
void display(int sol[N][N]) | ||
{ | ||
int i,j; | ||
for(i=0;i<N;i++) | ||
{ | ||
for(j=0;j<N;j++) | ||
printf("%d ",sol[i][j]); | ||
printf("\n"); | ||
} | ||
} | ||
|
||
|
||
int issafe(int maze[N][N],int x,int y) | ||
{ | ||
if((x>=0 && x<4) && (y>=0 && y<3) && maze[x][y]==1) | ||
return 1; | ||
else | ||
return 0; | ||
} | ||
|
||
int solvemaze(int maze[N][N],int x,int y,int sol[N][N]) | ||
{ | ||
if(x==N-1 && y==N-1) | ||
{ | ||
sol[x][y]=1; | ||
return 1; | ||
} | ||
|
||
if(issafe(maze,x,y)==1) | ||
{ | ||
sol[x][y]=1; | ||
|
||
if(solvemaze(maze,x+1,y,sol)==1) | ||
return 1; | ||
|
||
if(solvemaze(maze,x,y+1,sol)==1) | ||
return 1; | ||
|
||
sol[x][y]=0; | ||
return 0; | ||
} | ||
return 0; | ||
} | ||
|
||
void solve(int maze[N][N]) | ||
{ | ||
int sol[N][N]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; | ||
if(solvemaze(maze,0,0,sol)==0) | ||
{ | ||
printf("No Solution possible\n"); | ||
} | ||
else | ||
{ | ||
display(sol); | ||
} | ||
} | ||
void main() | ||
{ | ||
int maze[N][N]={{1,0,0,0},{1,1,0,1},{0,1,0,0},{1,1,1,1}}; | ||
solve(maze); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class Graph: | ||
def __init__(self,m,v): | ||
self.adj=m | ||
self.V=v | ||
|
||
def mColoring(k,m,g,x): | ||
|
||
while(True): | ||
|
||
x[k]=getNodeColor(k,m,g,x) | ||
if(x[k]==0): | ||
return | ||
if(k==g.V-1): | ||
print(x) | ||
else: | ||
mColoring(k+1,m,g,x) | ||
|
||
def getNodeColor(k,m,g,x): | ||
|
||
while(True): | ||
|
||
x[k]=(x[k]+1)%(m+1) #bcoz any vertex should be assigned from 1 to m | ||
|
||
if(x[k]==0): | ||
return x[k] | ||
|
||
#checking for any collisions of color | ||
for j in range(g.V): | ||
if(g.adj[k][j]==1 and x[k]==x[j]): #same color | ||
break | ||
else: | ||
return x[k] | ||
|
||
|
||
|
||
|
||
def main(): | ||
matrix=[[0,1,1,1], | ||
[1,0,1,0], | ||
[1,1,0,1], | ||
[1,0,1,0] ] | ||
g=Graph(matrix,4) | ||
|
||
m=3 #Number of colors | ||
x=[0 for i in range(g.V)] | ||
mColoring(0,m,g,x) | ||
|
||
if __name__=='__main__': | ||
main() | ||
|
||
|
||
''' | ||
Possible Coloring: | ||
[1, 2, 3, 2] | ||
[1, 3, 2, 3] | ||
[2, 1, 3, 1] | ||
[2, 3, 1, 3] | ||
[3, 1, 2, 1] | ||
[3, 2, 1, 2] | ||
''' |
Oops, something went wrong.