二叉树的后序遍历非递归算法相比先序和中序遍历非递归算法要复杂,
本文介绍后序遍历非递归三种算法,
- 使用2个栈
- 使用1个栈
- 非递归也不用栈
下面以图中所给树为例,介绍三种算法的实现
a).使用2个栈,S1, S2
算法基本思想:
- 根节点入栈S1
- 当栈S1非空时循环,S1栈顶元素出栈,并入栈S2,然后访问被出栈元素,若有左孩子,则左孩子入栈S1,若有右孩子,则右孩子入栈S1
- 栈S1为空时,栈S2所有元素依次出栈所得序列即为后序遍历序列
算法代码:
void postOrderTraversal(BiTreeNode *root){
stack<BiTreeNode* > s1, s2;
s1.push(root);
while(!s1.empty()){
BiTreeNode *cur = s1.top(); // cur指向被出栈元素
s1.pop();
s2.push(cur);
if(cur->lchild) s1.push(cur->lchild);
if(cur->rchild) s1.push(cur->rchild);
}
while(!s2.empty()){
cout << s2.top()->val << " ";
s2.pop();
}
}
C++实现完整代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct BiTreeNode{
int val;
struct BiTreeNode* lchild;
struct BiTreeNode* rchild;
BiTreeNode(int val){
this->val = val;
lchild = rchild = NULL;
}
};
void postOrderTraversal(BiTreeNode *root){
stack<BiTreeNode* > s1, s2;
s1.push(root);
while(!s1.empty()){
BiTreeNode *cur = s1.top();
s1.pop();
s2.push(cur);
if(cur->lchild) s1.push(cur->lchild);
if(cur->rchild) s1.push(cur->rchild);
}
while(!s2.empty()){
cout << s2.top()->val << " ";
s2.pop();
}
}
int main()
{
BiTreeNode *root = new BiTreeNode(1);
root->lchild = new BiTreeNode(2);
root->rchild = new BiTreeNode(3);
root->lchild->lchild = new BiTreeNode(4);
root->lchild->rchild = new BiTreeNode(5);
root->rchild->lchild = new BiTreeNode(6);
root->rchild->rchild = new BiTreeNode(7);
cout << "the postorder of the tree is:" << endl;
postOrderTraversal(root);
return 0;
}
程序运行结果:
.
b).使用1个栈,S
算法基本思想:
- 创建一个空栈S,并让根节点 root 入栈,当栈非空时重复步骤2,3
-
根节点root非空时,执行如下操作:
a.) root 若有右孩子,则右孩子入栈,然后root 入栈
b.) 设置 root 为其左孩子 (左孩子若为空则置root = NULL) -
出栈1个元素,并将其设为 root,执行如下操作:
a.) 如果被弹出栈元素root有右孩子节点,且当前栈顶元素为其右孩子节点,则弹出当前栈顶元素,root 重新入栈,并设置 root 为 root 的右孩子节点
b.)否则打印 root 数据并且设置 root 为NULL
算法代码如下
void postOrderTraversal(BiTreeNode *root){
if(!root) return;
stack<BiTreeNode* > s;
s.push(root);
while(!s.empty()){
while(root){
if(root->rchild)
s.push(root->rchild);
s.push(root);
root = root->lchild;
} //here root == NULL
root = s.top();
s.pop();
if(root->rchild && root->rchild == s.top()){
s.pop();
s.push(root);
root = root->rchild;
}else{
cout << root->val << " ";
root = NULL;
}
}
}
C++实现完整代码如下:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct BiTreeNode{
int val;
struct BiTreeNode* lchild;
struct BiTreeNode* rchild;
BiTreeNode(int val){
this->val = val;
lchild = rchild = NULL;
}
};
void postOrderTraversal(BiTreeNode *root){
if(!root) return;
stack<BiTreeNode* > s;
s.push(root); //根节点入栈
while(!s.empty()){
while(root){
if(root->rchild) //有右孩子,右孩子入栈
s.push(root->rchild);
s.push(root);
root = root->lchild;
}
root = s.top();
s.pop();
if(root->rchild && root->rchild == s.top()){
s.pop();
s.push(root);
root = root->rchild;
}else{
cout << root->val << " ";
root = NULL;
}
}
}
int main()
{
BiTreeNode *root = new BiTreeNode(1);
root->lchild = new BiTreeNode(2);
root->rchild = new BiTreeNode(3);
root->lchild->lchild = new BiTreeNode(4);
root->lchild->rchild = new BiTreeNode(5);
root->rchild->lchild = new BiTreeNode(6);
root->rchild->rchild = new BiTreeNode(7);
cout << "the postorder of the tree is" << endl;
postOrderTraversal(root);
return 0;
}
程序运行结果:
与该算法类似,另外一种使用一个栈的后序遍历算法,让 root 节点连续入栈两次,当发现出栈元素 root 和当前栈顶元素相等时,设置root = root->rchild
算法基本思想:
无限循环如下步骤:
- 创建一个空栈S
- 当根节点 root 不为空时,root 连续入栈两次,设置 root 为其左孩子
- 若栈为空,return跳出无限循环
-
若栈不为空,
a.) 出栈1个元素,并置为 root,
b.) 此时栈若不为空且root 若和当前栈顶元素相等,则置 root 为其右孩子
c.)否则打印root元素值,并置 root 为NULL
算法代码如下:
void postOrderTraversal(BiTreeNode *root){
if(!root) return;
stack<BiTreeNode* > s;
while(true){
while(root){
s.push(root);
s.push(root);
root = root->lchild;
} //here root == NULL
if(s.empty()) return; //jump out the loop
root = s.top();
s.pop();
if(!s.empty() && root == s.top()){
root = root->rchild;
}else{
cout << root->val << " ";
root = NULL;
}
}
}
C++实现完整代码如下:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct BiTreeNode{
int val;
struct BiTreeNode* lchild;
struct BiTreeNode* rchild;
BiTreeNode(int val){
this->val = val;
lchild = rchild = NULL;
}
};
void postOrderTraversal(BiTreeNode *root){
if(!root) return;
stack<BiTreeNode* > s;
while(true){
while(root){
s.push(root);
s.push(root);
root = root->lchild;
} //here root == NULL
if(s.empty()) return;
root = s.top();
s.pop();
if(!s.empty() && root == s.top()){
root = root->rchild;
}else{
cout << root->val << " ";
root = NULL;
}
}
}
int main()
{
BiTreeNode *root = new BiTreeNode(1);
root->lchild = new BiTreeNode(2);
root->rchild = new BiTreeNode(3);
root->lchild->lchild = new BiTreeNode(4);
root->lchild->rchild = new BiTreeNode(5);
root->rchild->lchild = new BiTreeNode(6);
root->rchild->rchild = new BiTreeNode(7);
cout << "the postorder of the tree is" << endl;
postOrderTraversal(root);
return 0;
}
程序运行结果:
.
c).既不使用栈,也不使用递归的后序遍历算法
可以通过在二叉树结点的数据结构里定义一个 bool 类型的 visited 变量,为true表示当前节点已被遍历输出过,否则尚未被遍历。visted置为true相当于当前节点“被删除”了,
算法基本思想:
- 用 curr 当前指针指向根节点 root
-
当根节点未被遍历时循环:每次循环的对象都是所有节点尚未被遍历的子树
a.) 若左子树未被遍历,遍历左子树,即 curr = curr->lchild
b.) 若右子树未被遍历,遍历右子树,即 curr = curr->rchild
c.) 否则左右子树都已被遍历,输出当前节点值,并通过置 visited 为true将当前节点“删除”,当前节点重新指向根节点root,开始新一轮遍历
算法代码如下:
void postOrderTraversal(BiTreeNode *root){
BiTreeNode *curr = root;
while(curr && curr->visited == false){ //根节点未被遍历时循环
if(curr->lchild && curr->lchild->visited == false)
curr = curr->lchild; //左子树未被遍历,遍历左子树
else if(curr->rchild && curr->rchild->visited == false)
curr = curr->rchild; //右子树未被遍历,遍历右子树
else { //否则说明为左右子树都被遍历过,打印当前节点,
cout << curr->val << " ";
curr->visited = true; //打印遍历完,置visited为true,相当于"删除了" 该结点
curr = root;
}
}
}
C++完整实现代码如下:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct BiTreeNode{
int val;
struct BiTreeNode* lchild;
struct BiTreeNode* rchild;
bool visited;
BiTreeNode(int val){
this->val = val;
lchild = rchild = NULL;
this->visited = false;
}
};
void postOrderTraversal(BiTreeNode *root){
BiTreeNode *curr = root;
while(curr && curr->visited == false){
if(curr->lchild && curr->lchild->visited == false)
curr = curr->lchild;
else if(curr->rchild && curr->rchild->visited == false)
curr = curr->rchild;
else{
cout << curr->val << " ";
curr->visited = true;
curr = root;
}
}
}
int main()
{
BiTreeNode *root = new BiTreeNode(1);
root->lchild = new BiTreeNode(2);
root->rchild = new BiTreeNode(3);
root->lchild->lchild = new BiTreeNode(4);
root->lchild->rchild = new BiTreeNode(5);
root->rchild->lchild = new BiTreeNode(6);
root->rchild->rchild = new BiTreeNode(7);
cout << "the postorder of the tree is" << endl;
postOrderTraversal(root);
return 0;
}
程序运行结果如图: