c7c7
BAN USER- 0of 0 votes
Answersfind path from root to leaf nodes in iterative fashion.
- c7c7 in India| Report Duplicate | Flag | PURGE
Algorithm
# include<stdio.h>
# include<conio.h>
# include<math.h>
int sieve(int factor,int c[],int size,int a)
{
//i need to chop out all factors of i in the array
//-1 for the number whose factor doesn't exist uptil and 1 for a non-prime number
int i;
if(a==1)
i=1;
for(i=0;i<size;i++)
{
if(c[i]==-1&&(a+i)!=factor&&!((i+a)%factor))
c[i]=1;
}//end of for loop
return 0;
}//end of sieve function
int print(int a ,int b)
{
int size=b-a+1,c[size],i;
for(i=0;i<size;i++)
c[i]=-1;
if(a==1)
c[0]=1;//coz one is not a prime number
for(i=2;i<=(int)sqrt(b);i++)
{
sieve(i,c,size,a);
}//end of for loop
for(i=0;i<size;i++)
{
if(c[i]==-1)
printf("%d\n",i+a);
}
return 0;
}//end of function
int main()
{
int a,b;
printf("this program is made by considering that we have to include a and b as well\n");
printf("enter the lower bound");
scanf("%d",&a);
printf("enter the upper bound");
scanf("%d",&b);
print(a,b);
printf("returned from the function");
getch();
return 0;
}//end of main function
basically in this question we have suppose a ->zeroes and b->ones then we need to permute then such that,possible arrangements are n!/(a!*b!)..
so can anyone help me how to code that thing..as i want to print non-repeated permutations..i m unable to find its solution..
# include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int info;
struct node*link;
}*NODE;
int display(NODE head)
{
while(head!=NULL)
{
printf("%d",head->info);
head=head->link;
}
return 0;
}//end of display
NODE exchange(int n,NODE head)
{
NODE prev,temp,p,q,r,s;
int count=1,i,c=1;
if(head==NULL)
return head;
//find the number of modes
for(p=head;p->link!=NULL;p=p->link)
{
prev=p;
count++;
}
printf("count%d",count);//debugging
if(n>=count)
{
puts("you exceed the number of nodes present in the list");
exit(1);
}
//when i will come out of this loop p will point to the last node
if(n==1)
{//we need to swap first and last node
prev->link=head;
p->link=head->link;
head->link=NULL;
head=p;
}
else
{
//need to find those nodes
i=count-n;
p=head;
while(c<n||c<=i)
{
if(c<n)
temp=p;
if(c<=i)
prev=p;
p=p->link;
c++;
}//end of while loop
//we need to swap q and r
q=temp->link;
r=prev->link;
s=q->link;
temp->link=r;
if(prev!=q)
{//no condition of loop arises
prev->link=q;
q->link=r->link;
r->link=s;
}
else
{
q->link=r->link;
r->link=q;
}
}//end of else
return head;
}//end of function
NODE insert(int n)
{
NODE t,start=NULL;
int i,item;
for(i=0;i<n;i++)
{
t=(NODE)malloc(sizeof(struct node));
if(t==NULL)
{
puts("NOT ENUF SPACE");
return NULL;
}
printf("enter info item");
scanf("%d",&item);
t->info=item;
t->link=start;
start=t;
}//end of for loop
return start;
}//end of insert
int main()
{
int n,m;
NODE head;
printf("enter the number of elements you want to enter");
scanf("%d",&n);
head=insert(n);
printf("enter the n to be exchanged");
scanf("%d",&m);
head=exchange(m,head);
printf("returned");
display(head);
getch();
return 0;
}//end of main function
PIVOT IS THE ELEMENT WHICH IS LESSER THEN IN LEFT ELEMENT AND RIGHT ELEMENT AS WELL.. so we need to use that...and recurse..where as problem will be divided in three parts when we have one element left then it is the pivot and when we are left with 2 elements then minimum of that...and when of more then three then we need to check the above stated condition and accordingly move to left or right part
- c7c7 October 05, 2012keep a variable c and every time u push it..then increment its priority such that..the element with high priority will be removed first ..so the element which is pushed in the end will have highest value of c so it will be the first element to be popped off.....
implement priority queue using linked list..and priority will be the time of insertion which we will track with c variable..
FULL WORKING CODE:
# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
struct node
{
struct node*left;
struct node *right;
int info;
};
typedef struct node* NODEPTR;
int top=-1;
NODEPTR stack[50];
int stackempty()
{
if(top==-1)
return 1;
return 0;
}
int push(NODEPTR ptr)
{
if(top==49)
{
puts("stack overflow");
return 0;
}
top++;
stack[top]=ptr;
return 0;
}//end of push
NODEPTR pop()
{
NODEPTR p;
if(top==-1)
{
puts("stack empty");
return NULL;
}
else
{
p=stack[top];
top--;
return p;
}
}//end of pop function
int print_stack()
{
int i=top;
while(i>=0)
{
printf("%d ",stack[i]->info);
i--;
}
printf("\n");
}//end ofprint stack function
NODEPTR insert(int n)
{
NODEPTR temp,start,p,q;
int i;
for(i=0;i<n;i++)
{
temp=(NODEPTR)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
printf("enter item\n");
scanf("%d",&(temp->info));
if(i==0)
start=temp;
else
{
p=start;
q=start;
while(p!=NULL)
{
q=p;
if(p->info>=temp->info)
p=p->left;
else
p=p->right;
}//end of while loop
if(temp->info>q->info)
q->right=temp;
else
q->left=temp;
}//end of else
}//end of for loop
return start;
}//end of insertion in tree
int print_path(NODEPTR root)
{
NODEPTR ptr,q;
ptr=root;
while(1)
{
while(ptr->left!=NULL)
{
push(ptr);
ptr=ptr->left;
}//end of inner loop
while(ptr->right!=NULL&&ptr->left==NULL)
{
push(ptr);
ptr=ptr->right;
}//END OF LOOP
if(ptr->left==NULL)
{
printf("%d",ptr->info);
print_stack();
q=ptr;
if(stackempty())
return 0;
ptr=pop();
if(ptr->right==NULL||ptr->right==q)
{
while(ptr->right==NULL||ptr->right==q)
{
q=ptr;
if(stackempty())
return 0;
ptr=pop();
}
push(ptr);
ptr=ptr->right;
}//end of if
else
{
push(ptr);
ptr=ptr->right;
}
}//end of if loop
else
{
push(ptr);
ptr=ptr->left;
}
}//end of while loop
}//end of print_path
int main()
{
NODEPTR root;
int n;
printf("enter the number of nodes you want to enter in the tree");
scanf("%d",&n);
root=insert(n);//this is inserting as bst ..but need to insert BT
printf("inserted");
print_path(root);
printf("returned");
getch();
return 0;
}//end of main function
//without the inorder traversal...
int find_max(NODEPTR root)
{
//using level order traversal we can find maximum
}//end of find max
int find_min(NODEPTR root)
{
//using level order traversal we can find minimum
}//end of find minimum
int check(NODEPTR root,int*max,int*min)
{
if(root==NULL)
return 1;
if(root->data>find_max(root->left))
{
if(root->data<find_min(root->right))
{
return(check(root->left)&&check(root->right));
}
else
return 0;
}
return 0;
}
CAN ANYONE SUGGEST SOMETHING BETTER SUCH THAT WE DON'T HAVE TO CALCULATE MAX AND MINIMUM FOR A NOE AGAIN AND AGAIN
# include<stdio.h>
- c7c7 November 22, 2012# include<stdlib.h>
# include<conio.h>
# include<math.h>
int compare(const void*a,const void*b)
{
return(*(int*)a-*(int*)b);
}//end of compare used by qsort
int find_triplet(int *a,int n)
{
int i,k,j,m,sum,flag=1;
//first modify the array if it can not be modified then create its copy first
for(i=0;i<n;i++)
a[i]=a[i]*a[i];
qsort(a,n,sizeof(int),compare);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
for(i=0;i<n;i++)
{
k=a[i];
j=0;
m=n-1;
while(j<m)
{
sum=a[j]+a[m];
if(sum==k&&j!=i&&m!=i)
{
flag=0;
puts("found triplets");
printf("%d %d %d",(int)sqrt(k),(int)sqrt(a[j]),(int)sqrt(a[m]));
printf("\n");
j++;m--;
}
else if(sum>k)
m--;
else
j++;
}//end of while
}//end of for loop
if(flag==1)
puts("no such triplet occurs");
return 0;
}
int main()
{
int n,i,*a;
printf("enter the number of elements you want to enter in the array");
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
if(a==NULL)
{
puts("not enough memory to store array elements");
}
else
{
for(i=0;i<n;i++)
{
printf("Enter element:");
scanf("%d",&a[i]);
}//end of for loop
find_triplet(a,n);
}
getch();
return 0;
}//end of main