好象是路径的那个INIT函数错了,可不知道具体怎么回事
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<time.h>
typedef struct TREE
{
char data;
struct TREE *ichild;
struct TREE *rchild;
int x;
int y;
}Tree;
struct OUTPUT
{
int x;
int y;
int num;
}s;
int nodeNUM=0;
char way;
char str[3];
void Init();
void Close();
Tree *CreatTree();
Tree *InitTree(int h,int t,int w);
void DrawTree(Tree *t);
void preorder(Tree *t);
void posorder(Tree *t);
void inorder(Tree *t);
void DrawNode(Tree *t,int color);
void clrscr();
void main()
{
Tree *root;
randomize();
root=CreatTree();
Init();
DrawTree(root);
sleep(1);
s.x=100;s.y=300;s.num=1;
preorder(root);
getch();
clrscr();
DrawTree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
inorder(root);
getch();
clrscr();
DrawTree(root);
sleep(1);
s.x=100;s.y=400;s.num=1;
posorder(root);
close();
}
void clrscr()
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
bar(0,20,640,280);
}
Tree *CreatTree()
{
Tree *root;
clrscr();
printf("please input n\n");
printf("1.computer creat\n");
printf("2.people creat\n");
way=getch();
if(way!='2')
way='1';
if(way=='2')
printf("please creat the treen\n");
root=InitTree(1,320,150);
system("pause");
return root;
}
Tree *InitTree(int h,int t,int w)
{
char ch;
int n;
Tree *node;
if(way=='2')
scanf("%c",&ch);
else
{
n=random(5);
if(n==0&&nodeNUM>=3)
ch='.';
else
ch=65+random(25);
}
if(ch=='.')
return NULL;
else
{
if(h==6||nodeNUM==26)
return NULL;
node=(Tree*)malloc(sizeof(Tree));
node->data=ch;
node->x=t;
node->y=h*50;
nodeNUM++;
node->ichild=InitTree(h+1,t-w,w/2);
node->rchild=InitTree(h+1,t+w,w/2);
}return node;
}
void DrawTree(Tree *t)
{
if(t!=NULL)
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(t->x,t->y,9,9);
setcolor(WHITE);
circle(t->x,t->y,10);
sprintf(str,"%c",t->data);
outtextxy(t->x-3,t->y-2,str);
if(t->ichild!=NULL)
{
line(t->x-5,t->y+12,t->ichild->x+5,t->ichild->y-12);
DrawTree(t->ichild);
}
if(t->rchild!=NULL)
{ line(t->x+5,t->y+12,t->ichild->x-5,t->ichild->y-12);
DrawTree(t->rchild);
}
}
}
void DrawNode(Tree *t,int color)
{
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(t->x,t->y,10,10);
setcolor(RED);
sprintf(str,"%c",t->data);
outtextxy(t->x-3,t->y-2,str);
setcolor(color);
outtextxy(s.x,s.y,str);
setcolor(RED);
sprintf(str,"%d",s.num);
outtextxy(t->x-3,t->y-20,str);
s.num++;
sleep(1);
}
void preorder(Tree *t)
{
if(t!=NULL)
{
s.x+=15;
DrawNode(t,GREEN);
preorder(t->ichild);
preorder(t->rchild);
}
}
void inorder(Tree *t)
{if(t!=NULL)
{
inorder(t->ichild);
s.x+=15;
DrawNode(t,YELLOW);
inorder(t->rchild);
}
}
void posorder(Tree *t)
{
if(t!=NULL)
{
posorder(t->ichild);
posorder(t->rchild);
s.x+=15;
DrawNode(t,BLUE);
}
}
void Init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
setcolor(YELLOW);
outtextxy(250,10,"anykey to continue");
setcolor(RED);
outtextxy(20,300,"preorder");
outtextxy(20,350,"inorder");
outtextxy(20,400,"posorder");
}
void Close()
{
getch();
closegraph();
}