سلام دوستان خوبم
اين يك برنامه سي هست كه مال درخت ها هست .
سوال امتحانيمون هم هست !
ميشه برام تشريح كنيد به صورت خط به خط كه اين چيكار مي كنه ؟
آخه ميخواستم حفظش كنم ديدم نميشه !
هم upload مي كنم هم اينجا مي نويسم .
لينك دانلود :
http://up.iranblog.com/images/is682hszw2d5yzeb220y.zip
ممنون
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include <iostream.h>
struct tree{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree node;
void main()
{
int search(node *t,int x);
void bts(node *t,int x);
void inorder(node *t);
int x,i=1;
node *t;
clrscr();
printf("enter root");
scanf("%d",&x);
t=(node*)malloc(sizeof(node));
t->data =x;
t->right=NULL;
t->left=NULL;
while(i<10)
{
scanf("%d",&x);
bts(t,x);
i=i+1;
}
inorder(t);
cout<<"enter number for search";
cin>>x;
if(search(t,x)==1) cout<<"data found"; else cout<<"Not found";
getch();
}
int search(node *t,int x)
{
node *s,*q,*p;
s=t;
while ( (s!=NULL) && (s->data!=x) )
{
if(s->data>x)
{
q=s;s=s->left;
}else{q=s;s=s->right;}
}
if (s->data==x) return 1;
else return 0;
}
void bts(node *t,int x)
{
node *p,*q,*s;
p=(node*)malloc(sizeof(node));
p->data =x;
p->right=NULL;
p->left=NULL;
s=t;
while ( (s!=NULL) && (s->data!=x) )
{
if(s->data>x)
{
q=s;s=s->left;
}else{q=s;s=s->right;}
}
if(q->data>x) q->left=p;
else q->right=p;
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d",t->data);
inorder(t->right);
}
}
اين يك برنامه سي هست كه مال درخت ها هست .
سوال امتحانيمون هم هست !
ميشه برام تشريح كنيد به صورت خط به خط كه اين چيكار مي كنه ؟
آخه ميخواستم حفظش كنم ديدم نميشه !
هم upload مي كنم هم اينجا مي نويسم .
لينك دانلود :
http://up.iranblog.com/images/is682hszw2d5yzeb220y.zip
ممنون
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include <iostream.h>
struct tree{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree node;
void main()
{
int search(node *t,int x);
void bts(node *t,int x);
void inorder(node *t);
int x,i=1;
node *t;
clrscr();
printf("enter root");
scanf("%d",&x);
t=(node*)malloc(sizeof(node));
t->data =x;
t->right=NULL;
t->left=NULL;
while(i<10)
{
scanf("%d",&x);
bts(t,x);
i=i+1;
}
inorder(t);
cout<<"enter number for search";
cin>>x;
if(search(t,x)==1) cout<<"data found"; else cout<<"Not found";
getch();
}
int search(node *t,int x)
{
node *s,*q,*p;
s=t;
while ( (s!=NULL) && (s->data!=x) )
{
if(s->data>x)
{
q=s;s=s->left;
}else{q=s;s=s->right;}
}
if (s->data==x) return 1;
else return 0;
}
void bts(node *t,int x)
{
node *p,*q,*s;
p=(node*)malloc(sizeof(node));
p->data =x;
p->right=NULL;
p->left=NULL;
s=t;
while ( (s!=NULL) && (s->data!=x) )
{
if(s->data>x)
{
q=s;s=s->left;
}else{q=s;s=s->right;}
}
if(q->data>x) q->left=p;
else q->right=p;
}
void inorder(node *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d",t->data);
inorder(t->right);
}
}