二叉树遍历实现(前中后层次/递归非递归)

本文深入探讨了二叉树的四种遍历方法:前序、中序、后序及层次遍历,提供了递归与非递归实现代码,帮助读者理解不同遍历方式的特点及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.前序遍历

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
struct BinaryTreeNode {
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};
//递归实现
void PreOrder1(BinaryTreeNode* pRoot) {
	if (pRoot != nullptr) {
		cout << pRoot->m_nValue << " ";
		if (pRoot->m_pLeft != nullptr) {
			PrintTree(pRoot->m_pLeft);
		}
		if (pRoot->m_pRight != nullptr) {
			PrintTree(pRoot->m_pRight);
		}
	}
}
//非递归实现
void PreOrder2(BinaryTreeNode* pRoot) {
	BinaryTreeNode* p = pRoot;
	stack<BinaryTreeNode*>s;
	s.push(p);
	//先压栈右子树再压栈左子树(这样访问的时候才是先访问左再访问右)
	while (!s.empty()) {
		p = s.top();
		s.pop();
		cout << p->m_nValue << " ";
		if (p->m_pRight != nullptr) {
			s.push(p->m_pRight);
		}
		if (p->m_pLeft != nullptr) {
			s.push(p->m_pLeft);
		}
	}
}

二.中序遍历

//递归实现
void InOrder1(BinaryTreeNode* pRoot) {
	if (pRoot != nullptr) {
		if (pRoot->m_pLeft != nullptr) {
			PrintTree(pRoot->m_pLeft);
		}
		cout << pRoot->m_nValue << " ";
		if (pRoot->m_pRight != nullptr) {
			PrintTree(pRoot->m_pRight);
		}
	}
}
//非递归实现
void InOrder2(BinaryTreeNode* pRoot) {
	BinaryTreeNode* p = pRoot;
	stack<BinaryTreeNode*>s;
	while (p != nullptr || !s.empty()) {
		if (p) {//1.向左压栈到最左边
			s.push(p);
			p = p->m_pLeft;
		}
		else {//2.回退出栈访问+向右压栈
			p = s.top();
			s.pop();
			cout << p->m_nValue << " ";
			p = p->m_pRight;
		}
	}
}

三.后序遍历

//递归实现
void PostOrder1(BinaryTreeNode* pRoot) {
	if (pRoot != nullptr) {
		if (pRoot->m_pLeft != nullptr) {
			PrintTree(pRoot->m_pLeft);
		}
		if (pRoot->m_pRight != nullptr) {
			PrintTree(pRoot->m_pRight);
		}
		cout << pRoot->m_nValue << " ";
	}
}
//非递归实现
void PostOrder2(BinaryTreeNode* pRoot) {
	BinaryTreeNode* p = pRoot;
	BinaryTreeNode* r = nullptr;//辅助指针
	stack<BinaryTreeNode*>s;
	while (p != nullptr || !s.empty()) {
		if (p) {//1.向左压栈到最左
			s.push(p);
			p = p->m_pLeft;
		}
		else {//2.回退分两种情况
			p = s.top();
			//第一种情况:右子树存在且未被访问=>继续压栈
			if (p->m_pRight != nullptr && p->m_pRight != r) {
				p = p->m_pRight;
				s.push(p);
				//※p已被压栈 故需向左 否则重复压栈
				p = p->m_pLeft;
			}
			//第二种情况:右子树不存在或者右子树访问过=>访问该节点
			else {
				s.pop();
				cout << p->m_nValue << " ";
				r = p;
				//※该节点左右子树均访问过 故设为NULL 继续出栈
				p = nullptr;
			}
		}
	}
}

四.层次遍历

void LevelOrder(BinaryTreeNode* pRoot) {
	BinaryTreeNode* p = pRoot;
	queue<BinaryTreeNode*>q;
	q.push(p);
	while (!q.empty()) {
		p = q.front();
		cout << p->m_nValue << " ";
		q.pop();
		if (p->m_pLeft != nullptr) {
			q.push(p->m_pLeft);
		}
		if (p->m_pRight != nullptr) {
			q.push(p->m_pRight);
		}
	}
}

测试补充代码

BinaryTreeNode* CreateBinaryTreeNode(double nValue)
{
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = nValue;
	pNode->m_pLeft = nullptr;
	pNode->m_pRight = nullptr;

	return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
	if (pParent != nullptr)
	{
		pParent->m_pLeft = pLeft;
		pParent->m_pRight = pRight;
	}
}

void DestroyTree(BinaryTreeNode* pRoot)
{
	if (pRoot != nullptr)
	{
		BinaryTreeNode* pLeft = pRoot->m_pLeft;
		BinaryTreeNode* pRight = pRoot->m_pRight;

		delete pRoot;
		pRoot = nullptr;

		DestroyTree(pLeft);
		DestroyTree(pRight);
	}
}
int main() {
	BinaryTreeNode* pNode1 = CreateBinaryTreeNode(8);
	BinaryTreeNode* pNode2 = CreateBinaryTreeNode(6);
	BinaryTreeNode* pNode3 = CreateBinaryTreeNode(10);
	BinaryTreeNode* pNode4 = CreateBinaryTreeNode(5);
	BinaryTreeNode* pNode5 = CreateBinaryTreeNode(7);
	BinaryTreeNode* pNode6 = CreateBinaryTreeNode(9);
	BinaryTreeNode* pNode7 = CreateBinaryTreeNode(11);

	ConnectTreeNodes(pNode1, pNode2, pNode3);
	ConnectTreeNodes(pNode2, pNode4, pNode5);
	ConnectTreeNodes(pNode3, pNode6, pNode7);
	//PostOrder2(pNode1);
	//InOrder2(pNode1);
	//PreOrder2(pNode1);
	LevelOrder(pNode1);
	DestroyTree(pNode1);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值