普通二叉树的前序中序后序便利,包括递归跟非递归,一共六种,当作复习
#include <bits/stdc++.h>
using namespace std;
priority_queue<long> min_pq;
priority_queue<long, vector<long> , greater<long> > max_pq;
struct node{
char ch;
node *l;
node *r;
bool visited; //记录节点是否被访问过,用于后续遍历
node(char c){
ch = c;
l = NULL;
r = NULL;
visited = false;
}
};
void inti_visited(node *root){
if(root!=NULL){
root->visited = false;
inti_visited(root->l);
inti_visited(root->r);
}
}
//递归前序
void pre_order(node *root){
if(root!=NULL){
cout << root->ch;
pre_order(root->l);
pre_order(root->r);
}
}
//非递归前序,借助栈,先将根入栈,然后每次弹出栈顶并输出,然后将右节点入栈,再将左节点入栈
void pre_order_stack(node *root){
stack<node*> s;
s.push(root);
while(!s.empty()){
node* tmp = s.top();
s.pop();
if(tmp==NULL) continue;
cout << tmp->ch;
s.push(tmp->r);
s.push(tmp->l);
}
}
//递归中序
void mid_order(node *root){
if(root!=NULL){
mid_order(root->l);
cout << root->ch;
mid_order(root->r);
}
}
//非递归中序, 节点不为空时,将该节点左子树的左节点都入栈,每次从栈顶取出节点并输出,并指向该节点的右子树
void mid_order_stack(node *root){
stack<node*> s;
node *tmp=root;
while(!s.empty() || tmp!=NULL){
while(tmp){
s.push(tmp);
tmp = tmp->l;
}
tmp = s.top();
cout << tmp->ch;
s.pop();
tmp = tmp->r;
}
}
//递归后序
void post_order(node *root){
if(root!=NULL){
post_order(root->l);
post_order(root->r);
cout << root->ch;
}
}
/*非递归后序,在节点中添加visited字段记录该节点是否访问过,只有某个节点的左右子节点都被访问过或者为空,
才将该节点出栈,并将该节点visited字段置为true,否则按右子节点,左子节点的顺序入栈*/
void post_order_stack(node *root){
stack<node*> s;
if(root==NULL) return;
s.push(root);
while(!s.empty()){
node* tmp = s.top();
if((tmp->l == NULL || tmp->l->visited)&&((tmp->r == NULL || tmp->r->visited))){
tmp->visited = true;
cout << tmp->ch;
s.pop();
}
if(tmp->r != NULL && tmp->r->visited==false) s.push(tmp->r);
if(tmp->l != NULL && tmp->l->visited==false) s.push(tmp->l);
}
}
node* build_tree(int n){
node* root = new node(char('A'+0));
queue<node *> q;
q.push(root);
for(int i=1; i < n; i+=2){
node *tmp=q.front();
q.pop();
tmp->l = new node(char('A'+i));
tmp->r = new node(char('A'+i+1));
q.push(tmp->l);
q.push(tmp->r);
}
return root;
}
void delete_tree(node* root){
if(root!=NULL){
delete_tree(root->l);
delete_tree(root->r);
delete root;
}
}
int main(){
node *root = build_tree(10);
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "递归前序 : ";
pre_order(root);
cout << endl;
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "非递归前序 : ";
pre_order_stack(root);
cout << endl;
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "递归中序 : ";
mid_order(root);
cout << endl;
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "非递归中序 : ";
mid_order_stack(root);
cout << endl;
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "递归后序 : ";
post_order(root);
cout << endl;
cout << setw(15) << setiosflags(ios::right) << setfill(' ') << "非递归后序 : ";
inti_visited(root);
post_order_stack(root);
cout << endl;
delete_tree(root);
}
//输出
递归前序 : ABDHIEJKCFG
非递归前序 : ABDHIEJKCFG
递归中序 : HDIBJEKAFCG
非递归中序 : HDIBJEKAFCG
递归后序 : HIDJKEBFGCA
非递归后序 : HIDJKEBFGCA
--------------------------------
Process exited after 0.381 seconds with return value 0
请按任意键继续. . .