二叉树的四种遍历
作者:互联网
文章目录
输入样例:
124005003600700
0
先序、中序、后序遍历(递归)
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor);
void BiTreeDestroy(BiTree *root);
void PreOrderTraverse(BiTree root);
void InOrderTraverse(BiTree root);
void PostOrderTraverse(BiTree root);
int main() {
char str[MaxSize] = "";
while (scanf("%[^\n]%*c", str) && str[0] != 48) {
BiTree T = NULL;
int cursor = 0;
BiTreeCreate(&T, str, &cursor);
PreOrderTraverse(T); printf("\n");
InOrderTraverse(T); printf("\n");
PostOrderTraverse(T); printf("\n");
BiTreeDestroy(&T);
}
return 0;
}
/**
* 先序非递归
*/
void PreOrderTraverse(BiTree root) {
if (root) {
printf("%c", root->data);
PreOrderTraverse(root->lchild);
PreOrderTraverse(root->rchild);
}
}
/**
* 中序非递归
*/
void InOrderTraverse(BiTree root) {
if (root) {
InOrderTraverse(root->lchild);
printf("%c", root->data);
InOrderTraverse(root->rchild);
}
}
/**
* 后序非递归
*/
void PostOrderTraverse(BiTree root) {
if (root) {
PostOrderTraverse(root->lchild);
PostOrderTraverse(root->rchild);
printf("%c", root->data);
}
}
/**
* 根据先序序列创建二叉树
*/
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor) {
if (nodeData[*cursor] == 48) {
*root = NULL;
} else {
*root = (BiTNode *) malloc(sizeof(BiTNode));
(*root)->data = nodeData[*cursor];
(*cursor)++;
BiTreeCreate(&(*root)->lchild, nodeData, cursor);
(*cursor)++;
BiTreeCreate(&(*root)->rchild, nodeData, cursor);
};
}
/**
* 释放二叉树内存空间
*/
void BiTreeDestroy(BiTree *root) {
if (*root) {
BiTreeDestroy(&(*root)->lchild);
BiTreeDestroy(&(*root)->rchild);
free(*root);
*root = NULL;
}
}
层序遍历
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 100
#define ElemType BiTree
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef struct SqQueue {
ElemType data[MaxSize];
int front, rear;
} SqQueue;
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor);
void BiTreeDestroy(BiTree *root);
void LevelOrderTraverse(BiTree root);
void QueueInit(SqQueue *queue);
bool QueueEmpty(SqQueue queue);
bool EnQueue(SqQueue *queue, ElemType e);
bool DeQueue(SqQueue *queue, ElemType *x);
int main() {
char str[MaxSize] = "";
while (scanf("%[^\n]%*c", str) && str[0] != 48) {
BiTree T = NULL;
int cursor = 0;
BiTreeCreate(&T, str, &cursor);
LevelOrderTraverse(T); printf("\n");
BiTreeDestroy(&T);
}
return 0;
}
/**
* 二叉树层序遍历
*/
void LevelOrderTraverse(BiTree root) {
BiTNode *nowNode = root;
SqQueue Q;
QueueInit(&Q);
EnQueue(&Q, nowNode);
while (!QueueEmpty(Q)) {
DeQueue(&Q, &nowNode);
printf("%c", nowNode->data);
if (nowNode->lchild) EnQueue(&Q, nowNode->lchild);
if (nowNode->rchild) EnQueue(&Q, nowNode->rchild);
}
}
/**
* 根据先序序列创建二叉树
*/
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor) {
if (nodeData[*cursor] == 48) {
*root = NULL;
} else {
*root = (BiTNode *) malloc(sizeof(BiTNode));
(*root)->data = nodeData[*cursor];
(*cursor)++;
BiTreeCreate(&(*root)->lchild, nodeData, cursor);
(*cursor)++;
BiTreeCreate(&(*root)->rchild, nodeData, cursor);
};
}
/**
* 释放二叉树内存空间
*/
void BiTreeDestroy(BiTree *root) {
if (*root) {
BiTreeDestroy(&(*root)->lchild);
BiTreeDestroy(&(*root)->rchild);
free(*root);
*root = NULL;
}
}
/**
* 初始化队列
*/
void QueueInit(SqQueue *queue) {
queue->front = queue->rear = 0;
}
/**
* 判队空
*/
bool QueueEmpty(SqQueue queue) {
return (queue.rear == queue.front);
}
/**
* 入队
*/
bool EnQueue(SqQueue *queue, ElemType e) {
if ((queue->rear + 1) % MaxSize == queue->front) return false;
queue->data[queue->rear] = e;
queue->rear = (queue->rear + 1) % MaxSize;
}
/**
* 出队
*/
bool DeQueue(SqQueue *queue, ElemType *x) {
if (QueueEmpty(*queue)) return false;
*x = queue->data[queue->front];
queue->front = (queue->front + 1) % MaxSize;
}
先序、后序、中序遍历(非递归)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 100
#define ElemType BiTree
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
typedef struct SqStack {
ElemType data[MaxSize];
int top;
} SqStack;
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor);
void BiTreeDestroy(BiTree *root);
void PreOrderTraverse(BiTree root);
void InOrderTraverse(BiTree root);
void PostOrderTraverse(BiTree root);
void StackInit(SqStack *stack);
bool StackEmpty(SqStack stack);
bool StackPush(SqStack *stack, ElemType e);
bool StackPop(SqStack *stack, ElemType *x);
bool StackGetTop(SqStack stack, ElemType *x);
int main() {
char str[MaxSize] = "";
while (scanf("%[^\n]%*c", str) && str[0] != 48) {
BiTree T = NULL;
int cursor = 0;
BiTreeCreate(&T, str, &cursor);
PreOrderTraverse(T); printf("\n");
InOrderTraverse(T); printf("\n");
PostOrderTraverse(T); printf("\n");
BiTreeDestroy(&T);
}
return 0;
}
/**
* 先序遍历非递归
*/
void PreOrderTraverse(BiTree root) {
BiTNode *nowNode = root;
SqStack S;
StackInit(&S);
StackPush(&S, nowNode);
while (!StackEmpty(S)) {
StackPop(&S, &nowNode);
printf("%c", nowNode->data);
if (nowNode->rchild) StackPush(&S, nowNode->rchild);
if (nowNode->lchild) StackPush(&S, nowNode->lchild);
}
}
/**
* 中序遍历非递归
*/
void InOrderTraverse(BiTree root) {
BiTNode *nowNode = root;
SqStack S;
StackInit(&S);
while (nowNode || !StackEmpty(S)) {
if (nowNode) {
StackPush(&S, nowNode);
nowNode = nowNode->lchild;
} else {
StackPop(&S,&nowNode);
printf("%c", nowNode->data);
nowNode = nowNode->rchild;
}
}
}
/**
* 后序遍历非递归
*/
void PostOrderTraverse(BiTree root) {
BiTNode *nowNode = root, *lastPopNode = NULL;
SqStack S;
StackInit(&S);
while (nowNode || !StackEmpty(S)) {
if (nowNode) {
StackPush(&S, nowNode);
nowNode = nowNode->lchild;
} else {
StackGetTop(S, &nowNode);
if (nowNode->rchild && nowNode->rchild != lastPopNode) {
nowNode = nowNode->rchild;
} else {
StackPop(&S,&nowNode);
printf("%c", nowNode->data);
lastPopNode = nowNode;
nowNode = NULL;
};
}
}
}
/**
* 根据先序序列创建二叉树
*/
void BiTreeCreate(BiTree *root, char *nodeData, int *cursor) {
if (nodeData[*cursor] == 48) {
*root = NULL;
} else {
*root = (BiTNode *) malloc(sizeof(BiTNode));
(*root)->data = nodeData[*cursor];
(*cursor)++;
BiTreeCreate(&(*root)->lchild, nodeData, cursor);
(*cursor)++;
BiTreeCreate(&(*root)->rchild, nodeData, cursor);
};
}
/**
* 释放二叉树内存空间
*/
void BiTreeDestroy(BiTree *root) {
if (*root) {
BiTreeDestroy(&(*root)->lchild);
BiTreeDestroy(&(*root)->rchild);
free(*root);
*root = NULL;
}
}
/**
* 初始化栈
*/
void StackInit(SqStack *stack) {
stack->top = -1;
}
/**
* 判栈空
*/
bool StackEmpty(SqStack stack) {
return (stack.top == -1) ? true : false;
}
/**
* 入栈
*/
bool StackPush(SqStack *stack, ElemType e) {
if (stack->top + 1 == MaxSize) return false;
stack->data[++stack->top] = e;
return true;
}
/**
* 出栈
*/
bool StackPop(SqStack *stack, ElemType *x) {
if (StackEmpty(*stack)) return false;
*x = stack->data[stack->top--];
return true;
}
/**
* 取栈顶元素但不出栈
*/
bool StackGetTop(SqStack stack, ElemType *x) {
if (StackEmpty(stack)) return false;
*x = stack.data[stack.top];
return true;
}
标签:queue,遍历,BiTree,void,cursor,二叉树,nowNode,root,四种 来源: https://blog.csdn.net/qq_43686863/article/details/123603312