c++ 二叉樹的遍歷(迭代,遞歸)

#include<iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include<stack>
#include<ctime>
#include <sstream>
#include <queue>
using namespace std;
// 樹節點的結構體
struct TreeNode 
{
    int val;
    TreeNode *left;
	TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 函數聲明
void preorder (TreeNode * p);
void midorder (TreeNode *p);
void postorder(TreeNode *p);
void preorder_1 (TreeNode *p);
void preorder_2 (TreeNode *p);
void midorder_1(TreeNode* p);
void postorder_1(TreeNode* p);
void levelorder_1(TreeNode*p);

int main()
{
	TreeNode* a=new TreeNode (2);
	TreeNode* b= new TreeNode(3);
	TreeNode* c= new TreeNode(1);
	a->left=b;a->right=c;
	TreeNode* d= new TreeNode(6);
	TreeNode* e= new TreeNode(4);
	b->left=new TreeNode(5);b->right=d;
	d->left=new TreeNode(3);d->right=new TreeNode(0);
	c->right=e;
	e->left=new TreeNode (-1);e->right=new TreeNode(2);

	cout << "遞歸版: " <<endl; 
	preorder(a);
	cout << endl;
	midorder(a);
	cout << endl;
	postorder(a);
	cout << endl;
	cout <<"迭代版: " <<endl;
	cout << "先序遍歷1.0:";
	preorder_1(a);
	cout <<endl;
	cout << "先序遍歷2.0:";
	preorder_2(a);
	cout <<endl;
	cout << "中序遍歷:";
	midorder_1(a);
	cout << endl;
	cout <<"層次遍歷:" ;
	levelorder_1(a);
	cout << endl;
	return 0;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%  遞歸版  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// 樹的前序遍歷
void preorder (TreeNode * p)
{
	if(p == NULL) return;

	cout<< p->val << " ";
	preorder(p->left);
	preorder(p->right);
}
//樹的中序遍歷
void midorder (TreeNode *p)
{
	if(p == NULL) return;

	midorder(p->left);
	cout<< p->val << " ";
	midorder(p->right);
}
// 樹的後序遍歷
void postorder(TreeNode *p)
{
	if(p == NULL) return;

	postorder(p->left);
	postorder(p->right);
	cout<< p->val << " ";
}
// %%%%%%%%%%%%%%%%%%%%%%%% 迭代版 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// 前序遍歷1.0
void preorder_1 (TreeNode *p)
{
	stack<TreeNode *> s;
	s.push(p);
	while(!s.empty())
	{
		TreeNode* tmp=s.top();
		s.pop();
		cout << tmp->val << " ";
		if(tmp->right != NULL) s.push(tmp->right);
		if(tmp->left != NULL) s.push(tmp->left);
	}
}
// 前序遍歷2.0
void visitalongleft(TreeNode* p,stack<TreeNode*> &s)
{	
	while(p!=NULL)
	{
		s.push(p);
		cout << p->val << " ";
		p=p->left;
	}
}
void preorder_2 (TreeNode *p)
{
	stack<TreeNode*> s;
	while(true)
	{
		visitalongleft(p,s);
		if(s.empty()==true) break;
		TreeNode* tmp=s.top();
		s.pop();
		p=tmp->right;
	}
}
// 中序遍歷迭代版
void leftalong(TreeNode* p,stack<TreeNode*> &s)
{
	while(p!=NULL) {s.push(p);p=p->left;}
}

void midorder_1(TreeNode* p)
{
	stack<TreeNode*> s;
	while(true)
	{
		leftalong(p,s);
		if(s.empty()==true) break;
		cout << s.top()->val << " ";
		p=s.top()->right;
		s.pop();
	}
}

void levelorder_1(TreeNode*p)
{
	queue<TreeNode *> q;
	if(p==NULL) return;
	q.push(p);
	while(!q.empty())
	{
		cout << q.front()->val <<" ";
		if(q.front()->left != NULL) q.push(q.front()->left);
		if(q.front()->right != NULL) q.push(q.front()->right);
		q.pop();
	}
}
相關文章
相關標籤/搜索