报的错总是改不对,求大佬指点下该怎么改?实现二叉树先序中序后序遍历的递归算法和层次遍历的算法

我醉欲眠卿尚在 2017-11-24 12:52:22
#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;
}

...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
懂了,少了一个引用符号
  • 打赏
  • 举报
回复
自己想明白了一点,是定义的变量的类型不匹配。改了一下,执行还是不正确,我觉得好像是只输出了最后一个字符,想问一下这个层次遍历怎么改?
#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;
}
自信男孩 2017-11-24
  • 打赏
  • 举报
回复
肯定报错呢,enQueue第二个参数类型和你的调用的类型就不对呢;如果是自己定义函数,调用的时候不应该这样哦。参数对不上号;

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧