二叉树遍历递归与非递归实现

本文详细介绍了二叉树的创建、判断是否为空以及四种遍历方式:先序、中序、后序和层序遍历。分别用递归和非递归方式实现了这些功能,并对比了它们的优缺点。

在这里插入图片描述

递归实现:

/*样例:3 4 6 0 0 7 8 0 0 0 0
  前序:3 4 6 7 8
  中序:6 4 8 7 3
  后序:6 8 7 4 3*/
#include <iostream>
using namespace std;
typedef class TreeNode BinTree;


class TreeNode
{
public:
	TreeNode();
	~TreeNode() {};
	void CreateBinaryTree(BinTree*& BT);//创建
	bool IsEmpty(BinTree* BT);//判断是否为空
	void PreOrderTraversal(BinTree* BT);//先序遍历
	void InOrderTraversal(BinTree* BT);//中序遍历
	void PostOrderTraversal(BinTree* BT);//后序遍历
	void LevelOrderTraversal(BinTree* BT);//层序遍历
private:
	int Data;//存值
	BinTree* LTree;//左子树
	BinTree* RTree;//右子树
};
TreeNode::TreeNode()
{
	Data = 0;
	LTree = NULL;
	RTree = NULL;
}

//创建
void TreeNode:: CreateBinaryTree(BinTree* &BT)//引用
{
	BT = new BinTree;
	//BT = (BinTree*)malloc(sizeof(BinTree));
	cin >> BT->Data;
	if (BT->Data == 0)
		BT = NULL;
	else
	{
		CreateBinaryTree(BT->LTree);
		CreateBinaryTree(BT->RTree);
	}
	return ;
}
//判断是否空
bool TreeNode:: IsEmpty(BinTree* BT)
{
	if (BT == NULL)
		return true;
	else
		return false;
}
//前序遍历
void TreeNode::PreOrderTraversal(BinTree* BT)
{
	if (BT)
	{
		cout << BT->Data<<" ";
		PreOrderTraversal(BT->LTree);
		PreOrderTraversal(BT->RTree);
	}
}
//中序遍历
void TreeNode::InOrderTraversal(BinTree* BT)
{
	if (BT)
	{
		InOrderTraversal(BT->LTree);
		cout << BT->Data<<" ";
		InOrderTraversal(BT->RTree);
	}
}
//后序遍历
void TreeNode::PostOrderTraversal(BinTree* BT)
{
	if (BT)
	{
		PostOrderTraversal(BT->LTree);
		PostOrderTraversal(BT->RTree);
		cout << BT->Data << " ";
	}
}
int main()
{
	BinTree T ;//声明类型T
	BinTree* BT;//声明一个二叉树指针
	cout << "创建二叉树:"<<endl;
	T.CreateBinaryTree(BT);
	if (T.IsEmpty(BT))
		cout << "树为空"<<endl;
	else
		cout << "树不空"<<endl;
	cout << "前序遍历:";
	T.PreOrderTraversal(BT);
	cout << endl;
	cout << "中序遍历:";
	T.InOrderTraversal(BT);
	cout << endl;
	cout << "后序遍历:";
	T.PostOrderTraversal(BT);
	cout << endl;
	return 0;
}

运行:

在这里插入图片描述
非递归实现:

#include <iostream>
#include <stack>//堆栈和队列实现
#include <queue>
using namespace std;
typedef class TreeNode BinTree;
typedef class TreeNode* Binary_Tree;

class TreeNode
{
public:
	TreeNode();
	~TreeNode() {};
	void CreateBinaryTree(Binary_Tree& BT);//创建
	bool IsEmpty(Binary_Tree BT);//判断是否空
	int Height(Binary_Tree BT);//树高
	void PreOrderTraversal(Binary_Tree BT);//先序
	void InOrederTraversal(Binary_Tree BT);//中序
	void PostOrderTraversal(Binary_Tree BT);//后序
	void LevelOrderTraversal(Binary_Tree BT);//层序
private:
	int Data;
	BinTree* LTree;
	BinTree* RTree;
};
TreeNode::TreeNode()
{
	Data = 0;
	LTree = NULL;
	RTree = NULL;
}
//创建
void TreeNode::CreateBinaryTree(Binary_Tree &BT)//引用
{
	BT = new BinTree;
	cin >> BT->Data;
	if (BT->Data == 0)
		BT = NULL;
	else
	{
		CreateBinaryTree(BT->LTree);
		CreateBinaryTree(BT->RTree);
	}
}
//判断是否空
bool TreeNode::IsEmpty(Binary_Tree BT)
{
	if (BT==NULL)
		return true;
	else
		return false;
}
//树高
int TreeNode::Height(Binary_Tree BT)
{
	int HL, HR, Maxth;
	if (BT)
	{
		HL = Height(BT->LTree);
		HR = Height(BT->RTree);
		Maxth = (HL > HR) ? HL : HR;
		return Maxth + 1;//最大子树高度加1为整棵树高度
	}
	else
		return 0;
}
//先序
void TreeNode::PreOrderTraversal(Binary_Tree BT)
{
	stack<Binary_Tree> s;//声明树类型的栈
	while (BT || !s.empty())//树不空或栈不空
	{
		while (BT)
		{
			s.push(BT);
			cout << BT->Data<<" ";
			BT = BT->LTree;//遍历左边
		}
		if (!s.empty())
		{
			BT= s.top();
			 s.pop();
			 BT = BT->RTree;
		}
	}
}
//中序
void TreeNode::InOrederTraversal(Binary_Tree BT)
{
	stack<Binary_Tree>s;
	while (BT || !s.empty())
	{
		while (BT)//树不空向左遍历
		{
			s.push(BT);
			BT = BT->LTree;
		}
		if (!s.empty())
		{
			BT = s.top();
			cout << BT->Data<<" ";
			s.pop();
			BT = BT->RTree;
		}

	}
}
//后序
void TreeNode::PostOrderTraversal(Binary_Tree BT)
{
	stack<Binary_Tree>s;
	Binary_Tree PT=NULL;//声明一个临时结点
	while (BT || !s.empty())
	{
		while (BT)
		{
			s.push(BT);
			BT=BT->LTree;
		}
		if (!s.empty())
		{
			BT = s.top();
			s.pop();
			if (BT->RTree == NULL||BT->RTree==PT)//右子树为空或右子树已经输出
			{
				cout << BT->Data<<" ";
				 PT = BT;//记录当前已经遍历的右子树
				BT = NULL;
			}
			else
			{
				s.push(BT);
				BT = BT->RTree;
			}
		}
	}
}
//层序
void TreeNode::LevelOrderTraversal(Binary_Tree BT)
{
	queue<Binary_Tree>s;//声明队列
	s.push(BT);
		while (!s.empty())//队列不空循环从左到右入列、出列
		{
			BT = s.front();
			cout << BT->Data << " ";
			s.pop();

			if (BT->LTree)
			{
				s.push(BT->LTree);
			}
			if (BT->RTree)
			{
				s.push(BT->RTree);
		}
	}
}
int main()
{
	TreeNode T;
	Binary_Tree BT;
	cout << "创建一个二叉树:" << endl;
	T.CreateBinaryTree(BT);
	cout << endl;
	if (T.IsEmpty(BT))
		cout << "树空" << endl;
	else
		cout << "树不空" << endl;
	cout << "树高:" << T.Height(BT) << endl;
	cout << "先序:";
	T.PreOrderTraversal(BT);
	cout << endl;
	cout << "中序:";
	T.InOrederTraversal(BT);
	cout << endl;
	cout << "后序:";
	T.PostOrderTraversal(BT);
	cout << endl;
	cout << "层序:";
	T.LevelOrderTraversal(BT);
	cout << endl;
	return 0;
}

运行:

在这里插入图片描述

递归算法简洁,但其开销较大,效率低;若二叉树较深则选择非递归算法效率更高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值