#include <iostream>
#include <fstream>
using namespace std;
int sum[100];
struct node
{
int data;
node *left,*right;
};
void print(node *t)
{
if(t!=NULL)
{
print(t->left);
print(t->right);
cout<<t->data<<" ";
}
}
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);
}
}
void atk(node *t,int k,int c)
{
if(t==NULL)
return ;
if(k==c)cout<<t->data<<" ";
atk(t->left,k,c+1);
atk(t->right,k,c+1);
}
int main()
{
int k=2;
node *t;
t=NULL;
int a[]={10,6,4,7,14,12,16,1,2,34,23,-1};
int i=0;
while(a[i]!=-1)
{
create(t,a[i]);
i++;
}
print(t);
cout<<endl;
atk(t,k,0);
return 0;
}
#include <fstream>
using namespace std;
int sum[100];
struct node
{
int data;
node *left,*right;
};
void print(node *t)
{
if(t!=NULL)
{
print(t->left);
print(t->right);
cout<<t->data<<" ";
}
}
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);
}
}
void atk(node *t,int k,int c)
{
if(t==NULL)
return ;
if(k==c)cout<<t->data<<" ";
atk(t->left,k,c+1);
atk(t->right,k,c+1);
}
int main()
{
int k=2;
node *t;
t=NULL;
int a[]={10,6,4,7,14,12,16,1,2,34,23,-1};
int i=0;
while(a[i]!=-1)
{
create(t,a[i]);
i++;
}
print(t);
cout<<endl;
atk(t,k,0);
return 0;
}
No comments:
Post a Comment