Showing posts with label recursion in data structure. Show all posts
Showing posts with label recursion in data structure. Show all posts
Monday, 12 February 2018
how to create tree and display all its elements
#include<iostream>
using namespace std;
struct tree
{
int num;
tree* left,*right;
}*root;
tree * create(int n)
{
tree *t= new tree;
t->num=n;
t->left=t->right=NULL;
return t;
}
tree * add(tree * r,int nu)
{
if(r==NULL)
{
r= create(nu);
}
else if(nu<=r->num)
{
r->left= add(r->left,nu);
}
else
r->right=add(r->right,nu);
return r;
}
void disply(tree * t)
{
if(t==NULL)
{
return;
}
else
{
cout<<t->num<<" ";
disply(t->left);
disply(t->right);
}
}
int main()
{
root==NULL;
int n,a;
while(1){
cout<<"Enter 1 for add Enter 2 for display"<<endl;
cin>>n;
switch(n)
{
case 1:
cout<<"Enter the number for add"<<endl;
cin>>a;
root=add(root,a);
break;
case 2:
disply(root);
cout<<endl;
break;
}
}
}
how to sum of numbers using recursion
#include<iostream>
using namespace std;
int sum(int num)
{
if(num==0)
return 0;
else return num+sum(num-1);
}
int main()
{
int n;
cout<<"Enter any number please"<<endl;
cin>>n;
cout<<"factorial is "<<sum(n)<<endl;
}
Tuesday, 9 January 2018
recursion in data stucture factorial of a number
#include<iostream>
using namespace std;
int fact(int num)
{
if(num==0)
return 1;
else return num*fact(num-1);
}
int main()
{
int n;
cout<<"Enter any number please"<<endl;
cin>>n;
cout<<"factorial is "<<fact(n)<<endl;
}
using namespace std;
int fact(int num)
{
if(num==0)
return 1;
else return num*fact(num-1);
}
int main()
{
int n;
cout<<"Enter any number please"<<endl;
cin>>n;
cout<<"factorial is "<<fact(n)<<endl;
}
Subscribe to:
Posts (Atom)