# 二叉树的遍历

1. 广度优先遍历

void width_first_search(Tree* root)
{
queue<Tree*> q;
Tree* node;
q.push(root);
while (!q.empty()) {
node = q.front();
cout << node->value << " ";
q.pop();
if(node->left != NULL)
q.push(node->left);
if(node->right != NULL)
q.push(node->right);
}
}

2.深度优先遍历

void depth_first_search(Tree *root)
{
stack<Tree *> s;
s.push(root);
while (!s.empty())
{
root = s.top();
cout << root->value << " ";
s.pop();
if (root->right != NULL)
s.push(root->right);
if (root->left != NULL)
s.push(root->left);
}
}

3. 其他遍历和实现方式

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

struct Tree
{
int value;
Tree* left;
Tree* right;
Tree() {}
Tree(int a, Tree* pl, Tree* pr) : value(a), left(pl), right(pr) {}
};

Tree* create_tree()
{
// 10, 7  21,  38  57  13,  62
Tree* t_3_0 = new Tree(62, NULL, NULL);
Tree* t_2_1 = new Tree(57, NULL, NULL);
Tree* t_2_2 = new Tree(13, NULL, NULL);
Tree* t_2_0 = new Tree(38, t_3_0, NULL);
Tree* t_1_0 = new Tree(7, t_2_0, t_2_1);
Tree* t_1_1 = new Tree(21, t_2_2, NULL);
Tree* root = new Tree(10, t_1_0, t_1_1);
return root;
}

void pre_order_search(Tree* root)
{
stack<Tree*> s;
Tree* node = root;
while (node != NULL || !s.empty()) {
while (node != NULL) {
cout << node->value << " ";
s.push(node);
node = node->left;
}
if (!s.empty()) {
node = s.top();
s.pop();
node = node->right;
}
}
}

void in_order_search(Tree* root)
{
stack<Tree*> s;
Tree* node = root;
while (node != NULL || !s.empty()) {
while (node != NULL) {
s.push(node);
node = node->left;
}
if (!s.empty()) {
node = s.top();
cout << node->value << " ";
s.pop();
node = node->right;
}
}
}
void pre_order_search_recursion(Tree* root)
{
if (root != NULL)
{
cout << root->value << " ";
pre_order_search_recursion(root->left);
pre_order_search_recursion(root->right);
}
}

void in_order_search_recursion(Tree* root)
{
if (root != NULL)
{
in_order_search_recursion(root->left);
cout << root->value << " ";
in_order_search_recursion(root->right);
}
}

void post_order_search_recursion(Tree* root)
{
if (root != NULL)
{
in_order_search_recursion(root->left);
in_order_search_recursion(root->right);
cout << root->value << " ";
}
}

void width_first_search(Tree* root)
{
queue<Tree*> q;
Tree* node;
q.push(root);
while (!q.empty()) {
node = q.front();
cout << node->value << " ";
q.pop();
if(node->left != NULL)
q.push(node->left);
if(node->right != NULL)
q.push(node->right);
}
}

void depth_first_search(Tree *root)
{
stack<Tree *> s;
s.push(root);
while (!s.empty())
{
root = s.top();
cout << root->value << " ";
s.pop();
if (root->right != NULL)
s.push(root->right);
if (root->left != NULL)
s.push(root->left);
}
}

int main()
{
// create tree
Tree* root;
root = create_tree();
// pre-order
cout << "Result of pre-order search: ";
pre_order_search(root);
cout << endl;
cout << "Result of pre-order search (recursion): ";
pre_order_search_recursion(root);
cout << endl;
// in-order
cout << "Result of in-order search: ";
in_order_search(root);
cout << endl;
cout << "Result of in-order search (recursion): ";
in_order_search_recursion(root);
cout << endl;
// post-order search
cout << "Result of post-order search (recursion): ";
post_order_search_recursion(root);
cout << endl;
// width first search
cout << "Width first search: ";
width_first_search(root);
cout << endl;
depth_first_search(root);
cout << endl;
system("pause");
return 0;
}

#### 数据结构二叉树的遍历

2009年11月21日 2KB 下载

#### PTA的6-10 二叉树的遍历

2017-11-17 16:12:49

#### 二叉树的遍历及其应用

2016-06-06 19:51:27

#### 二叉树的三种遍历算法 源码

2012-06-10 15:04:28

#### 二叉树的遍历有几种方式？

2017-04-21 22:28:11

#### 二叉树的多种遍历方法加例题

2017-03-16 15:59:20

#### 4-9 二叉树的遍历 (25分)(前、中、后、层次)

2016-11-02 22:30:02

#### 二叉树的遍历.ppt

2011年02月25日 635KB 下载

#### 二叉树的遍历【 详细讲解 】

2016-10-18 17:04:53

#### 数据结构实验:二叉树的遍历(C语言版)

2017-10-18 23:32:02