bfs #include #include #include #include typedef struct t { int value; t *leftson; t *rightson; }tree; class queue { int front; int rear; tree * element[100]; public: queue() { front = rear = -1; } void push(tree *item) { element[++rear]=item; } tree *pop() { return element[++front]; } int isempty(void) { return (rear==front)?1:0; } }; queue q; int bfs(int searchvalue) { if(q.isempty()) { return 0; } else { tree * temp = (q.pop()); printf("\n Current value searched is %d",temp->value); if(temp->value==searchvalue) { printf("\n Match Found"); return 1; } if(temp->leftson!=NULL) q.push(temp->leftson); if(temp->rightson!=NULL) q.push(temp->rightson); if(bfs(searchvalue)==1) return 1; if(bfs(searchvalue)==1) return 1; } return 0; } tree * newtree(int value) { tree *temp = (tree *) malloc(sizeof(tree )); temp->leftson = NULL; temp->rightson = NULL; temp->value = value; return temp; } tree * gettree(tree * base) { int templeftson,temprightson; printf("\n Enter the value for left child & (-1) for null "); scanf("%d",&templeftson); printf("\n Enter the value for right child & (-1) for null "); scanf("%d",&temprightson); if(templeftson!=-1) { base->leftson = newtree(templeftson); printf("\n %d's LEFT CHILD INFO ",base->leftson->value); base->leftson = gettree(base->leftson); } if(temprightson!=-1) { base->rightson = newtree(temprightson); printf("\n %d's RIGHT CHILD INFO ",base->rightson->value); base->rightson = gettree(base->rightson); } return base; } void main() { tree *base ; int temp,searchvalue; clrscr(); printf(" Enter the value for parent & (-1) for null"); scanf("%d",&temp); if(temp!=-1) { base = newtree(temp); base = gettree(base); } clrscr(); printf(" Enter the value to be searched -> "); scanf("%d",&searchvalue); if(base!=NULL) { q.push(base); if(bfs(searchvalue)==0) { printf("\n Match not found"); } } getch(); } dfs #include #include #include #include typedef struct t { int value; t *leftson; t *rightson; }tree; //void PrintDetails(tree * base) { //if(base != null) { int dfs(int searchvalue,tree * base1) { if( base1 == NULL) { return 0; } else { //getch(); printf("\n Current value searched is %d",base1->value); if(base1->value==searchvalue) { printf("\n Match Found"); return 1; } else { if(!((base1->leftson)==NULL)) { //cout<< " c " << (base1->leftson==NULL); if(dfs(searchvalue,base1->leftson)==1){return 1;} } if(!(base1->rightson==NULL)) { //cout<< " d " << (base1->rightson==NULL); if(dfs(searchvalue,base1->rightson)==1){return 1;} } } //else //return 0; } return 0; } tree * newtree(int value) { tree *temp = (tree *) malloc(sizeof(tree )); temp->leftson = NULL; temp->rightson = NULL; //if(temp->leftson==NULL) { //printf("Its null"); } temp->value = value; return temp; } tree * gettree(tree * base) { int templeftson,temprightson; printf("\n Enter value for the left child(-1) for null-> "); scanf("%d",&templeftson); printf("\n Enter value for the right child(-1) for null-> "); scanf("%d",&temprightson); if(templeftson!=-1) { base->leftson = newtree(templeftson); printf("\n %d's Left Child Information",base->leftson->value); base->leftson = gettree(base->leftson); } if(temprightson!=-1) { base->rightson = newtree(temprightson); printf("\n %d's Right Child Information",base->rightson->value); base->rightson = gettree(base->rightson); } return base; } void main() { tree *base ; int temp,searchvalue; clrscr(); printf(" Enter value for Parent the node (-1) for null-> "); //fflush(stdio); scanf("%d",&temp); if(temp!=-1) { base = newtree(temp); base = gettree(base); } //clrscr(); //printf(" The details of the tree are "); //PrintDetails(base); clrscr(); printf(" Enter the value to be searched -> "); scanf("%d",&searchvalue); if(dfs(searchvalue,base)==0) { printf("\n Match not found"); } //bfs(searchvalue,base); getch(); } water #include #include #include typedef struct JUG { int x; int y; }jug; int Solution(jug jug1, int maxnodes) { jug temp; if(maxnodes<0) return 0; if(jug1.x0) { temp.x=0; temp.y = jug1.y; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if(jug1.y<3) { temp.x=jug1.x; temp.y = 3; if(Solution(temp,maxnodes-1)==1) { printf(" \nx = %d , y = %d",temp.x,temp.y); return 1; } } if(jug1.y>0) { temp.x=jug1.x; temp.y = 0; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d , y = %d",temp.x,temp.y); return 1; } } if((jug1.x+jug1.y)>=4&&jug1.y>0) { temp.x=4; temp.y = jug1.y-(4-jug1.x); if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if((jug1.x+jug1.y)>=3&&jug1.x>0) { temp.x=jug1.x-(3-jug1.y); temp.y = 3; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if(((jug1.x+jug1.y)<=4)&&temp.y>0) { temp.x=jug1.x+jug1.y; temp.y = 0; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if((jug1.x+jug1.y)<=3&&jug1.x>0) { temp.x=0; temp.y = jug1.x+jug1.y; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if(jug1.y==2&&jug1.x==0) { temp.x=2; temp.y = 0; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if(jug1.x==2&&jug1.y>0) { temp.x=jug1.x; temp.y = 0; if(Solution(temp,maxnodes-1)==1) { printf("\n x = %d, y = %d",temp.x,temp.y); return 1; } } if(jug1.x==2&&jug1.y==0) { temp.x=2; temp.y =0; printf("\n Success with \n x = %d, y = %d",temp.x,temp.y); return 1; } return 0; } void main() { clrscr(); jug jug1; int a,b; cout<<" ENTER INPUT x= "; cin>>a; cout<<" ENTER INPUT y= "; cin>>b; jug1.x = a; jug1.y = b; clrscr(); Solution(jug1,9); getch(); }