33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#define MaxSize 50
typedef char ElemType;
typedef struct node
{
ElemType data;//数据元素
struct node *lchild;//指向左孩子结点
struct node *rchild;//指向右孩子结点
}BTNode;
typedef struct
{
BTNode *data[MaxSize];//存放队中元素
int front,rear;//队头和队尾指针
}SqQueue;//顺序队类型
void CreateBTree(BTNode *&b,char *str)//创建二叉树
{
BTNode *St[MaxSize],*p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case'(':top++;St[top]=p;k=1;break;
case')':top--;break;
case',':k=2;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode *&b)// 销毁二叉树
{
if(b!=NULL)
{
DestroyBTree(b->lchild);
DestroyBTree(b->rchild);
free(b);
}
}
void InitQueue(SqQueue *&q)//初始化队列
{
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
bool QueueEmpty(SqQueue *q)//判断队列是否为空
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)//进队列
{
if((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,ElemType &e)//出队列
{
if(q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
e=q->data[q->front];
return true;
}
void PreOrder(BTNode *b)//前序遍历
{
if(b!=NULL)
{
printf("%c",b->data);
PreOrder(b->lchild);
PreOrder(b->rchild);
}
}
void InOrder(BTNode *b)//中序遍历
{
if(b!=NULL)
{
InOrder(b->lchild);
printf("%c",b->data);
InOrder(b->rchild);
}
}
void PostOrder(BTNode *b)//后序遍历
{
if(b!=NULL)
{
PostOrder(b->lchild);
PostOrder(b->rchild);
printf("%c",b->data);
}
}
void LevelOrder(BTNode *b)//层次遍历
{
BTNode *p;
SqQueue *qu;
InitQueue(qu);
enQueue(qu,b);
while(!QueueEmpty(qu))
{
deQueue(qu,p);
printf("%c",p->data);
if(p->lchild!=NULL)
enQueue(qu,p->lchild);
if(p->rchild!=NULL)
enQueue(qu,p->rchild);
}
}
int main()
{
BTNode *b;
CreateBTree(b,"A(B(D(,G)),C(E,F))");
printf("先序遍历结果为:\n");
PreOrder(b);
printf("中序遍历结果为:\n");
InOrder(b);
printf("后序遍历结果为:\n");
PostOrder(b);
printf("层次遍历结果为:\n");
LevelOrder(b);
while (!QueueEmpty(qu))
{ deQueue(qu,e);
printf("%d ",e);
}
printf("\n");
DestroyBTree(b);
return 0;
}
#include <stdio.h>
#include <malloc.h>
#define MaxSize 30
typedef char ElemType;
typedef struct node
{
ElemType data;//数据元素
struct node *lchild;//指向左孩子结点
struct node *rchild;//指向右孩子结点
}BTNode;
typedef struct
{
BTNode *data[MaxSize];//存放队中元素
int front,rear;//队头和队尾指针
}SqQueue;//顺序队类型
void CreateBTree(BTNode *&b,char *str)//创建二叉树
{
BTNode *St[MaxSize],*p;
int top=-1,k,j=0;
char ch;
b=NULL;
ch=str[j];
while(ch!='\0')
{
switch(ch)
{
case'(':top++;St[top]=p;k=1;break;
case')':top--;break;
case',':k=2;break;
default:p=(BTNode *)malloc(sizeof(BTNode));
p->data=ch;
p->lchild=p->rchild=NULL;
if(b==NULL)
b=p;
else
{
switch(k)
{
case 1:St[top]->lchild=p;break;
case 2:St[top]->rchild=p;break;
}
}
}
j++;
ch=str[j];
}
}
void DestroyBTree(BTNode *&b)// 销毁二叉树
{
if(b!=NULL)
{
DestroyBTree(b->lchild);
DestroyBTree(b->rchild);
free(b);
}
}
void InitQueue(SqQueue *&q)//初始化队列
{
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
bool QueueEmpty(SqQueue *q)//判断队列是否为空
{
return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,BTNode *b)//进队列
{
if((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->data[q->rear]=b;
return true;
}
bool deQueue(SqQueue *&q,BTNode *b)//出队列
{
if(q->front==q->rear)
return false;
q->front=(q->front+1)%MaxSize;
b=q->data[q->front];
return true;
}
void LevelOrder(BTNode *b)//层次遍历
{
BTNode *p;
SqQueue *qu;
InitQueue(qu);
enQueue(qu,b);
while(!QueueEmpty(qu))
{
deQueue(qu,p);
printf("%c",p->data);
if(p->lchild!=NULL)
enQueue(qu,p->lchild);
if(p->rchild!=NULL)
enQueue(qu,p->rchild);
}
}
int main()
{
BTNode *b;
SqQueue *qu;
CreateBTree(b,"A(B(D(,G)),C(E,F))");
printf("\n层次遍历结果为:\n");
LevelOrder(b);
while (!QueueEmpty(qu))
{ deQueue(qu,b);
printf("%c ",b);
}
printf("\n");
DestroyBTree(b);
return 0;
}