本文总结的包括先序、中序、后序的递归与非递归写法,此外,还有常见的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);
}
}
}
}
清商随风发,中曲正徘徊。
一弹再三叹,慷慨有余哀。
不惜歌者苦,但伤知音稀。
愿为双鸿鹄,奋翅起高飞。