📄 hafuman.txt
字号:
赫夫曼树课程设计
这可是我的课程设计呀 亲自验证过 可以画树的
#include <stdio.h>
#include <graphics.h>
void page()
{ int u;
int gdriver, gmode;
gdriver=DETECT;
initgraph(&gdriver, &gmode, "c:\\");
setbkcolor(BLUE);
rectangle(10,2,635,470);
setcolor(BROWN);
setlinestyle(0,0,1);
circle(320,38,20);
outtextxy(315,35,"ha");
setcolor(BROWN);
setlinestyle(0,0,1);
line(295,59,285,70);
line(345,59,360,70);
circle(265,95,20);
outtextxy(260,92,"fu");
setcolor(BROWN);
setlinestyle(0,0,1);
line(250,120,240,130);
line(280,120,290,130);
circle(375,95,20);
outtextxy(370,92,"man");
setcolor(BROWN);
setlinestyle(0,0,1);
line(355,120,345,130);
line(395,120,405,130);
circle(235,160,20);
outtextxy(230,158,"ke");
setcolor(BROWN);
setlinestyle(0,0,1);
/* line(215,185,205,195);*/
circle(297,160,20);
outtextxy(280,158,"cheng");
circle(352,160,20);
outtextxy(348,158,"she");
circle(413,160,20);
outtextxy(410,158,"ji");
setcolor(LIGHTMAGENTA);
outtextxy(310,325,"ZUOZHE:XiaoBowen");
getch();
for(u=0;u<=10000&&!kbhit();u++) /*检查当前按下的键*/
if(u>14)
setcolor(u/14);
else
setcolor(u);
settextstyle(1,0,4);
outtextxy(200,380,"Press any key !");
cleardevice(); /*清除图形屏幕*/
}
typedef struct
{ int weight;
int parent,lchild,rchild;
int l,r;
}huffnode;
huffnode ht[50];
char name[50];
int select(int x,int y)/*找叶子节点中权最小的*/
{ int a=10000;
int i;
for(i=1;i<=x;i++)
{ if(ht[i].parent==0&&ht[i].weight<=a)
{ a=ht[i].weight;
y=i;
}
}
return y;
}
char *z;
void drawcircle(int rx,int ry,int ccolor,int key);
void creat()
{ int i,n,m;
int c,e,f,j;
int *huffcd;
rectangle(0,2,635,470);
printf("\nInput the leaf's number n:");
scanf("%d",&n);
m=2*n-1;
for(i=1;i<=n;i++)
{ getchar();
printf("The %d number's weight:",i);
scanf("%d",&ht[i].weight);
ht[i].parent=ht[i].lchild=ht[i].rchild=0;
}
cleardevice();
rectangle(10,2,635,470);
for(;i<=m;i++)
{ ht[i].weight=ht[i].parent=ht[i].lchild=ht[i].rchild=0;
}
for(i=n+1;i<=m;i++)
{ int s1=0,s2=0;
s1=select(i-1,s1);
ht[s1].parent=i;
ht[i].lchild=s1;
s2=select(i-1,s2);
ht[s2].parent=i;
ht[i].rchild=s2;
ht[i].weight=ht[s1].weight+ht[s2].weight;
}
for(i=n+1;i<=m;i++)
{ for(j=n+1;j<=m;j++)
{ if(ht[j].lchild==i||ht[j].rchild==i)
ht[i].parent=j;
}
}
huffcd=(int *)malloc((n-1)*sizeof(int));
e=n-1;
for(i=1;i<=n;i++)
{ for(c=i,f=ht[i].parent;f!=0;c=f,f=ht[f].parent)
{ if(ht[f].lchild==c)
huffcd[--e]=0;
else
huffcd[--e]=1;
}
setcolor(14);
outtextxy(100,50,"bian ma shi");
setcolor(4);
itoa(i,z,10);
outtextxy(200+50*i-50,50,z);
outtextxy(205+50*i-50,50,":");
delay(10000);
for(;e<n-1;e++)
{ itoa(huffcd[e],z,10);
outtextxy(210+e*7+50*i-50,50,z);
}
}
getchar();
setcolor(RED);
ht[m].l=300;
ht[m].r=200;
for(j=m;j>m-1;j--) /*控制第二成节点的坐标*/
{ for(i=1;i<m;i++)
{ if(ht[i].parent==j)
{ if(ht[j].lchild==i)
{ ht[i].l=ht[j].l-70;
ht[i].r=ht[j].r+60;
}
else
{ ht[i].l=ht[j].l+70;
ht[i].r=ht[j].r+60;
}
}
}
}
for(c=1,j=m-1;j>n;j--,c++) /*确定叶子节点以上到第二成以下接点的坐标*/
{ for(i=1;i<m;i++)
{ if(ht[i].parent==j)
{ if(ht[j].lchild==i)
{ ht[i].l=ht[j].l-50+c*5;
ht[i].r=ht[j].r+60;
}
else
{ ht[i].l=ht[j].l+50-c*5;
ht[i].r=ht[j].r+60;
}
}
}
}
for(j=n+1;j<=m;j++)
{ for(i=1;i<m;i++)
{ if(ht[i].parent==j)
{ if(ht[j].lchild==i)
{ drawcircle(ht[i].l,ht[i].r,10,ht[i].weight);
}
else
{ drawcircle(ht[i].l,ht[i].r,10,ht[i].weight);
}
}
line(ht[ht[j].lchild].l+10,ht[ht[j].lchild].r-8,ht[j].l-10,ht[j].r+8);
line(ht[ht[j].rchild].l-10,ht[ht[j].rchild].r-8,ht[j].l+10,ht[j].r+8);
delay(1500);
drawcircle(ht[j].l,ht[j].r,10,ht[j].weight);
delay(2500);
}
}
getch();
}
void drawcircle(int rx,int ry,int ccolor,int key)
{ char *y;
setcolor(ccolor);
sprintf(y,"%d",key);
outtextxy(rx-5,ry-2,y);
circle(rx,ry,12);
floodfill(rx,ry,ccolor);
setcolor(MAGENTA);
settextstyle(1,0,2);
}
main()
{ char *z;
page();
cleardevice();
setbkcolor(BLUE);
setfillstyle(1,BLUE);
floodfill(300,20,WHITE);
setfillstyle(1,BLUE);
floodfill(20,450,WHITE);
creat();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -