二叉树的生成与遍历

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include <alloc.h>
#include "btree.h"
#include "sstack.h"
typedef struct stack_tag
{
elemtype *elem;
int top;
int size;
}SQSTACK;
int InitSqstack(SQSTACK *S,int n);
void DestroySqstack(SQSTACK *S);
int IsSqstackEmpty(SQSTACK S);
int IsSqstackFull(SQSTACK S);
int Push(SQSTACK *S,elemtype e);
int Pop(SQSTACK *S,elemtype *e);
int InitSqstack(SQSTACK *S, int n)
{
S->elem=(elemtype *)malloc(n*sizeof(elemtype)); if(S->elem==NULL)return 0;
S->size=n;
S->top=-1;
return 1;
}
void DestroySqstack(SQSTACK *S)
{
free(S->elem);
S->elem=NULL;
S->top=-1;
S->size=0;
}
int IsSqstackEmpty(SQSTACK S)
{
return S.top==-1;
}
int IsSqstackFull(SQSTACK S)
{
return S.top==S.size-1;
}
int Push(SQSTACK *S,elemtype e)
{
if(IsSqstackFull(*S))return 0;
S->top++;
S->elem[S->top]=e;
return 1;
}
int Pop(SQSTACK *S,elemtype *e)
{
if(IsSqstackEmpty(*S)) return 0;
*e=S->elem[S->top];
S->top--;
return 1;
}
typedef struct thrbtreenode
{
char data;
int ltag,rtag;
struct thrbtreenode *lchild,*rchild;
} THRBTREENODE, *THRBTREENODEPTR,*THRBTREE; typedef THRBTREENODEPTR elemtype;
typedef struct
{
int x,y;
}BTREENODEPOS;
#define MAXCOUNT 32
void InitBtreeNodePos();
THRBTREE CreateBtree2(char *str);
void DestroyBtree(THRBTREE root);
void ShowBtree(THRBTREE root,int index);
void PreOrderThr(THRBTREE p);
THRBTREE InOrderThread(THRBTREE root);
void InOrderThr(THRBTREE p);
THRBTREE PreOrderThread(THRBTREE root);
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <alloc.h>
#include <string.h>
#include "btree.h"
#include "sstack.h"
THRBTREENODEPTR pre;
BTREENODEPOS btnpos[MAXCOUNT];
void InitBtreeNodePos(void)
{
int i;
for(i=0;i<16;i++)
{
btnpos[16+i].x=20+i*40;
btnpos[16+i].y=480-30;
}
for(i=15;i>=1;i--)
{
btnpos[i].x=(btnpos[2*i].x+btnpos[2*i+1].x)/2;
btnpos[i].y=btnpos[2*i].y-80;
}
}
THRBTREE CreateBtree1(char *str)
{
THRBTREE root=NULL;
THRBTREENODEPTR p;
int tag,i,len;
int mark;/* 1--characters,2--(,3--,4--) */
SQSTACK s;
if(str[0]==0)return root;
root=(THRBTREENODEPTR)malloc(sizeof(THRBTREENODE)); if(root==NULL)return root;
root->data=str[0];
root->lchild=root->rchild=NULL;
root->ltag=root->rtag=0;
len=strlen(str);
InitSqstack(&s,len);
p=root;
mark=1;
for(i=1;str[i]!=0;i++)
switch(str[i])
{
case '(':
if(mark==2)
{
DestroyBtree(root);
printf("illegal global list!");
return NULL;
}
mark=2;
Push(&s,p);
tag=0;
break;
case ')':
mark=4;
if(!Pop(&s,&p))
{
DestroyBtree(root);
printf("illegal global list!");
return NULL;
}
break;
case ',':
if(mark==3)
{
DestroyBtree(root);
printf("illegal global list!");
return NULL;
}
mark=3;
tag=1;
break;
default:
if(mark==1)
{
DestroyBtree(root);
printf("illegal global list!");
return NULL;
}
mark=1;
if(IsSqstackEmpty(s))
{
DestroyBtree(root);
printf("illegal global list!");
return NULL;
}
p=(THRBTREENODEPTR)malloc(sizeof(THRBTREENODE)); if(p==NULL)
{
DestroyBtree(root);
return NULL;
}
p->data=str[i];
p->lchild=p->rchild=NULL;
p->ltag=p->rtag=0;
if(tag==0)
s.elem[s.top]->lchild=p;
else
s.elem[s.top]->rchild=p;
break;
}
return root;
}
void DestroyBtree(THRBTREE root)
{
if(root==NULL)return;
if(root->ltag==0)
DestroyBtree(root->lchild);
if(root->rtag==0)
DestroyBtree(root->rchild);
root->lchild=NULL;
root->rchild=NULL;
free(root);
}
void ShowBtree(THRBTREE root,int index)
{
int i,j,len;
char str[10];
if(root==NULL)return;
i=index*2;
j=index*2+1;
setcolor(YELLOW);
if(i<MAXCOUNT&&root->ltag==0&&root->lchild!=NULL) line(btnpos[index].x,btnpos[index].y,btnpos[i].x,btnpos[i].y); if(j<MAXCOUNT&&root->rtag==0&&root->rchild!=NULL) line(btnpos[index].x,btnpos[index].y,btnpos[j].x,btnpos[j].y); setfillstyle(SOLID_FILL,BLACK);
setcolor(WHITE);
fillellipse(btnpos[index].x,btnpos[index].y,15,15);
sprintf(str,"%c",root->data);
len=strlen(str);
outtextxy(btnpos[index].x-len*8/2,btnpos[index].y-4,str); setcolor(YELLOW);
if(root->ltag==1&&root->lchild!=NULL)
{
sprintf(str,"%c",root->lchild->data);
len=strlen(str);
outtextxy(btnpos[index].x-len*8/2-8,btnpos[index].y,str);
}
if(root->rtag==1&&root->rchild!=NULL)
{
sprintf(str,"%c",root->rchild->data);
len=strlen(str);
outtextxy(btnpos[index].x-len*8/2+8,btnpos[index].y,str);
}
if(i<MAXCOUNT&&root->ltag==0) ShowBtree(root->lchild,i);
if(j<MAXCOUNT&&root->rtag==0) ShowBtree(root->rchild,j);
}
void InOrder(THRBTREE root,char *str)
{
char tmpstr[10];
if(root==NULL)return;
InOrder(root->lchild,str);
sprintf(tmpstr,"%c ",root->data);
strcat(str,tmpstr);
InOrder(root->rchild,str);
}
void InOrderThr(THRBTREE p)
{
}
THRBTREE InOrderThread(THRBTREE root)
{
THRBTREE head;
head=(THRBTREENODEPTR)malloc(sizeof(THRBTREENODE)); if(head==NULL)return NULL;
head->lchild=NULL;
return head;
}
void TraverseInOrderThr(THRBTREE root,char *str)
{
return;
}
void main()
{
char *gstr="A(B(C,),E(F(G,H),))"; char trastr[80];
THRBTREE root,thrt;
int gdriver=DETECT,gmode;
int i;
InitBtreeNodePos();
root=CreateBtree1(gstr);
registerbgidriver(EGA VGA_driver); initgraph(&gdriver,&gmode,"");
trastr[0]=0;
InOrder(root,trastr);
setcolor(WHITE);
outtextxy(10,10,trastr);
thrt=InOrderThread(root);
trastr[0]=0; TraverseInOrderThr(root,trastr); setcolor(YELLOW);
outtextxy(10,20,trastr); ShowBtree(thrt->lchild,1);
getch();
closegraph();
DestroyBtree(root);
}。

相关文档
最新文档