二叉树的遍历——递归与非递归

A
B C
D E

#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct treenode {//二叉树的结构体
	int val;
	struct treenode* left;
	struct treenode* right;
}Tree;
typedef struct node {//后续遍历添加指针
	Tree* link;
	bool first;
	node() :link(NULL), first(true) {};
}Node;

Tree* create(int x) {//创建二叉树
	Tree* t;
	t = (Tree*)malloc(sizeof(Tree));
	t->val = x;
	t->right = t->left = nullptr;
	return t;
}
void preorder(Tree* a) {//递归前序遍历
	if (a == nullptr)
		return;
	cout<<a->val<<" ";
	preorder(a->left);
	preorder(a->right);
}
void preorder1(Tree* t) {//非递归前序遍历
	stack<Tree*>s;
	if (t == NULL)
		return;
	
	Tree* p = t;
	while(p!=NULL||!s.empty()) {
		while (p!=NULL) {
			s.push(p);
			cout << p->val<<"  ";
			p = p->left;
		}
		while (!s.empty()) {
			p = s.top();
			s.pop();
			p = p->right;

		}

	}
}
void inorder(Tree* t) {//递归中序遍历
	if (t == nullptr) {
		return;
	}
	inorder(t->left);
	cout << t->val<<" ";
	inorder(t->right);
}
void postorder(Tree* t) {//递归后续遍历
	if (t == nullptr)
		return;
	postorder(t->left);
	postorder(t->right);
	cout << t->val << " ";
}
void leveoorder(Tree* t) {//层次遍历
	queue<Tree*>Q;
	Tree* p = t;
	Q.push(p);
	while (!Q.empty()) {
		p = Q.front();
		cout << Q.front()->val<<"  ";
		Q.pop();
		if (p->left) {
			Q.push(p->left);
		}
		if (p->right)
			Q.push(p->right);
	}


}
void inorder1(Tree* t) {//非递归中序遍历
	stack<Tree*>s;
	Tree* p=t;
	while (p != NULL || !s.empty()) {
		
		while (p!=NULL) {
			s.push(p);
			p = p->left;
		}
		if (!s.empty()) {
			p = s.top();
			s.pop();
			cout << p->val<<" ";
			p = p->right;

		}
	}
}
void postorder1(Tree* root) {//非递归后续遍历
	stack<Node*>s;
	Node* p;
	while (root || !s.empty()) {
		if (root) {
			p = new Node();
			p->link = root;
			s.push(p);
			root = root->left;
		}
		else {
			p = s.top();
			s.pop();
			if (p->first == true) {
				s.push(p);
				p->first = false;
				root = p->link->right;
			}
			else {
				cout << p->link->val << " ";
			}
		}
	
	}

}
int sizeleafTrree(Tree* t) {//返回节点个数
	if (t == nullptr)
		return 0;
	return 1 + sizeleafTrree(t->left) + sizeleafTrree(t->right);
}
int Treesize(Tree* t) {//返回叶子节点个数
	if (t == nullptr)
		return 0;
	if (t->left == nullptr && t->right == nullptr)
		return 1;
	return Treesize(t->left) + Treesize(t->right);
}

int main() {
	Tree* A= create(1);
	Tree* B = create(2);
	Tree* C = create(3);
	Tree* D = create(4);
	Tree* E= create(5);
	//Tree* A = create(1);
	A->left = B;
	A->right = C;
	B->left = D;
	B->right = E;
	preorder(A);
	cout << endl;
	inorder(A);
	cout << endl;
	postorder(A);
	cout << endl;
	leveoorder(A);
	cout << endl;
	inorder1(A);
	cout << endl;
	postorder1(A);
	/*printf("\n");
	cout << sizeleafTrree(A) << endl;
	cout << Treesize(A) << endl;
	preorder1(A);*/
	return 0;
	 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

向前走()

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

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

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

打赏作者

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

抵扣说明:

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

余额充值