【数据结构】二叉树的遍历
来源:互联网 发布:安卓软件 知乎 编辑:程序博客网 时间:2024/05/21 06:52
二叉树是每个结点最多有两个子树的有序树。通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用作二叉查找树和二叉堆或是二叉排序树。二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。
二叉树的遍历包括深度优先和宽度优先,深度优先又有前序,中序遍历和后序遍历三种。
对于深度优先遍历,递归遍历方法直观而简洁,如果要使用非递归方法,一般要借用栈结构;
宽度优先则常使用队列来实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include
using namespace std;
template
class
TreeNode
{
protected
:
TreeNode* left;
TreeNode* right;
public
:
T data;
TreeNode (
const
T& item, TreeNode *lptr,TreeNode *rptr):
data(item), left(lptr), right(rptr) {}
virtual ~TreeNode() {}
TreeNode* Left()
const
{
return
left;
}
TreeNode* Right()
const
{
return
right;
}
//递归遍历
void
Preorder(TreeNode *
const
node )
const
;
void
Inorder(TreeNode *
const
node )
const
;
void
Postorder(TreeNode *
const
node)
const
;
void
Preorder() {
Preorder(
this
);
}
void
Inorder() {
Preorder(
this
);
}
void
Postorder() {
Preorder(
this
);
}
//非递归遍历
void
PreTraverse(TreeNode *
const
node )
const
;
void
InTraverse(TreeNode *
const
node )
const
;
void
PostTraverse(TreeNode *
const
node)
const
;
void
PreTraverse() {
PreTraverse(
this
);
}
void
InTraverse() {
InTraverse(
this
);
}
void
PostTraverse() {
PostTraverse(
this
);
}
};
// preorder recursive scan of the nodes in a tree
template
void
TreeNode::Preorder (TreeNode *
const
t)
const
{
if
(t != NULL) {
cout << t->data <<
" "
;
// visit the node
Preorder (t->Left());
// descend left
Preorder (t->Right());
// descend right
}
}
// inorder recursive scan of the nodes in a tree
template
void
TreeNode::Inorder (TreeNode*
const
t)
const
{
if
(t != NULL) {
Inorder (t->Left());
// descend left
cout << t->data <<
" "
;
// visit the node
Inorder (t->Right());
// descend right
}
}
// postorder recursive scan of the nodes in a tree
template
void
TreeNode::Postorder (TreeNode *
const
t)
const
{
if
(t != NULL) {
Postorder (t->Left());
// descend left
Postorder(t->Right());
// descend right
cout << t->data <<
" "
;
// visit the node
}
}
template
void
TreeNode::PreTraverse(TreeNode *
const
node )
const
{
if
(node == NULL)
return
;
TreeNode *t = node;
stack< TreeNode* > s;
s.push(t);
while
(! s.empty()) {
t = s.top();
s.pop();
cout << t->data <<
" "
;
if
(t->right != NULL)
s.push(t->right);
if
(t->left != NULL)
s.push(t->left);
}
}
template
void
TreeNode::InTraverse(TreeNode *
const
node )
const
{
if
(node == NULL)
return
;
TreeNode *t = node;
stack< TreeNode* > s;
do
{
while
(t != NULL) {
s.push(t);
t = t->left;
}
t = s.top();
s.pop();
cout << t->data <<
" "
;
t = t->right;
}
while
(!s.empty()|| t!=NULL);
}
template
void
TreeNode::PostTraverse(TreeNode *
const
node )
const
{
stack<TreeNode* > s;
stack sf;
TreeNode *t = node;
int
flag;
while
(!s.empty() || t!=NULL){
while
(t!=NULL){
s.push(t);
sf.push(
0
);
t = t->left;
}
t = s.top(),s.pop();
flag = sf.top(),sf.pop();
if
(flag ==
0
) {
s.push(t);
sf.push(
1
);
t = t->right;
}
else
{
cout << t->data <<
" "
;
t = NULL;
}
}
}一个简单的测试程序
1 int main()
2 {
3 TreeNode<int> n1(10,NULL,NULL);
4 TreeNode<int> n2(9,NULL,NULL);
5 TreeNode<int> n3(6,&n1,&n2);
6 TreeNode<int> n4(7,NULL,NULL);
7 TreeNode<int> n5(8,NULL,NULL);
8 TreeNode<int> n6(3,&n4,&n5);
9 TreeNode<int> n7(1,&n3,&n6);
10
11 n7.PreTraverse(); cout << endl;
12 n7.InTraverse(); cout << endl;
13 n7.PostTraverse();cout << endl;
14
15 system("pause");
16 return 0;
17 }
2 {
3 TreeNode<int> n1(10,NULL,NULL);
4 TreeNode<int> n2(9,NULL,NULL);
5 TreeNode<int> n3(6,&n1,&n2);
6 TreeNode<int> n4(7,NULL,NULL);
7 TreeNode<int> n5(8,NULL,NULL);
8 TreeNode<int> n6(3,&n4,&n5);
9 TreeNode<int> n7(1,&n3,&n6);
10
11 n7.PreTraverse(); cout << endl;
12 n7.InTraverse(); cout << endl;
13 n7.PostTraverse();cout << endl;
14
15 system("pause");
16 return 0;
17 }
0 0
- 数据结构 - 二叉树的遍历
- 数据结构-----二叉树的遍历
- 数据结构----二叉树的遍历
- 数据结构 二叉树的遍历
- 【数据结构】 二叉树的遍历
- 【数据结构】二叉树的遍历
- 数据结构 - 二叉树的遍历
- 数据结构二叉树的遍历
- 二叉树的遍历(数据结构)
- 数据结构--二叉树的遍历
- [数据结构]二叉树的遍历
- 【数据结构】二叉树的遍历
- 【数据结构】二叉树的遍历
- 数据结构----二叉树的遍历
- 数据结构-二叉树的遍历
- 数据结构-二叉树的遍历
- 二叉树遍历 - 数据结构
- 数据结构 二叉树遍历
- OpenCV图像处理车牌检测与定位应用
- JAVA多个文件压缩
- 安装内核文档
- Android App接入微信开放平台注意事项
- 常用排序算法
- 【数据结构】二叉树的遍历
- Telnet英文全称
- 第十周项目四(1)——大奖赛计分
- 解决Qt5屏幕翻转问题
- 猜数字游戏
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- 理解C++复杂声明
- c#试验九4678合集
- 第十周项目4-大奖赛计分4