二cha树

时间:2023-03-09 17:14:54
二cha树
 void porder(BTree *b)
{
BTree *St[MaxSize],*p;
int top=;
if(b!=NULL)
{
top++;
St[top]=b;
while(top>-)
{
p=St[top];
top--;
printf("%c",p->data);
if(p->rchild!=NULL)
{
top++;
St[top]=p->rchild;
}
if(p->lchild!=NULL)
{
top++;
St[top]=p->lchild;
}
}
}
} void psorder(BTree*t)
{
BTree *St[MaxSize];
BTree*p;
int flag,top=-;
do
{
while(t)
{
top++;
St[top]=t;
t=t->lchild;
}
p=NULL;
flag=;
while(top!=-&&flag)
{
t=St[top];
if(t->rchild==p)
{
printf("%c ",t->data);
top--;
p=t;
}
else
{
t=t->rchild;
flag=;
}
}
}while(top!=-)
} int level(BTree *b,ElemType x,int h)
{
int h1;
if(b==NULL)
return();
else
if(b->data==x)
return(h);
else
{
h1=level(b->lchild,x,h+);
if(h1!=)
return(h1);
else
return level(b->rchild,x,h+);
}
} void translevel(BTree *b)
{
struct node
{
BTree *vec[MaxSize];
int f,r;
}Qu;
Qu.f=;
Qu.r=;
if(b!=NULL)
printf("%c ",b->data);
Qu.vec[Qu.r]=b;
Qu.r=Qu.r+;
while(Qu.f<Qu.r)
{
b=Qu.vec[Qu.f];
Qu.f=Qu.f+;
if(b->lchild!=NULL)
{
printf("%c",b->lchild->data);
Qu.vec[Qu.r]=b->lchild;
Qu.r=Qu.r+;
}
if(b->rchild!=NULL)
{
printf("%c ",b->rchild->data);
Qu.vec[Qu.r]=b->rchild;
Qu.r=Qu.r+;
}
}
printf("\n");
}