二叉树的遍历
二叉树的遍历
二叉树遍历
递归算法
- BinaryTree.h
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
//
// Created by Administrator on 2021/10/25.
//
#ifndef TEST_BINARYTREE_H
#define TEST_BINARYTREE_H
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct BiNode {
ElemType data;
BiNode *left;
BiNode *right;
BiNode(ElemType val) {
data = val;
left = nullptr;
right = nullptr;
}
} BiNode, *BiTree;
class BinaryTree {
public:
void Create();
int getSize();
int getHeight();
void preOrder();
void inOrder();
void postOrder();
void destroy();
private:
BiTree create();
void preOrder(BiTree root);
void inOrder(BiTree root);
void postOrder(BiTree root);
void destroy(BiTree root);
int getHeight(BiTree root);
void AddNode(ElemType key, int direction, BiTree root);
BiTree m_root;
int size;
};
#endif //TEST_BINARYTREE_H
- BinaryTree.cpp
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
//
// Created by Administrator on 2021/10/25.
//
#include "BinaryTree.h"
void BinaryTree::Create() {
size = 0;
m_root = create();
}
int BinaryTree::getSize() {
return size;
}
int BinaryTree::getHeight() {
return getHeight(m_root);
}
void BinaryTree::preOrder() {
cout << "先序遍历:" << endl;
preOrder(m_root);
cout << endl;
}
void BinaryTree::inOrder() {
cout << "中序遍历:" << endl;
inOrder(m_root);
cout << endl;
}
void BinaryTree::postOrder() {
cout << "后序遍历:" << endl;
postOrder(m_root);
cout << endl;
}
void BinaryTree::destroy() {
destroy(m_root);
}
BiTree BinaryTree::create() {
BiTree current = nullptr;
ElemType val;
// 输入值
cin >> val;
if (val == '#') {
// 当前子树为空
return nullptr;
} else {
// 递归创建左右子树
size++;
current = new BiNode(val);
current->left = create();
current->right = create();
return current;
}
}
void BinaryTree::preOrder(BiTree root) {
if (root == nullptr)
return;
else {
cout << root->data << " --> "; // 先打印根节点
preOrder(root->left); // 遍历左子树
preOrder(root->right); // 遍历右子树
}
}
void BinaryTree::inOrder(BiTree root) {
if (root == nullptr)
return;
else {
inOrder(root->left);
cout << root->data << " --> ";
inOrder(root->right);
}
}
void BinaryTree::postOrder(BiTree root) {
if (root == nullptr)
return;
else {
postOrder(root->left);
postOrder(root->right);
cout << root->data << " --> ";
}
}
void BinaryTree::destroy(BiTree root) {
if (root) {
destroy(root->left);
destroy(root->right);
// 释放节点
delete root;
root = nullptr;
size = 0;
}
}
/**
*
* @param root
* @return 递归得到树高
*/
int BinaryTree::getHeight(BiTree root) {
if (root == nullptr)
return 0;
int left_height = getHeight(root->left);
int right_height = getHeight(root->right);
return (left_height > right_height) ? (left_height + 1) : (right_height + 1);
}
/**
* 前序遍历的方式创建
* @param key 值
* @param direction 0是从左子树插入,1是从右子树插入
* @param root 被插入的根节点
*/
void BinaryTree::AddNode(const ElemType key, int direction, BiTree root) {
if (direction == 0) {
// 插入到左子树
if (root->left == nullptr)
root->left = new BiNode(key);
else
AddNode(key, direction, root->left);
} else if (direction == 1) {
// 插入到右子树
if (root->right == nullptr)
root->right = new BiNode(key);
else
AddNode(key, direction, root->right);
}
}
- main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "BinaryTree.h"
using namespace std;
int main() {
BinaryTree tree{};
cout << "按先序遍历方式创建树:" << endl;
// ABCD##E##FG###HI#J##K##
tree.Create();
cout << "树的高度为:" << tree.getHeight() << endl;
cout << "树的节点为:" << tree.getSize() << endl;
tree.preOrder();
tree.inOrder();
tree.postOrder();
tree.destroy();
return 0;
}
- 截图
本文由作者按照 CC BY 4.0 进行授权