经典算法之非递归算法实现二叉树前、中、后序遍历

/************************
author's email:wardseptember@gmail.com
date:2017.12.24
非递归算法实现二叉树前、中、后序遍历
************************/
#include<iostream>
using namespace std;
#define maxsize 100
typedef struct BTNode
{
	char data;
	struct BTNode *lchild;
	struct BTNode *rchild;

}BTNode,*BTree;
BTree createTree();
void preorderNonrecursion(BTNode *p);
void inorderNonrecursion(BTNode *p);
void postorderNonrecursion(BTNode *p);
int main()
{
	BTree ptree;
	ptree = createTree();

	cout << "非递归算法实现二叉树前序遍历:";
	preorderNonrecursion(ptree);
	cout << endl;

	cout << "非递归算法实现二叉树中序遍历:";
	inorderNonrecursion(ptree);
	cout << endl;


	cout << "非递归算法实现二叉树后序遍历:";
	postorderNonrecursion(ptree);
	cout << endl;

	return 0;
}
BTree createTree() 
{
	BTree pA = (BTNode *)malloc(sizeof(BTNode));
	BTree pB = (BTNode *)malloc(sizeof(BTNode));
	BTree pC = (BTNode *)malloc(sizeof(BTNode));
	BTree pD = (BTNode *)malloc(sizeof(BTNode));
	BTree pE = (BTNode *)malloc(sizeof(BTNode));
	BTree pF = (BTNode *)malloc(sizeof(BTNode));
	BTree pG = (BTNode *)malloc(sizeof(BTNode));
	BTree pH = (BTNode *)malloc(sizeof(BTNode));
	BTree pI = (BTNode *)malloc(sizeof(BTNode));


	pA->data = 'A';
	pB->data = 'B';
	pC->data = 'C';
	pD->data = 'D';
	pE->data = 'E';
	pF->data = 'F';
	pG->data = 'G';
	pH->data = 'H';
	pI->data = 'I';

	pA->lchild = pC;
	pA->rchild = pB;
	pC->lchild = pD;
	pC->rchild = pE;
	pB->rchild = pF;
	pB->lchild = NULL;
	pD->lchild = pG;
	pD->rchild = NULL;
	pE->lchild = NULL;
	pE->rchild = pH;
	pF->lchild = pI;
	pF->rchild = NULL;
	pG->lchild = pG->rchild = NULL;
	pH->lchild = pH->rchild = NULL;
	pI->lchild = pI->rchild = NULL;

	return pA;
}
//非递归算法实现二叉树前序遍历
void preorderNonrecursion(BTNode *p) 
{
	if (p != NULL) 
	{
		BTNode *Stack[maxsize];
		int top = -1;
		BTNode *q;
		Stack[++top] = p;
		while (top != -1)
		{
			q = Stack[top--];
			cout << q ->data;
			if (q->rchild != NULL)
				Stack[++top] = q->rchild;
			if (q->lchild != NULL)
				Stack[++top] = q->lchild;
		}
	}
}
//非递归算法实现二叉树中序遍历
void inorderNonrecursion(BTNode *p) 
{
	if (p != NULL) 
	{
		BTNode *Stack[maxsize];
		int top = -1;
		BTNode *q;
		q = p;
		while (top != -1 || q != NULL)
		{
			while (q!= NULL) 
			{      //左孩子存在,则左孩子进栈
				Stack[++top] = q;
				q = q->lchild;

			}
			if (top != -1) {
				q = Stack[top--];
				cout << q->data;
				q = q->rchild;
			}
		}
	}
}
/*用两个栈实现非递归二叉树后序遍历,逆后序遍历是将前序遍历过程对左右子树遍历顺序交换,第二个栈用于把逆后序遍历变成后序遍历*/
void postorderNonrecursion(BTNode *p)
{
	if (p != NULL)
	{
		BTNode *Stack1[maxsize];
		int top1 = -1;
		BTNode *Stack2[maxsize];
		int top2 = -1;
		BTNode *q = NULL;
		Stack1[++top1] = p;
		while (top1 != -1)
		{
			q = Stack1[top1--];
			Stack2[++top2] = q;//栈一出栈元素进入栈二
			if (q->lchild != NULL)
				Stack1[++top1] = q->lchild;
			if (q->rchild != NULL)
				Stack1[++top1] = q->rchild;
		}
		while (top2!= -1)
		{
			q = Stack2[top2--];
			cout << q->data;
		}
	}
}
    原文作者:递归算法
    原文地址: https://blog.csdn.net/wardseptember/article/details/78883843
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞