این برنامه یک درخت دودویی را رسم نموده و ان را پیمایش می کند
#include<conio.h>
#include<stdio.h>
#include <alloc.h>
#include <graphics.h>
#include<dos.h>
#include <stdlib.h>
struct tree{
int data;
struct tree *left;
struct tree *right;
};
struct tree *start=NULL,*ptr=NULL,*newp=NULL;
int a1,a2,i;
char help[10];
void tahaye(void);
int addnode(struct tree *test);
int sum1(struct tree *tes);
int showtree(struct tree *test1,int x,int y,int xy);
int order(struct tree *test2,int,int,int,int);
//************************* main program **********************************
void main()
{
tahaye();
textbackground(1);
clrscr();
printf("Enter node number from tree<1 to 20>:");
scanf("%d",&a1);
if((a1>20)||(a1<1))
{
printf("the number out rang......press any key for quit...");
getch();
}
for(i=1;i<=a1;i++)
{
printf("Enter number[%d]:",i);
scanf("%d",&a2);
if(i==1)
{
start=(tree *)malloc(sizeof(tree));
start->data=a2;
start->left=NULL;
start->right=NULL;
}
else
{
ptr=start;
addnode(start);
}
}
int x1=DETECT,y1;
initgraph(&x1,&y1,"c:\\tc\\bgi");
setbkcolor(15);
showtree(start,2,1,1);
outtextxy(30,430,"press any key for inorder");
getch();
outtextxy(30,30,"inorder");
order(start,2,1,1,1);
bar(20,420,300,440);
outtextxy(30,430,"press any key for preorder");
getch();
bar(20,20,100,40);
outtextxy(30,30,"preorder");
order(start,2,1,1,2);
bar(20,420,300,440);
outtextxy(30,430,"press any key for postorder");
getch();
bar(20,20,100,40);
outtextxy(30,30,"postorder");
order(start,2,1,1,3);
bar(20,420,300,440);
setcolor(1);
outtextxy(30,430,"press any key for quit...");
getch();
closegraph();
}
//***************************** tahaye subprogram ***************************
void tahaye()
{
int x1=DETECT,y1;
initgraph(&x1,&y1,"c:\\tc\\bgi");
setbkcolor(1);
setcolor(15);
outtextxy(30,30,"help:");
outtextxy(30,50," 1)Max node=20 & Min node=1");
outtextxy(30,70," 2)read=inorder,preorder,postorder");
outtextxy(30,430,"press any key for start...");
getch();
closegraph();
}
//***************************** addnode subprogram **************************
int addnode(struct tree *test)
{
if((test->left==NULL)||(test->right==NULL))
{
newp=(tree *)malloc(sizeof(tree));
newp->data=a2;
newp->left=NULL;
newp->right=NULL;
if(test->left==NULL)
test->left=newp;
else
test->right=newp;
newp=NULL;
return(0);
}
if(sum1(test->right)+1<sum1(test->left))
addnode(test->right);
else
addnode(test->left);
return 0;
}
//**************************** sum1 subprogram ******************************
int sum1(struct tree *tes)
{
if (tes==NULL)
return(0);
return(sum1(tes->left)+sum1(tes->right)+1);
}
//***************************** showtree subprogram ************************
int showtree(struct tree *test1,int x,int y,int xy)
{
if(test1==NULL)
return(0);
setcolor(1);
circle(640/x*xy,50*y,10);
setcolor(4);
itoa(test1->data,help,10);
outtextxy(640/x*xy+10,50*y,help);
if(test1->left!=NULL)
line(640/x*xy,50*y+10,640/(x*2)*(xy*2-1),50*(y+1)-10);
showtree(test1->left,x*2,y+1,xy*2-1);
if(test1->right!=NULL)
line(640/x*xy,50*y+10,640/(x*2)*(xy*2+1),50*(y+1)-10);
showtree(test1->right,x*2,y+1,xy*2+1);
return 0;
}
//******************************** order subprogram *************************
int order(struct tree *test1,int x,int y,int xy,int e)
{
if(test1==NULL)
return(0);
int i;setcolor(e*2);
if(e==2)
{
for(i=1;i<=9;i++)
circle(640/x*xy,50*y,i);
delay(1000);
}
order(test1->left,x*2,y+1,xy*2-1,e);
if(e==1)
{
for(i=1;i<=9;i++)
circle(640/x*xy,50*y,i);
delay(1000);
}
order(test1->right,x*2,y+1,xy*2+1,e);
if(e==3)
{
setcolor(14);
for(i=1;i<=9;i++)
circle(640/x*xy,50*y,i);
delay(1000);
}
return 0;
}
۶۰ تا ارر میده !!!!!!!!!!
سلام دوست عزیز.
شاید شما از کامپایلر مناسب استفاده نمی کنید.
این برنامه رو باید با Turbo C++ 3.0 DOS اجرا کنید.
در ضمن باید رو BGI قرار داشته باشه.
اگه مشکلتون حل نشد می تونید با میل زیر مکاتبه کنید
h.abs1367@gmail.com