Friday, August 9, 2013

Recursive algorithms

What is an recursive algorithm?

A recursive algorithm calls itself which usually passes the return value as a parameter to the algorithm again. This parameter is the input while the return value is the output.

What is an recursive algorithm?

Recursive algorithm is a method of simplification that divides the problem into sub-problems of the same nature. The result of one recursion is the input for the next recursion. The repletion is in the self-similar fashion. The algorithm calls itself with smaller input values and obtains the results by simply performing the operations on these smaller values. Generation of factorial, Fibonacci number series are the examples of recursive algorithms.







Que- write a program to construct B.S.T or it`s traversing algorithm in recursive way.

              #include<stdio.h>
              #include<conio.h>
              #include<process.h>
          
               struct BST*LC;

               int data;
               struct BST*RC;

               struct BST*start=NULL;
               void create( );
               void insert (int d);
               void inorder (struct BST*tmp);
               void preorder(struct BST*tmp);
               void postorder (struct BST*tmp);
               void main( )
               {
               int ch;
               while(1)
               {
               printf("press 1 for create\n");
               printf("press 2 for insert\n");
               printf("press 3 for inorder traversing\n");
               printf("press 4 for preorder traversing\n");
               printf("press 5 for postorder traversing\n");
               printf("press 6 for exit\n");
               printf("enter your choice\n")
               scanf("%d",&ch);
               switch(ch)
               {
               case 1:
               create();
               break;
               case 2:
               printf("enter data\n");
               scanf("%d",&d);
               insert(d);
               break;
               case 3:
              inorder(start);
              getch();
              break;
              case 4:
             preorder(start);
             getch();
             break;
             case 5:
            postorder(start);
            getch();
            break;



void creat()
{
struct BST*tmp;
if(start==NULL)
{
tmp=(struct BST*)malloc(sizeof(struct BST));
tmp-->LC=tmp-->RC=NULL;
printf("enter value\n");
scanf("%d",&tmp-->data);
start=tmp;
}
}


void insert(int d)
{
struct BST* tmp,*tmp1;
tmp=start;
while(1)
{
if(d==tmp-->data)
{
printf("duplicate not allow\n");
return;
}

if(d>tmp-->data)
{
if(tmp-->RC!= NULL)
{
tmp=tmp-->RC;
}
else
{
tmp1=(struct BST*)malloc(sizeof(struct BST));
tmp-->RC=tmp;
tmp-->RC=tmp1-->LC=NULL;
tmp-->data=d;
return;
}
else
{
if(tmp-->LC!=NULL)
{
tmp=tmp-->LC;
}
else
{
tmp1=(struct BST*)malloc(sizeof(struct BST));
tmp1-->LC=tmp1-->RC=NULL;
tmp1-->data=d;
tmp-->LC=tmp;
return;
}
}
}
}

No comments:

Post a Comment