栈实现二叉树的前序、中序和后序遍历

前序遍历

思想

  前序遍历的顺序为DLR,因此先将根结点进栈,当栈不为空:当前结点出栈,访问该结点->如果当前结点的右子树不为空则进栈->如果当前结点的左子树不为空则进栈。

代码

void preorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	while(!stk.empty()) {
		BitTree* t = stk.top();
		stk.pop();
		cout << t->val << " ";
		if(t->right) {
			stk.push(t->right);
		}
		if(t->left) {
			stk.push(t->left);
		}
	}
	cout << endl;
}

中序遍历

思想

  中序遍历的顺序为LDR,因此我们需要先找到最左下方的结点,步骤:当前结点的左孩子不为空则一直进栈->当栈不为空时,栈顶的元素肯定是没有左孩子的,依据中序遍历的特点,直接输出。如果该结点存在右孩子,说明已经进行完LD,下一步应该进行R,所以应该将右孩子入栈,然后结束循环。

代码

void inorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	while(!stk.empty()) {
		while(stk.top()->left != NULL) {
			stk.push(stk.top()->left);
		}
		while(!stk.empty()) {
			BitTree* t = stk.top();
			stk.pop();
			cout << t->val << " "; //不存在左孩子,直接输出
			if(t->right != NULL) {
				stk.push(t->right);  //如果有右孩子,下一步就输出右孩子
				break;
			}
		}
	}
	cout << endl;
}

后序遍历

思想

  后序遍历的顺序为LRD,同样需要先找到最左下方的结点,这和中序遍历一致,所以步骤为:如果当前结点的左孩子不为空则一直进栈->当栈不为空时,当前结点肯定为最左下方的结点,如果上一次出栈并被访问的结点为当前结点的右孩子或者当前结点的右孩子为空,都说明已经进行完了LR的步骤,应该进行输出,否则如果右孩子不为空,则说明只进行完了L步骤,下一步应该进行R步骤,即将当前结点的右孩子入栈,然后结束循环。

代码

void postorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	BitTree* temp = NULL;
	while(!stk.empty()) {
		while(stk.top()->left != NULL) {
			stk.push(stk.top()->left);
		}
		while(!stk.empty()) {
			BitTree* t = stk.top();
			if(temp == t->right || t->right == NULL) {
				cout << t->val << " ";
				temp = t;
				stk.pop();
			}else if(t->right != NULL) {
				stk.push(t->right);
				break;
			}
		}
	}
	cout << endl;
} 

完整代码

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
using namespace std;

map<int, int> mp;

struct BitTree {
	int val;
	BitTree* left;
	BitTree* right;
	BitTree(int x) : val(x), left(NULL), right(NULL) {}
};

BitTree* create1(vector<int>& pre, int x1, int x2, int x3, int x4) {
	if(x1 > x2 || x3 > x4) {
		return NULL;
	}
	//创建根结点 
	int root_val = pre[x1];
	BitTree* root = new BitTree(root_val);
	int x = mp[root_val];  //中序遍历中根的位置
	int y = x - x3 + x1;
	//创建左子树
	root->left = create1(pre, x1 + 1, y, x3, x - 1);
	//创建右子树 
	root->right = create1(pre, y + 1, x2, x + 1, x4);
	return root; 
}

//中序遍历和后续遍历构建二叉树
BitTree* create2(vector<int>& post, int x1, int x2, int x3, int x4) {
	if(x1 > x2 || x3 > x4) {
		return NULL;
	}
	//创建根结点
	int root_val = post[x2];
	BitTree* root = new BitTree(root_val);
	int x = mp[root_val]; //中序遍历中的位置
	int y = x1 + x - 1 - x3;
	//创建左子树
	root->left = create2(post, x1, y, x3, x - 1);
	//创建右子树
	root->right = create2(post, y + 1, x2 - 1, x + 1, x4);
	return root; 
}

//层次遍历
void level(BitTree* T) {
	queue<BitTree*> que;
	que.push(T);
	while(!que.empty()) {
		BitTree* t = que.front();
		que.pop();
		cout << t->val << " ";
		if(t->left) {
			que.push(t->left);
		}
		if(t->right) {
			que.push(t->right);
		}
	}
}

//栈实现前序遍历
void preorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	while(!stk.empty()) {
		BitTree* t = stk.top();
		stk.pop();
		cout << t->val << " ";
		if(t->right) {
			stk.push(t->right);
		}
		if(t->left) {
			stk.push(t->left);
		}
	}
	cout << endl;
}

//栈实现中序遍历
void inorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	while(!stk.empty()) {
		//寻找左节点
		while(stk.top()->left != NULL) {
			stk.push(stk.top()->left);
		}
		while(!stk.empty()) {
			BitTree* t = stk.top();
			stk.pop();
			cout << t->val << " ";
			if(t->right != NULL) {
				stk.push(t->right);
				break;
			}
		}
	}
	cout << endl;
}

//栈实现后序遍历
void postorder(BitTree* T) {
	if(T == NULL) {
		return;
	}
	stack<BitTree*> stk;
	stk.push(T);
	BitTree* temp = NULL;
	while(!stk.empty()) {
		while(stk.top()->left != NULL) {
			stk.push(stk.top()->left);
		}
		while(!stk.empty()) {
			BitTree* t = stk.top();
			if(temp == t->right || t->right == NULL) {
				cout << t->val << " ";
				temp = t;
				stk.pop();
			}else if(t->right != NULL) {
				stk.push(t->right);
				break;
			}
		}
	}
	cout << endl;
} 

int main() {
	vector<int> pre = {3, 9, 20, 15, 7};
	vector<int> in = {9, 3, 15, 20, 7};
	vector<int> post = {9, 15, 7, 20, 3};
	for(int i = 0; i < in.size(); i++) {
		mp[in[i]] = i;  //记录中序遍历中元素的位置
	}
	int size = in.size();
	BitTree *T1 = create1(pre, 0, size - 1, 0, size - 1);
	//BitTree *T2 = create2(post, 0, size - 1, 0, size - 1);
	postorder(T1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值