#include "graphics.h"
#include "conio.h"
#include "stdlib.h"
#include "dos.h"
#include "time.h"
#include "stdio.h"
#define MX 160
#define MY 260
#define MW 320
#define MH 50
#define BK_COLOR BLACK
#define CHAR_COLOR WHITE
#define C_COLOR BLUE
#define UpKey 72
#define DownKey 80
#define Esc 27
#define Enter 13
#define CX MX+15
#define CY MY+10
#define CW 280
#define CH 36
typedef char elemtype;
typedef struct btnode
{
elemtype data;
struct btnode *lchild,*rchild;
int x,y;
}btnode;
struct output
{
int x;
int y;
int num;
}s;
int nodenum=0;
int way;
char str[3];
int i=0;
void drawnode(btnode *t,int color)
{
setcolor(YELLOW);
setfillstyle(1,YELLOW);
fillellipse(t->x,t->y,10,10);
setcolor(RED);
sprintf(str,"%c",t->data);
settextstyle(0,0,1);
outtextxy(t->x-3,t->y-2,str);
setcolor(color);
outtextxy(s.x,s.y,str);
setcolor(4);
sprintf(str,"%d",s.num);
outtextxy(t->x-3,t->y-20,str);
s.num++;
sleep(1);
}
void drawtree(btnode *t)
{
if(t)
{
setcolor(0);
setfillstyle(1,0);
fillellipse(t->x,t->y,9,9);
setcolor(WHITE);
circle(t->x,t->y,10);
sprintf(str,"%c",t->data);
settextstyle(0,0,1);
outtextxy(t->x-3,t->y-2,str);
if(t->lchild)
{
line(t->x-5,t->y+12,t->lchild->x+5,t->lchild->y-12);
drawtree(t->lchild);
}
if(t->rchild)
{
line(t->x+5,t->y+12,t->rchild->x-5,t->rchild->y-12);
drawtree(t->rchild);
}
}
}
void init(int way)
{
cleardevice();
setcolor(4);
settextstyle(0,0,1);
outtextxy(210,10,"press any key to continue.");
setcolor(2);
if(way==0)
outtextxy(20,350,"Preorder:");
else if(way==1)
outtextxy(20,350,"Inorder:");
else if(way==2)
outtextxy(20,350,"Postorder:");
setcolor(4);
settextstyle(1,0,3);
outtextxy(240,380,"waiting......");
getch();
}
void preorder(btnode *t)
{
if(t)
{
s.x+=15;
drawnode(t,2);
preorder(t->lchild);
preorder(t->rchild);
}
}
void inorder(btnode *t)
{
if(t)
{
inorder(t->lchild);
s.x+=15;
drawnode(t,14);
inorder(t->rchild);
}
}
void postorder(btnode *t)
{
if(t)
{
postorder(t->lchild);
postorder(t->rchild);
s.x+=15;
drawnode(t,1);
}
}
btnode *inittree(int h,int t,int w)
{
char ch;
int n;
btnode *node;
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=(btnode *)malloc(sizeof(btnode));
node->data=ch;
node->x=t;
node->y=h*50;
nodenum++;
node->lchild=inittree(h+1,t-w,w/2);
node->rchild=inittree(h+1,t+w,w/2);
}
return node;
}
btnode *creat(int h,int t,int w)
{
char ch;
btnode *node;
ch=getch();
sprintf(str,"%c",ch);
setcolor(4);
settextstyle(5,0,10);
outtextxy(200+(i++)*10,140,str);
if(ch=='.')
return NULL;
else
{
if(h==6||nodenum==26)
{node=NULL;return;}
node=(btnode *)malloc(sizeof(btnode));
node->data=ch;
node->x=t;
node->y=h*50;
nodenum++;
node->lchild=creat(h+1,t-w,w/2);
node->rchild=creat(h+1,t+w,w/2);
}
return node;
}
void bg_ck()
{
setcolor(BLACK);
setfillstyle(1,0);
bar(0,0,640,290);
}
void c_creat()
{
btnode *root;
cleardevice();
setbkcolor(9);
setcolor(14);
randomize();
root=inittree(1,320,150);
way=0;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
preorder(root);
getch();
bg_ck();
way=1;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
inorder(root);
getch();
bg_ck();
way=2;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
postorder(root);
closegraph();
}
void input()
{
cleardevice();
setbkcolor(9);
setcolor(5);
settextstyle(1,0,2);
outtextxy(200,100,"init the tree:");
setfillstyle(1,3);
bar(200,130,440,160);
}
void p_creat()
{
btnode *root;
cleardevice();
setbkcolor(9);
setcolor(5);
input();
root=creat(1,320,150);
way=0;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
preorder(root);
getch();
bg_ck();
way=1;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
inorder(root);
getch();
bg_ck();
way=2;
init(way);
drawtree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
postorder(root);
closegraph();
}
void setresult()
{
int result;
result=choose_menu(1);
loop:
switch(result)
{
case Esc:
return 0;
case CY:
c_creat();
break;
case CY+MH:
p_creat();
case CY+2*MH:
exit(0);
break;
default:
break;
}
}
void bkground()
{
int i,j;
setcolor(CYAN);
for(i=0;i<60;i++)
{
line(i,0,i,500);
line((getmaxx()-i),0,(getmaxx()-i),500);
}
setcolor(YELLOW);
for(i=60,j=0;i<85;i++,j++)
{
line(i,j,i,500);
line((getmaxx()-i),j,(getmaxx()-i),500);
}
setcolor(WHITE);
rectangle(MX,MY,MX+MW,MY+MH*3);
line(MX,MY+MH,MX+MW,MY+MH);
line(MX,MY+2*MH,MX+MW,MY+2*MH);
line(MX,MY+3*MH,MX+MW,MY+3*MH);
}
void choice(int c_x,int c_y,int color)
{
setfillstyle(1,color);
bar(c_x,c_y,c_x+CW,c_y+CH);
}
void clear_bg()
{
setfillstyle(1,DARKGRAY);
bar(MX,MY-180,MX+MW,MY-130);
}
void main_menu()
{
int i;
settextstyle(0,0,2);
outtextxy(MX+50,MY+10,"Step System!");
outtextxy(MX+50,MY+MH+10,"Help..");
outtextxy(MX+50,MY+2*MH+10,"Exit..");
clear_bg();
settextstyle(0,0,2);
outtextxy(265,100,"welcome!");
}
void option_menu()
{
int i;
settextstyle(0,0,2);
outtextxy(MX+50,MY+10,"computer creat.");
outtextxy(MX+50,MY+MH+10,"people creat.");
outtextxy(MX+50,MY+2*MH+10,"back main_menu..");
clear_bg();
settextstyle(0,0,2);
outtextxy(265,100,"welcome!");
}
int choose_menu(int flag)
{
int x,y;
char C_Key;
x=CX;y=CY;
do
{
kbhit();
C_Key=getch();
if(C_Key==Esc)
return Esc;
else
if(C_Key==Enter)
return y;
else
switch(C_Key)
{
case UpKey:
if(y==CY)
break;
else
{
choice(x,y,BK_COLOR);
y-=MH;
choice(x,y,C_COLOR);
}
break;
case DownKey:
if(y==(CY+2*MH))
break;
else
{
choice(x,y,BLACK);
y+=MH;
choice(x,y,BLUE);
}
break;
default:
break;
}
if(flag==0)
main_menu();
else
if(flag==1)
option_menu();
}while(C_Key!=Esc);
}
void main()
{
int gdriver=DETECT,gmode;
int c_x,c_y,result;
char C_Key;
initgraph(&gdriver,&gmode,"c:\tc");
c_x=CX;c_y=CY;
Begin:
cleardevice();
bkground();
choice(c_x,c_y,BLUE);
main_menu();