二叉树的非递归遍历 C/C++

前面用递归的方式实现了二叉树的遍历:https://blog.csdn.net/feel_myself_is_lowB/article/details/106897311

下面用非递归方式实现二叉树遍历:

先来个节点的结构体

typedef struct BiTreeNode
{
	char data;  //数据
	struct BiTreeNode *left;  //左孩子
	struct BiTreeNode *right;  //右孩子
}Node;

一、先序遍历

//先序遍历
void preOrder(Node *root)
{
	if (NULL == root)
	{
		return;
	}
	stack<Node *> sNode;
	Node *cur = root;
	while (NULL != cur || !sNode.empty())
	{
		if(NULL != cur)
		{
			sNode.push(cur);
		}
		if (!sNode.empty())
		{
			cur = sNode.top();
			sNode.pop();
			cout << cur->data << " ";
			Node *right = cur->right;
			if (right)
				sNode.push(right);
			cur = cur->left;
		}
	}
	cout << endl;
}

二、中序遍历

//中序遍历
void inOrder(Node *root)
{
	if (NULL == root)
	{
		return;
	}
	stack<Node*> sNode;
	Node *cur = root;
	while (NULL != cur || !sNode.empty())
	{
		if (NULL != cur)
		{
			sNode.push(cur);
			cur = cur->left;
			
		}
		else
		{
			cur = sNode.top();
			sNode.pop();
			cout << cur->data<<" ";
			cur = cur->right;
		}
	}
	cout << endl;
}

三、后序遍历

//后序遍历
void laOrder(Node *root)
{
	if (NULL == root)
	{
		return;
	}
	stack<Node*> sNode;
	Node *cur = root;
	Node *pIsRead = NULL;
	while (cur != NULL)
	{
		sNode.push(cur);
		cur = cur->left;
	}
	while (!sNode.empty())
	{
		cur = sNode.top();
		sNode.pop();
		if (NULL == cur->right || cur->right == pIsRead)
		{
			cout << cur->data << " " ;
			pIsRead = cur;
		}
		else
		{
			sNode.push(cur);
			cur = cur->right;
			
			while (NULL != cur)
			{
				sNode.push(cur); 
				cur = cur->left;
			}
		}
	}
	cout << endl;
}

四、层次遍历:比较特殊 用的队列

//层次遍历
void leOrder(Node *root)
{
	if (NULL == root)
	{
		return;
	}
	queue<Node*> qNode;
	Node *cur = root;
	qNode.push(cur);
	while (!qNode.empty())
	{
		cur = qNode.front();
		cout << cur->data << " ";
		qNode.pop();
		Node *left = cur->left;
		if (left)
		{
			qNode.push(left);
		}
		Node *right = cur->right;
		if (right)
		{
			qNode.push(right);
		}
	}
}

测试主函数代码:

#include<iostream>
#include<stack>
#include<queue>
using namespace  std;


int main()
{
	//准备数据
	Node nodeA = { 'A',NULL,NULL };
	Node nodeB = { 'B',NULL,NULL };
	Node nodeC = { 'C',NULL,NULL };
	Node nodeD = { 'D',NULL,NULL };
	Node nodeE = { 'E',NULL,NULL };
	Node nodeF = { 'F',NULL,NULL };
	Node nodeG = { 'G',NULL,NULL };

	//建立关系
	nodeA.left = &nodeB;
	nodeA.right = &nodeC;

	nodeB.left = &nodeD;
	nodeB.right = &nodeE;

	nodeC.left = &nodeF;
	nodeC.right = &nodeG;


	//调用递归函数
	cout << "前序遍历--------------------------------" << endl;
	preOrder(&nodeA);
	cout << "中序遍历--------------------------------" << endl;
	inOrder(&nodeA);
	cout << "后序遍历--------------------------------" << endl;
	laOrder(&nodeA);
	printf("\n");


	system("pause");
	return 0;
}

二叉树其实就是这么一棵二叉树。

运行结果:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值