如何体现二叉树的遍历算法的动态实现

原问题:如何体现二叉树的遍历算法的动态实现
分类:编程开发 > 最后更新时间:【2017-02-21 18:42:52】

最佳答案

c语言二叉树的建立及其遍历算法动态演示程序

#include <graphics.h>

#include <stdio.h>

#include <stdlib.h>

#include <dos.h>

#include <time.h>

typedefstruct TREE

{

char data;

struct TREE*lchild;

struct TREE *rchild;

int x;

int y;

}Tree;

struct OUTPUT

{

int x;

int y;

int num;

}s;

void HZ16(int x0,int y0,int w,int color,char *s)

{

FILE *fp;

register char buffer[32];

register char str[2];

unsigned long fpos;

register int i,j,k;

fp=fopen("hzk16","r");

while(*s)

{

if(*s<0)

{

str[0]=(*s)-0xa0;

str[1]=*(s+1)-0xa0;

fpos=((str[0]-1)*94+(str[1]-1))*32L;

fseek(fp,fpos,SEEK_SET);

fread(buffer,32,1,fp);

for(i=0;i<16;i++)

for(j=0;j<2;j++)

for(k=0;k<8;k++)

if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)

putpixel(x0+8*j+k,y0+i,color);

s+=2;

x0+=w;

}

else

{

settextstyle(0,0,1);

setcolor(color);

str[0]=*s;str[1]=0;

outtextxy(x0,y0+7,str);

x0+=w-7;

s++;

}

}

fclose(fp);

}

int nodeNUM=0;

char way;

int flag;

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 Midorder(Tree *t);

void Posorder(Tree *t);

void DrawNode(Tree *t,int color);

void ClrScr();

void main()

{

Tree *root;

randomize();

root=CreatTree();

printf("please input n\n");

printf("1.computer auto\n");

printf("2.people \n");

flag=getch();

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;

Midorder(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 tree:");

root=InitTree(1,320,150);

printf("\n");

system("pause");

return root;

}

Tree *InitTree(int h,int t,int w)

{

char ch;

Tree *node;

if(way=='1')

{

if(h==5||nodeNUM==9)

{ return NULL ;}

ch=65+random(25);

node=(Tree*)malloc(sizeof(Tree));

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);

}

if(way=='2')

{

if(h==5||nodeNUM==9)

{ return NULL ;}

ch=getch();

printf("%c",ch);

node=(Tree*)malloc(sizeof(Tree));

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;

}

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->lchild!=NULL)

{

line(t->x-5,t->y+12,t->lchild->x+5,t->lchild->y-12);

DrawTree(t->lchild);

}

if(t->rchild!=NULL)

{

line(t->x+5,t->y+12,t->rchild->x-5,t->rchild->y-12);

DrawTree(t->rchild);

}

}

}

void DrawNode(Tree *t,int color)

{

setfillstyle(SOLID_FILL,color);

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);

if(flag=='2')

{getch();}

Preorder(t->lchild);

Preorder(t->rchild);

}

}

void Midorder(Tree *t)

{

if(t!=NULL)

{

Midorder(t->lchild);

s.x+=15;

DrawNode(t,YELLOW);

if(flag=='2')

{getch();}

Midorder(t->rchild);

}

}

void Posorder(Tree *t)

{

if(t!=NULL)

{

Posorder(t->lchild);

Posorder(t->rchild);

s.x+=15;

DrawNode(t,BLUE);

if(flag=='2')

{getch();}

}

}

void Init()

{

int gd=DETECT,gm;

initgraph(&gd,&gm,"d:\\tc");

cleardevice();

setcolor(YELLOW);

outtextxy(250,10,"anykey to continue");

setcolor(2);

HZ16(20,295,20,2,"先序遍历");

setcolor(14);

HZ16(20,345,20,14,"中序遍历");

setcolor(1);

HZ16(20,395,20,1,"后序遍历");

getch();

}

void Close()

{

getch();

closegraph();

}

最佳答案由网友  牛奶般_纯白  提供
公告: 为响应国家净网行动,部分内容已经删除,感谢网友理解。
11

分享到:

其他回答

暂无其它回答!

    推荐