二叉树非递归遍历
04 二叉树非递归遍历.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include"SeqStack.h"
struct BiNode
{
char ch;
struct BiNode *lchild;
struct BiNode *rchild;
};
struct Info
{
struct BiNode *node;
bool flag;
};
struct Info* createInfo(struct BiNode *node, bool flag)
{
struct Info *info = malloc(sizeof(struct Info));
info->flag = flag;
info->node = node;
return info;
}
void nonRecursion(struct BiNode *root)
{
//初始化栈
SeqStack stack = Init_SeqStack();
//先把根节点压入栈中
Push_SeqStack(stack, createInfo(root, false));
while (Size_SeqStack(stack) > 0)
{
//获得栈顶元素
struct Info *info = (struct Info *)Top_SeqStack(stack);
//弹出栈顶元素
Pop_SeqStack(stack);
if (info->flag)
{
printf("%c ",info->node->ch);
free(info);
continue;
}
//将根节压入栈中
info->flag = true;
Push_SeqStack(stack, info);
//将右子树压入栈中
if (info->node->rchild != NULL)
{
Push_SeqStack(stack, createInfo(info->node->rchild, false));
}
//将左子树压入栈中
if (info->node->lchild != NULL)
{
Push_SeqStack(stack, createInfo(info->node->lchild, false));
}
}
//销毁栈
Destroy_SeqStack(stack);
}
void test()
{
struct BiNode nodeA = { 'A', NULL, NULL };
struct BiNode nodeB = { 'B', NULL, NULL };
struct BiNode nodeC = { 'C', NULL, NULL };
struct BiNode nodeD = { 'D', NULL, NULL };
struct BiNode nodeE = { 'E', NULL, NULL };
struct BiNode nodeF = { 'F', NULL, NULL };
struct BiNode nodeG = { 'G', NULL, NULL };
struct BiNode nodeH = { 'H', NULL, NULL };
nodeA.lchild = &nodeB;
nodeA.rchild = &nodeF;
nodeB.rchild = &nodeC;
nodeC.lchild = &nodeD;
nodeC.rchild = &nodeE;
nodeF.rchild = &nodeG;
nodeG.lchild = &nodeH;
nonRecursion(&nodeA);
}
int main(){
test();
system("pause");
return EXIT_SUCCESS;
}
SeqStack.h
#pragma once
#include<stdlib.h>
#include<string.h>
#ifdef __cplusplus
extern "C"{
#endif
#define MAX 1024
//顺序栈数据结构
struct SStack
{
void *data[MAX]; //存放数据的数组
int size;//栈中元素的个数
};
typedef void * SeqStack;
//数组高下标的位置当做栈顶,因为不需要移动数组中的元素在插入和删除中
//初始化
SeqStack Init_SeqStack();
//入栈
void Push_SeqStack(SeqStack stack, void *data);
//出栈
void Pop_SeqStack(SeqStack stack);
//获得栈顶元素
void *Top_SeqStack(SeqStack stack);
//获得栈的大小
int Size_SeqStack(SeqStack stack);
//销毁栈
void Destroy_SeqStack(SeqStack stack);
#ifdef __cplusplus
}
#endif
SeqStack.c
#include"SeqStack.h"
//初始化
SeqStack Init_SeqStack()
{
struct SStack *stack = malloc(sizeof(struct SStack));
if (NULL == stack)
{
return NULL;
}
//memset(stack->data, 0, sizeof(struct SStack));
stack->size = 0;
for (int i = 0; i < MAX; ++i)
{
stack->data[i] = NULL;
}
return stack;
}
//入栈
void Push_SeqStack(SeqStack stack, void *data)
{
if (NULL == stack)
{
return;
}
if (NULL == data)
{
return;
}
struct SStack *s = (struct SStack *)stack;
if (s->size == MAX)
{
return;
}
s->data[s->size] = data;
s->size++;
}
//出栈
void Pop_SeqStack(SeqStack stack)
{
if (NULL == stack)
{
return;
}
struct SStack *s = (struct SStack *)stack;
if (s->size == 0)
{
return;
}
s->data[s->size - 1] = NULL;
s->size--;
}
//获得栈顶元素
void *Top_SeqStack(SeqStack stack)
{
if (NULL == stack)
{
return NULL;
}
struct SStack *s = (struct SStack *)stack;
if (s->size == 0)
{
return NULL;
}
return s->data[s->size - 1];
}
//获得栈的大小
int Size_SeqStack(SeqStack stack)
{
if (NULL == stack)
{
return -1;
}
struct SStack *s = (struct SStack *)stack;
return s->size;
}
//销毁栈
void Destroy_SeqStack(SeqStack stack)
{
if (NULL == stack)
{
return;
}
free(stack);
}