二叉树前中后序便利,递归/非递归

本文介绍了二叉树的前序、中序和后序遍历,包括递归和非递归两种实现方式,旨在巩固二叉树遍历的知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

普通二叉树的前序中序后序便利,包括递归跟非递归,一共六种,当作复习

#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
请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值