数据结构(C语言)关于二叉树的非递归遍历

非递归算法

中序遍历(利用栈)

基本思想:

  1. 建立一个栈
  2. 根结点进栈,遍历左子树
  3. 根结点出栈,输出根结点,遍历右子树

栈的代码
二叉树的代码

这里用到栈的代码了,又翻开了以前整理的链栈的操作,发现了这种代码可移植性的好处,在之前操作链栈定义的数据类型为int,typedef int SElemType;现在我们要操作的对象不是整数类型,而是二叉树的指针类型,所以我们将SElemType重新定义别名为:typedef BiTree SElemType;拿过来就可以使用了(-△-)。

完整代码

#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef char TElemType;
typedef struct BiNode {
	TElemType data;
	struct BiNode* lchild, * rchild;//左右孩子指针
}BiTNode,*BiTree;
Status CreateBiTree(BiTree& T);
//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树
//构造二叉链表表示的二叉树T
Status CreateBiTree(BiTree& T)
{
	char ch;
	scanf("%c",&ch);
	if (ch == '@')
		T = NULL;
	else 
	{
		T = ((BiTNode*)malloc(sizeof(BiTNode)));
		if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
			exit(OVERFLOW);
		T->data = ch;
		CreateBiTree(T->lchild);//构造左子树
		CreateBiTree(T->rchild);//构造右子树
	}
	return OK;
}//CreateBiTree

typedef BiTree SElemType;
typedef struct StackNode
{
	SElemType data;
	struct StackNode* next;
}StackNode, * LinkStack;
Status InitStack(LinkStack& S)
{
	//构造一个空栈,栈顶指针置为空
	S = NULL;
	return OK;
}//InitStack
Status StackEmpty(LinkStack S)
{
	if (S == NULL)//非空
		return OK;
	else
		return FALSE;
}//StackEmpty
Status Push(LinkStack& S, SElemType e)
{
	LinkStack p = (LinkStack)malloc(sizeof(StackNode));
	p->data = e;//将新结点数据域置为e
	p->next = S;//将新结点插入栈顶
	S = p;//修改栈顶指针
	return OK;
}//Push
Status Pop(LinkStack& S, SElemType& e)
{
	if (S == NULL)
		return ERROR;
	e = S->data;
	LinkStack p = S;
	S = S->next;
	free(p);
	return OK;
}//Pop
Status InOrderTraverse(BiTree T)
{
	BiTree p;
	LinkStack S;
	InitStack(S);
	p = T;
	while (p || !StackEmpty(S))
	{
		if (p!=NULL)
		{
			Push(S, p);
			p = p->lchild;
		}
		else
		{
			BiTree q;
			Pop(S, q);
			printf("%c", q->data);
			p = q->rchild;
		}
	}
	return OK;
}//InOrderTraverse


int main()
{
	BiTree T;
	CreateBiTree(T);//-+A@@*B@@C@@/D@@E@@ 输入这个进去
	InOrderTraverse(T);
	return 0;
}

二叉树的层次遍历(队列)

结点按照从上到下、从左到右的顺序,每个结点仅仅访问一次。

+ ÷ A × D E B C -+\div A\times DEBC
算法设计思路:

  1. 将根结点进队;
  2. 队不空时循环:从队列中出列一个结点*p,访问它;
    ①若它有左孩子结点,将左孩子结点进队;
    ②若它有右孩子结点,将右孩子结点进队。

代码实现

Status LevelOrder(BiTNode* b)
{
	BiTNode* p;
	SqQueue qu;//建立队列
	InitQueue(qu);//初始化队列
	EnQueue(qu, b);//将二叉树头结点入队
	int i = 0;
	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);
	}
	return OK;
}

这次又翻出了以前整理的队列的代码,发现其中DeQueue(SqQueue& Q, QElemType& e);的精髓之处,以前总觉得保不保留这个数据没有必要,现在发现很有必要。

完整代码

#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXQSIZE 100//队列长度 根据二叉树结点数更改
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef char TElemType;

typedef struct BiNode {
	TElemType data;
	struct BiNode* lchild, * rchild;//左右孩子指针
}BiTNode, * BiTree;

Status CreateBiTree(BiTree& T);
Status CreateBiTree(BiTree& T)
{
	char ch;
	scanf("%c", &ch);
	if (ch == '@')
		T = NULL;
	else
	{
		T = ((BiTNode*)malloc(sizeof(BiTNode)));
		if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
			exit(OVERFLOW);
		T->data = ch;
		CreateBiTree(T->lchild);//构造左子树
		CreateBiTree(T->rchild);//构造右子树
	}
	return OK;
}//CreateBiTree

typedef BiTree QElemType;

typedef struct {
	QElemType* base;//初始化的动态分配存储空间
	int front;//头指针,若队列不空,指向队列头元素
	int rear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

Status InitQueue(SqQueue& Q);
//构造一个空队列Q
bool QueueEmpty(SqQueue Q);
//判断队列是否为空
Status EnQueue(SqQueue& Q, QElemType e);
//插入元素e为Q的新的队尾元素
Status DeQueue(SqQueue& Q, QElemType& e);
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR

Status InitQueue(SqQueue& Q)
{
	Q.base = (QElemType*)malloc(MAXQSIZE * sizeof(QElemType));
	if (!Q.base)
		exit(OVERFLOW);//存储分配失败
	Q.front = Q.rear = 0;
	return OK;
}
bool QueueEmpty(SqQueue Q)
{
	if (Q.front == Q.rear)
		return TRUE;
	else
		return FALSE;
}
Status EnQueue(SqQueue& Q, QElemType e)
{
	if (Q.rear == MAXQSIZE)
		return ERROR;
	Q.base[Q.rear] = e;
	Q.rear = Q.rear + 1;
	return OK;
}
Status DeQueue(SqQueue& Q, QElemType& e)
{
	if (Q.front == Q.rear)
		return ERROR;
	e = Q.base[Q.front];
	Q.front = Q.front + 1;
	return OK;
}
Status LevelOrder(BiTNode* b)
{
	BiTNode* p;
	SqQueue qu;//建立队列
	InitQueue(qu);//初始化队列
	EnQueue(qu, b);//将二叉树头结点入队
	int i = 0;
	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);
	}
	return OK;
}


int main()
{
	BiTree T;
	CreateBiTree(T);//-+A@@*B@@C@@/D@@E@@
	LevelOrder(T);
	return 0;
}

输入样例

+ A @ @ B @ @ C @ @ / D @ @ E @ @ -+A@@*B@@C@@/D@@E@@

输出样例

+ / A D E B C -+/A*DEBC

猜你喜欢

转载自blog.csdn.net/qq_44864262/article/details/107179150