📄 btrechar.h
字号:
{
bptrlink p;
int k,n,l,sg;
n=0;
l=8*strlen(sptr->ptrname) / 2;
k=0;
if (sp!=NULL){
p=frstbptr;
while ((p!=NULL)){
if ((p->ptr==sp) && (strcmp(p->ptrname,sptr->ptrname))) n=n+1;
p=p->next;
k=k+1;
}
// if (sptr->x>0) ptrcases(sptr,l);
sptr->ptr=sp;
sptr->num=n % 4+1;
switch (sptr->num){
case 1:{sptr->x=sp->x-l;sptr->y=sp->y-36;break;}
case 2:{sptr->x=sp->x-l;sptr->y=sp->y+28;break;}
case 3:{sptr->x=sp->x-2*l-28;sptr->y=sp->y-4;break;}
case 4:{sptr->x=sp->x+28;sptr->y=sp->y-4;break;}
}
ptrcases(sptr,l);
}else{
sptr->x=0;
sptr->y=0;
sptr->ptr=NULL;
}
}
void copy_grbt_bitre(grbitre grt,bitre &t)
{
if (grt==NULL) t=NULL;
else{
t=(bitre)malloc(sizeof(struct bnode));
t->data=grt->data;
t->ltag=0;
t->rtag=0;
copy_grbt_bitre(grt->lchild,t->lchild);
copy_grbt_bitre(grt->rchild,t->rchild);
}
}
void filfild(grbitre t,int orxor)
{
putimage(t->x+1,t->y+1,chldfild,orxor);
}
void firfild(grbitre t,int orxor)
{
putimage(t->x+ld+dd+1,t->y+1,chldfild,orxor);
}
void fidfild(grbitre t,int orxor)
{
putimage(t->x+ld+1,t->y+1,datafild,orxor);
}
void fifild(grbitre t,int orxor,ldr ll)
{
switch (ll){
case lt:filfild(t,orxor);break;
case r:firfild(t,orxor);break;
case d:fidfild(t,orxor);break;
}
}
void disnodedat(grbitre t)
{
string ss;
strcpy(ss,conv(t->data));
fidfild(t,0);
fidfild(t,1);
outtextxy(t->x+w / 2,t->y+hd / 2,ss);
fidfild(t,1);
}
void dismenu(ldr ll)
{
const string space=" ";
char ss[255],s[255],s1[255],mss[255],ms1[255],stemp[255];
setfillstyle(1,0);
rectangle(1,1,getmaxx(),startrow-2);
strcpy(s,"^w : return , ");
bar(2,2,getmaxx()-2,startrow-3);
switch (ll){
case lt:{
strcpy(s1," ->:data, down, <-:lchild ");
strcpy(ss," lchild field");
strcpy(ms1," ->: 到数据域, ↓, <-:产生左孩子或到左孩子 ");
strcpy(mss," 左孩子域");
break;
}
case d:{
strcpy(s1," <-:lchild, ->:rchild, ↑:parent, ^i or space : input");
strcpy(ss," data field ");
strcpy(mss," 数据域");
strcpy(ms1," <-:到左孩子域, ->:到右孩子域, ↑:到父结点, ^i 或空格 : 输入数据");
break;
}
case r:{
strcpy(s1," <-: data, ↓, ->: rchild");
strcpy(ss," rchild field");
strcpy(ms1," <-: 到数据域, ↓, ->: 产生右孩子或到右孩子");
strcpy(mss," 右孩子域");
break;
}
case m:{
strcpy(s1,"<-, ^l<-: move left, ->,^l-> : move right, esc:abort");
strcpy(ss," locate node ");
strcpy(ms1," <-,^ <-: 左移, ->,^ ->: 右移, esc:取消结点, 以回车键结束");
strcpy(mss,"选择结点位置");
break;
}
case 3:{
strcpy(s1," } by <return> ");
strcpy(ss," input data");
strcpy(s," ");
strcpy(ms1," 以回车结束 ");
strcpy(mss," 输入数据 ");
strcpy(s," ");
break;
}
}
strcat(ss,s);
outtextxy(getmaxx() / 2,6,space);
outtextxy(getmaxx() / 2,17,space);
outtextxy(getmaxx() / 2,6,ss);
outtextxy(getmaxx() / 2,17,s1);
strcpy(stemp,mss);
strcat(stemp,"──");
strcat(stemp,ms1);
Menu(stemp);
}
void loctnode(grbitre &t,ldr ll,int fx,int fy,ffield &ff)
{
int step;
grbitre fa;
int key;
dismenu(m);
putimage(t->x,t->y,ff,2);
do{
Getkey(key,funckey);
if ((key!=10)&&(key!=13)&&(key!=esc)){
if (funckey && ((key==left)||(key==right)||(key==bigleft)||(key==bigright))){
//outtextxy(200,200,"move key");
if ((key==left)||(key==right)) step=keystep;
else step=bigstep;
putimage(t->x,t->y,ff,1);
switch (key){
case left:
case bigleft:
switch (ll){
case lt:
if (t->x==2) printf("\007");
else t->x=Max(2,t->x-step);
break;
case r:
if (t->father==NULL)
if (t->x<=w) printf("\007");
else t->x=Max(t->x-step,w);
else
if (t->x==t->father->x+w / 2+sd ) printf("\007");
else t->x=Max(t->father->x+w / 2+sd,t->x-step);
break;
// default:printf("\nthe data out of the area!");
}
break;
case right:
case bigright:
switch ( ll ){
case lt:
if (t->father==NULL)
if (t->x>getmaxx()-2*w ) printf("\007");
else t->x=Min(t->x+step,getmaxx()-2*w);
else
if (t->x==t->father->x-w / 2 -sd ) printf("\007");
else t->x=Min(t->x+step,t->father->x-w / 2-sd);
break;
case r:
if (t->x>=getmaxx()-w-2 ) printf("\007");
else t->x=Min(t->x+step,getmaxx()-w-2);
break;
// default:printf("\nthe data out of the area!");
}
break;
}//switch
putimage(t->x,t->y,ff,2);
}else printf("\007");
}
if ((key==esc) && (t->father==NULL)) { printf("\007");key=49; }
}while ((key!=10)&&(key!=13)&&(key!=esc));
if (key==esc){
putimage(t->x,t->y,ff,1);
free(t);
t=NULL;
}else
if (fx+fy!=0){
fa=t->father;
fifild(fa,2,ll);
fifild(fa,1,ll);
Arrowdot(fx,fy,t->x+w / 2,t->y);
}
}
void crtbitress(grbitre &root)
{
grbitre t,fa,rootptr;
ldr ldrstate,ll;
char keych;
int fx,fy;
string ss;
int key;
// assigncrt(input); reset(input);
rootptr=(grbitre)malloc(sizeof(struct grbinode));
rootptr->x=getmaxx() / 4;
rootptr->y=startrow;
fx=0;
fy=0;
rootptr->rchild=NULL;
Menu("选择根结点的父结点的位置----用方向箭移动, 以回车键结束");
getch();
getch();
rootptr->father=NULL;
rootptr->lr=r; //keych=readkey;
loctnode(rootptr,r,fx,fy,ff);
window(52,2,66,2); // firfild(rootptr,2);
do{
Getkey(key,funckey);
if (key!=ctrlw)
if (!(funckey && ((key==down)||(key==right)))) printf("\007");
else{
firfild(rootptr,1);
ll=r;
fa=rootptr;
t=fa->rchild;
do{
ldrstate=d;
if ((t==NULL)&&((fa!=rootptr)||(ll!=lt))){
t=(grbitre)malloc(sizeof(struct grbinode));
if (ll==lt) fa->lchild=t;
else fa->rchild=t;
fy=fa->y+hd / 2;
fx=fa->x+w / 2+ll*(w-ld) / 2;
t->y=fa->y+steprow;
t->x=fa->x+ll*(w / 2+sd);
t->father=fa;
t->lr=ll;
t->lchild=NULL;
t->rchild=NULL;
t->hvdt=false;
loctnode(t,ll,fx,fy,ff);
}
if ((t==NULL) && ((fa!=rootptr) || (ll!=lt))){
ldrstate=ll;
t=fa;
fifild(t,1,ll);
if (ll==lt) fa->lchild=NULL;
else fa->rchild=NULL;
}
do{
switch (ldrstate){
case d:{
dismenu(d);
fidfild(t,1);//enter data state
if (t->hvdt) disnodedat(t);
do{
Getkey(key,funckey);
while (!funckey && (key==ctrli) || (key==' ')){
dismenu(3);
clrscr();
scanf("%c",&(t->data));
dismenu(d);
t->hvdt=true;
disnodedat(t);
Getkey(key,funckey);
}
if (key!=ctrlw)
if (!(funckey && ((key==up)||(key==left)||(key==right)))) printf("\007");
else
switch (key){
case left: fidfild(t,1);ldrstate=lt;break;
case right:fidfild(t,1);ldrstate=r;break;
case up: if (t->father==NULL){printf("\007");key=49;}
else {
fidfild(t,1);
ldrstate=ll;
t=t->father;
ll=t->lr;
}
break;
}
}while (!((key==ctrlw)||funckey && ((key==up)||(key==left)||(key==right)))); //go away from datastate
break;
}
case lt:{
dismenu(lt);
filfild(t,1);//enter lchild state
do{
Getkey(key,funckey);
if (key!=ctrlw)
if (!(funckey && ((key==down)||(key==right)||(key==left)))) printf("\007");
else
switch (key){
case right:ldrstate=d;filfild(t,1);break;//go away from lchildstate
case down:
case left:
filfild(t,1);//go away from lchildstate
fa=t;
t=t->lchild;
ll=lt;
key=down;
break;
}
}while (!((key==ctrlw) || funckey && ((key==down)||(key==right))));
break;
}
case r:{
dismenu(r);
firfild(t,1);//enter rchild state
do{
Getkey(key,funckey);
if (key!=ctrlw)
if (!(funckey && ((key==down)||(key==right)||(key==left)))) printf("\007");
else
switch (key){
case left:
if ( t->father==NULL) Error("因该结点的右孩子才是根结点, 左边无效, 故不能左移");
else{
ldrstate=d;
firfild(t,1); //go away from rchildstate}
}
break;
case down:
case right:{
firfild(t,1);//go away from rchildstate
fa=t;
t=t->rchild;
ll=r;
key=down;
break;
}
}
}while (!((key==ctrlw) || funckey && ((key==down)||(key==left))));
break;
}
}
}while (!((key==ctrlw) || funckey && (key==down)));
}while (key!=ctrlw);
}
}while (!(key==ctrlw));
root=rootptr->rchild;
window(1,1,80,25);
gotoxy(1,1);//clrscr;
Clear_range(0,0,getmaxx(),getmaxy());
}
void initial_bitre()
{
Into_graph();
// if (bitrecount==0){
if ((graph_state==0)||(bitre_graph_state==0)){
bitre_graph_state=1;
may=25;//maxcrty
if (getmaxy()>250){
steprow=2*Round(getmaxy() / may)-4;
hd=steprow / 2;
startrow=2*Round(getmaxy()/may)+2;
}
// setfillstyle(1,0);
// setcolor(14);
// setbkcolor(1);
// textcolor(14);
// textbackground(1);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(forecolor);
directvideo=false;
w=ld+rd+dd;
setfillpattern(fillmode,fillcolor);
rectangle(1,1,2*ld+3,2*rd+3);
bar(1,1,2*ld+3,2*rd+3);
size=imagesize(1,1,2*ld+3,2*rd+3);
bnfd=farmalloc(size);
getimage(1,1,2*ld+3,2*rd+3,bnfd);
putimage(1,1,bnfd,1);
rectangle(1,1,w+1,hd);
line(1+ld,1,1+ld,hd);
line(ld / 2-1,hd / 2+2,ld / 2,ld / 2 -1);
line(ld / 2+1,hd / 2+2,ld / 2,ld / 2 -1);
size=imagesize(1,1,ld+1,hd+1);
ff1=farmalloc(size);
getimage(1,1,ld+1,hd+1,ff1);
putimage(ld+dd+1,1,ff1,2);
size=imagesize(1,1,w+1,hd+1);
ff=farmalloc(size);
getimage(1,1,w+1,hd+1,ff);
bar(1,1,dd-2,hd-2);
size=imagesize(1,1,dd-2,hd-2);
datafild=farmalloc(size);
getimage(1,1,dd-2,hd-2,datafild);
chldfild=farmalloc(size);
getimage(1,1,ld-2,hd-2,chldfild);
putimage(1,1,datafild,1);
size=imagesize(1,1,2*radiux+1,2*radiuy+1);
nd1=farmalloc(size);
size=imagesize(1,1,5,20);
fd1=farmalloc(size);
fd2=farmalloc(size);
Arrow(3,1,3,20);
getimage(1,1,5,20,fd1);
putimage(1,1,fd1,1);
Arrow(3,20,3,1);
getimage(1,1,5,20,fd2);
putimage(1,1,fd2,1);
size=imagesize(1,1,20,5);
fd3=farmalloc(size);
fd4=farmalloc(size);
Arrow(1,3,20,3);
getimage(1,1,20,5,fd3);
putimage(1,1,fd3,1);
Arrow(20,3,1,3);
getimage(1,1,20,5,fd4);
putimage(1,1,fd4,1);
size=imagesize(1,1,20,20);
ndcur=farmalloc(size);
bar(1,1,20,20);
getimage(1,1,20,20,ndcur);
putimage(1,1,ndcur,1);
// size=imagesize(1,1,11,11);
// getmem(newroot,size);
// circle(6,6,5);
// getimage(1,1,11,11,newroot);
// putimage(1,1,newroot,1);
}
setfillstyle(1,0);
bar(0,0,getmaxx(),getmaxy());
setfillstyle(1,WHITE);
bitrecount++;
// }
for(i=0;i<4;i++) visitebtmodes[i]=onevisitemode;
}
void create_gr_bitre(bitre &t)
{
initial_bitre();
crtbitress(grbt);
copy_grbt_bitre(grbt,t);
comput_bitre_card(t);
}
void create(bitre &t)
{
datatype ch;
ch=getchar();
while (ch==13||ch==10) ch=getchar();
if (ch==btr_end) t=NULL; // ||(ch=='#')
else{
t=(bitre)malloc(sizeof(struct bnode));
t->data=ch;
t->ltag=0;
t->rtag=0;
t->x=0;
t->y=0;
create(t->lchild);
create(t->rchild);
}
}
void create_ex_bitre(bitre &t)
{
initial_bitre();
window(1,1,80,8);
gotoxy(1,1);
for(i=1;i<=3;i++) printf("%80c",' ');//ln
printf("%45s%35c","create binary tree",' ');
printf("%60s%20c"," please input the data according the preorder",' ');
printf("%50s%30c"," of the extended tree ",' ');
create(t);
comput_bitre_card(t);
Clear_range(0,0,getmaxx(),getmaxy() / 3);
gotoxy(1,1);
}
void inorder(bitre t)
{
if (t!=NULL){
inorder(t->lchild);
printf("%c",t->data);
inorder(t->rchild);
}
}
void preorder(bitre t)
{
if (t!=NULL){
printf("%c",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -