#include <iostream>
#include <fstream>
using namespace std;
int sum[100];
struct node
{
int data;
node *left,*right;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=NULL;
t->right=NULL;
}
else if(a>t->data)
{
create(t->right,a);
}
else
{
create(t->left,a);
}
}
int max(int a ,int b)
{
if(a>b)
return a;
return b;
}
bool print(node *t,int k)
{
if(!t)
return false;
if(t->data==k)
return true;
if(print(t->left,k)||(print(t->right,k)))
{
cout<<t->data<<" ";
return true;
}
return false;
}
int main()
{
for(int i=0;i<100;i++)
{
sum[i]=0;
}
ifstream fin;
int k;
fin.open("/home/pawan/Desktop/ds/input.txt");
if(!fin.is_open())
cout<<"file not opened\n";
node *t;
t=NULL;
int a,a1;
cin>>a;
while(a!=-1)
{
create(t,a);
cin>>a;
}
fin.close();
cout<<"enter the key you want to print ancestor ";
int n;
cin>>n;
print(t,n);
return 0;
}
#include <fstream>
using namespace std;
int sum[100];
struct node
{
int data;
node *left,*right;
};
void create(node *&t,int a)
{
if(t==NULL)
{
t=new node;
t->data=a;
t->left=NULL;
t->right=NULL;
}
else if(a>t->data)
{
create(t->right,a);
}
else
{
create(t->left,a);
}
}
int max(int a ,int b)
{
if(a>b)
return a;
return b;
}
bool print(node *t,int k)
{
if(!t)
return false;
if(t->data==k)
return true;
if(print(t->left,k)||(print(t->right,k)))
{
cout<<t->data<<" ";
return true;
}
return false;
}
int main()
{
for(int i=0;i<100;i++)
{
sum[i]=0;
}
ifstream fin;
int k;
fin.open("/home/pawan/Desktop/ds/input.txt");
if(!fin.is_open())
cout<<"file not opened\n";
node *t;
t=NULL;
int a,a1;
cin>>a;
while(a!=-1)
{
create(t,a);
cin>>a;
}
fin.close();
cout<<"enter the key you want to print ancestor ";
int n;
cin>>n;
print(t,n);
return 0;
}
No comments:
Post a Comment