-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
186 lines (154 loc) · 4.26 KB
/
binary_tree.cpp
File metadata and controls
186 lines (154 loc) · 4.26 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "binary_tree.h"
#include <queue>
#include <stdexcept>
BinaryTree::BinaryTree() : root(nullptr) {}
BinaryTree::BinaryTree(const std::vector<std::optional<int>>& level)
: root(nullptr) {
if (level.empty() || !level[0].has_value()) {
return;
}
root = new Node(*level[0]);
std::queue<Node*> q;
q.push(root);
std::size_t i = 1;
while (!q.empty() && i < level.size()) {
Node* cur = q.front();
q.pop();
if (i < level.size() && level[i].has_value()) {
cur->left = new Node(*level[i]);
q.push(cur->left);
}
++i;
if (i < level.size() && level[i].has_value()) {
cur->right = new Node(*level[i]);
q.push(cur->right);
}
++i;
}
}
BinaryTree::BinaryTree(const std::vector<int>& preorder,
const std::vector<int>& inorder)
: root(nullptr) {
if (preorder.size() != inorder.size()) {
throw std::invalid_argument("preorder and inorder size mismatch");
}
if (!preorder.empty()) {
root = build(preorder,
0,
static_cast<int>(preorder.size()) - 1,
inorder,
0,
static_cast<int>(inorder.size()) - 1);
}
}
BinaryTree::~BinaryTree() {
clear();
}
BinaryTree::Node* BinaryTree::build(const std::vector<int>& preorder,
int preL,
int preR,
const std::vector<int>& inorder,
int inL,
int inR) {
if (preL > preR) {
return nullptr;
}
int rootValue = preorder[preL];
int rootPos = inL;
while (rootPos <= inR && inorder[rootPos] != rootValue) {
++rootPos;
}
if (rootPos > inR) {
throw std::invalid_argument("invalid preorder/inorder sequence");
}
// 前序第一个是根;中序里根左边的长度就是左子树大小。
int leftLen = rootPos - inL;
Node* node = new Node(rootValue);
node->left = build(preorder,
preL + 1,
preL + leftLen,
inorder,
inL,
rootPos - 1);
node->right = build(preorder,
preL + leftLen + 1,
preR,
inorder,
rootPos + 1,
inR);
return node;
}
void BinaryTree::free_tree(Node* node) {
if (node == nullptr) {
return;
}
free_tree(node->left);
free_tree(node->right);
delete node;
}
void BinaryTree::collect_pre(Node* node, std::vector<int>& out) {
if (node == nullptr) {
return;
}
out.push_back(node->value);
collect_pre(node->left, out);
collect_pre(node->right, out);
}
void BinaryTree::collect_in(Node* node, std::vector<int>& out) {
if (node == nullptr) {
return;
}
collect_in(node->left, out);
out.push_back(node->value);
collect_in(node->right, out);
}
void BinaryTree::collect_post(Node* node, std::vector<int>& out) {
if (node == nullptr) {
return;
}
collect_post(node->left, out);
collect_post(node->right, out);
out.push_back(node->value);
}
bool BinaryTree::empty() const {
return root == nullptr;
}
void BinaryTree::clear() {
free_tree(root);
root = nullptr;
}
std::vector<int> BinaryTree::preorder() const {
std::vector<int> out;
collect_pre(root, out);
return out;
}
std::vector<int> BinaryTree::inorder() const {
std::vector<int> out;
collect_in(root, out);
return out;
}
std::vector<int> BinaryTree::postorder() const {
std::vector<int> out;
collect_post(root, out);
return out;
}
std::vector<int> BinaryTree::level_order() const {
std::vector<int> out;
if (root == nullptr) {
return out;
}
std::queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* cur = q.front();
q.pop();
out.push_back(cur->value);
if (cur->left != nullptr) {
q.push(cur->left);
}
if (cur->right != nullptr) {
q.push(cur->right);
}
}
return out;
}