【数据结构】二叉树的非递归遍历(完整代码)

题目描述

按要求建立一棵树(#表示空),并用非递归的方法对它进行遍历

解题思路

默认给的序列是前序遍历的结果,创建一棵树。
递归遍历:是分治问题,将每一棵树都分为根-左子树-右子树三部分,每部分按同样的方法递归遍历即可。
二叉树的遍历:https://blog.csdn.net/ly_6699/article/details/87891642
非递归遍历:借用栈的知识完成。以前序为例
前序:(根-左-右)若根节点有效则输出根节点的值,将所遇到的根节点入栈,继续向根节点的左边访问(输出-入栈-左子树),直到再无左子树则将此时的根节点出栈并访问它的右子树的“根”,直到再无右子树,则继续出栈。
【数据结构】二叉树的非递归遍历(完整代码)_第1张图片

while (StackEmpty(&st) != 0 || cur != NULL)
{
	while (cur != NULL)
	{
		printf("%c ", cur->_data);
		StackPush(&st, cur);
		cur = cur->_left;
	}
	BTNode* top = StackTop(&st);
	StackPop(&st);
	cur = top->_right;
}

中序:

while (StackEmpty(&st) != 0 || cur != NULL)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		printf("%c ", top->_data);
		StackPop(&st);
		cur = top->_right;
	}

**后序:**要判断此时访问的结点之前是否访问过!!

while (cur || StackEmpty(&st) != 0)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		if (top->_right == NULL || top->_right == prev)
		{
			StackPop(&st);
			printf("%c ", top->_data);
			prev = top;
		}
		else cur = top->_right;
	}

完整代码

BT.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __TREE_H__
#define __TREE_H__ 

#include 
#include 
#include 
#include 

typedef char BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;
}BTNode;

//创建树
BTNode *BinaryTreeCreate(BTDataType*a, int*pi);
void BinaryTreeDestory(BTNode* root);
//非递归遍历
void BinaryPrevOrder(BTNode* root);
void BinaryInOrder(BTNode* root);
void BinaryPostOrder(BTNode* root);

void Test();

typedef BTNode* STDataType;

typedef struct Stack
{
	STDataType* _a;
	int _top;
	int _capacity;
}Stack;

void StackInit(Stack* ps);
void StackDestory(Stack* ps);

void StackPush(Stack *ps, STDataType x);
void StackPop(Stack* ps);
STDataType  StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack* ps);

#endif//__TREE_H__
BT.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BT.h"


BTNode *BinaryTreeCreate(BTDataType*a, int* pi)
{
	if (a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}
	BTNode* root = (BTNode*)malloc(sizeof(BTNode));
	root->_left = root->_right = NULL;
	root->_data = a[*pi];
	(*pi)++;
	root->_left = BinaryTreeCreate(a, pi);
	root->_right = BinaryTreeCreate(a, pi);
	return root;
}

void BinaryTreeDestory(BTNode* root)
{
	if (root)
	{
		BinaryTreeDestory(root->_left);
		BinaryTreeDestory(root->_right);
		free(root);
		root = NULL;
	}
}
void BinaryPrevOrder(BTNode* root)
{
	Stack st;
	StackInit(&st);
	BTNode* cur = root;
	while (StackEmpty(&st) != 0 || cur != NULL)
	{
		while (cur != NULL)
		{
			printf("%c ", cur->_data);
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		StackPop(&st);
		cur = top->_right;
	}
}

void BinaryInOrder(BTNode* root)
{
	Stack st;
	StackInit(&st);
	BTNode* cur = root;
	while (StackEmpty(&st) != 0 || cur != NULL)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		printf("%c ", top->_data);
		StackPop(&st);
		cur = top->_right;
	}
}

void BinaryPostOrder(BTNode* root)
{
	BTNode* cur = root;
	BTNode* prev = NULL;
	Stack st;
	StackInit(&st);
	while (cur || StackEmpty(&st) != 0)
	{
		while (cur != NULL)
		{
			StackPush(&st, cur);
			cur = cur->_left;
		}
		BTNode* top = StackTop(&st);
		if (top->_right == NULL || top->_right == prev)
		{
			StackPop(&st);
			printf("%c ", top->_data);
			prev = top;
		}
		else cur = top->_right;
	}
}


void Test()
{
	char a[20] = "ABD##E#H##CF##G##";
	int pi = 0;
	int i = 0;
	BTNode* root = BinaryTreeCreate(a, &pi);
	printf("树创建成功!\n");
	printf("树前序遍历的结果是: ");
	BinaryPrevOrder(root);
	printf("\n");
	printf("树中序遍历的结果是: ");
	BinaryInOrder(root);
	printf("\n");
	printf("树后序遍历的结果是: ");
	BinaryPostOrder(root);
	printf("\n");
	BinaryTreeDestory(root);
	printf("销毁成功!\n");
}

ST.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "BT.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->_a = NULL;
	ps->_capacity = 0;
	ps->_top = 0;
}

void StackDestory(Stack* ps)
{
	assert(ps);
	ps->_capacity = 0;
	ps->_top = 0;
	free(ps->_a);
	ps->_a = NULL;
}

void StackPush(Stack *ps, STDataType x)
{
	assert(ps);
	if (ps->_top == ps->_capacity)
	{
		size_t newcapacity = (ps->_capacity == 0 ? 4 : ps->_capacity * 2);
		ps->_a = (STDataType*)realloc(ps->_a, sizeof(STDataType)*newcapacity);
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top] = x;
	ps->_top++;
}

void StackPop(Stack* ps)
{
	assert(ps&&ps->_top>0);
	ps->_top--;
}

STDataType  StackTop(Stack* ps)
{
	assert(ps&&ps->_top>0);
	return ps->_a[ps->_top - 1];
}

int StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->_top == 0)
		return 0;
	else return 1;
}

int StackSize(Stack* ps)
{
	assert(ps);
	STDataType size = ps->_top;
	return size;
}

void StackPrint(Stack* ps)
{
	assert(ps);
	while (StackEmpty(ps) != 0)
	{
		printf("%d ", StackTop(ps));
		StackPop(ps);
	}
	printf("\n");
}
main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "BT.h"

int main()
{
	printf("      *******二叉树的实现******\n");
	Test();
	printf("\n");
	system("pause");
	return 0;
}

你可能感兴趣的:(数据结构的模拟实现,C++,/,数据结构)