二叉树代码--各个遍历递归与非递归--层次--Z型--reverseZ型

本文总结的包括先序、中序、后序的递归与非递归写法,此外,还有常见的Z型、层次、以及reverse-Z型,有不清楚的地方欢迎置评。

#include<stack>
#include<limits.h>
#include<queue>
using namespace std;

typedef struct BTtree{
	BTtree* left;
	BTtree* right;
	char val;
	BTtree() : val(0), left (nullptr), right(nullptr) {}
	BTtree(char data) : val(data), left (nullptr), right(nullptr) {}
	BTtree(char x, BTtree *left, BTtree *right) : val(x), left(left), right(right) {}
}BTnode;

//递归
void Preorder(BTtree* root)  //先序遍历
{
	if (root == nullptr) return;
	cout<<root->val<<" ";
	Preorder(root->left);
	Preorder(root->right);
}

void Inorder(BTnode* root)  //中序遍历
{
	if (root == nullptr)  return;
	Inorder(root->left);
	cout<<root->val<<" ";
	Inorder(root->right);
}

void Pastorder(BTtree* root)  //后序遍历
{
	if (root == nullptr) return;
	Pastorder(root->left);
	Pastorder(root->right);
	cout<<root->val<<" ";
}


//非递归方法
void Nicepreorder(BTnode* root)   //前序遍历
{
	if (root == nullptr) return;
	stack<BTnode*> st;
	BTnode* p =root;
	st.push(root);
	while (!st.empty())
	{
		p = st.top(); st.pop();
		cout<<p->val<<" ";
		if (p->right!=nullptr)
		{
			st.push(p->right);	
		}
		if (p->left!=nullptr)
		{
			st.push(p->left);
		}
	}
}

void Niceinorder(BTnode* root)  //中序遍历
{
	if (root == nullptr) return;
	stack<BTnode*> st;
	BTnode* p = root;
	while (!st.empty() || p!=nullptr)
	{
		while (p!=nullptr)
		{
			st.push(p);
			p=p->left;
		}
		p=st.top(); st.pop();
		cout<<p->val<<" ";
		p=p->right;
	}
}

void Nicepastorder(BTnode* root)   //后序遍历
{
	if (root == nullptr) return;
	stack<BTnode*> st;
	BTnode* flag = nullptr;
	BTnode* p = root;
	while (!st.empty()|| p!=nullptr)
	{
		while (p!=nullptr)
		{
			st.push(p);
			p=p->left;
		}
		 p = st.top(); st.pop();
		 if (p->right == nullptr || p->right == flag)
		 {
			cout<<p->val<<" ";
			flag = p;
			p=nullptr; //********
		 }
		 else
		 {
			st.push(p);
			p=p->right;
		 }
	}
}

void Nicelevelorder(BTnode* root) //层次遍历
{
	if (root == nullptr) return;
	queue<BTnode*> qu;
	qu.push(root);
	BTnode* p = root;
	while (!qu.empty())
	{
		p = qu.front(); qu.pop();
		cout<<p->val<<" ";
		if (p->left!=nullptr)
		{
			qu.push(p->left);
		}
		if (p->right != nullptr)
		{
			qu.push(p->right);
		}
	}
}

void NiceZ(BTnode* root)  // Z字型遍历
{
	if (root == nullptr) return;
	stack<BTnode*> st;
	stack<BTnode*> st2;
	st.push(root);
	BTnode* p = root;
	
	while (!st.empty() || !st2.empty())
	{
			while (!st.empty())
		{
			p=st.top(); st.pop();
			cout<<p->val<<" ";
			if (p->right!=nullptr)
			{
				st2.push(p->right);
			}
			if  (p->left!=nullptr)
			{
				st2.push(p->left);
			}
		}
			while (!st2.empty())
		{
			p=st2.top(); st2.pop();
			cout<<p->val<<" ";
			if  (p->left!=nullptr)
			{
				st.push(p->left);
			}
			if (p->right!=nullptr)
			{
				st.push(p->right);
			}
		}
		}
}

void ReverseZ(BTnode* root)
{
	if (root == nullptr) return;
	stack<BTnode*> st;
	stack<BTnode*> st2;
	BTnode* p = root;
	st.push(root);
	int index = 0;
	while (!st.empty() || !st2.empty())
	{ 
		while (!st.empty())
		{
			p=st.top(); st.pop();
			cout<<p->val<<" ";
			if (p->left!=nullptr)
			{
				st2.push(p->left);
			}
			if (p->right!=nullptr)
			{
				st2.push(p->right);
			}
		}
		while (!st2.empty())
		{
			p=st2.top(); st2.pop();
			cout<<p->val<<" ";
			if (p->right!=nullptr)
			{
				st.push(p->right);
			}
			if (p->left!=nullptr)
			{
				st.push(p->left);
			}
		}
	}
}

清商随风发,中曲正徘徊。
一弹再三叹,慷慨有余哀。
不惜歌者苦,但伤知音稀。
愿为双鸿鹄,奋翅起高飞。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值