-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinarytree.cpp
More file actions
270 lines (240 loc) · 6.84 KB
/
Copy pathbinarytree.cpp
File metadata and controls
270 lines (240 loc) · 6.84 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "binarytree.h"
// 暂停等待seconds秒
void waitForSeconds(qreal seconds)
{
QEventLoop loop;
QTimer::singleShot(seconds * 1000, &loop, &QEventLoop::quit);
loop.exec();
}
binaryTreeNode::~binaryTreeNode()
{
}
/**
* @brief binaryTree::binaryTree 传入对应的根节点指针构造二叉树
* @param _root 根节点指针
*/
binaryTree::binaryTree(binaryTreeNode* _root):
root(_root)
{
}
/**
* @brief binaryTree::countLeafNode 供外部调用,统计叶子结点数
* @return 二叉树的叶子结点数
*/
qint16 binaryTree::countLeafNode()
{
return countLeafNode(root);
}
/**
* @brief binaryTree::countLeafNode 函数内部递归的统计叶子结点数
* @param cur 当前遍历的结点
* @return 当前结点为根结点的二叉树的叶子结点数
*/
qint16 binaryTree::countLeafNode(binaryTreeNode* cur)
{
if(!cur)
return 0;
else if(!cur->getLeftChild() && !cur->getRightChild())
return 1;
return countLeafNode(cur->getLeftChild()) + countLeafNode(cur->getRightChild());
}
/**
* @brief binaryTree::preOrderTraversal
* @param withDelay 是否延迟动画
*/
void binaryTree::preOrderTraversal(bool withDelay)
{
QStack<binaryTreeNode*> s;
binaryTreeNode* p;
s.push(root); //根指针进栈
while(!s.empty()) {
while((p = s.top())) {
p = s.pop(); //根元素出栈
p->visit();
if(withDelay)
waitForSeconds(0.5);
s.push(p->getRightChild()); //右子树先进栈
s.push(p->getLeftChild()); //左子树再进栈
}
s.pop(); //空指针退栈
}
}
/**
* @brief binaryTree::inOrderTraversal
* @param withDelay 是否延迟动画
*/
void binaryTree::inOrderTraversal(bool withDelay)
{
QStack<binaryTreeNode*> s;
binaryTreeNode* p;
s.push(root); // 根指针进栈
while(!s.empty()) {
while((p = s.top())){
s.push(p->getLeftChild());
}
s.pop(); // 空指针
if(!s.empty()){
p = s.pop();
p->visit();
if(withDelay)
waitForSeconds(0.5);
s.push(p->getRightChild());
}
}
}
/**
* @brief binaryTree::postOrderTraversal
* @param withDelay 是否延迟动画
*/
void binaryTree::postOrderTraversal(bool withDelay)
{
QStack<binaryTreeNode*> s;
binaryTreeNode* p, * pre = nullptr;
s.push(root); // 根指针进栈
while(!s.empty()) {
while((p = s.top())){
s.push(p->getLeftChild());
}
s.pop(); // 空指针
if(!s.empty()){
p = s.top();
if(!p->getRightChild() || p->getRightChild() == pre){
s.pop();
p->visit();
if(withDelay)
waitForSeconds(0.5);
pre = p;
s.push(nullptr); // 压一个空指针,因为上面要弹出
}
else{
s.push(p->getRightChild());
}
}
}
}
/**
* @brief binaryTree::preOrderTraversal_Thr 线索化遍历前序二叉树
* @param withDelay 是否延迟动画
*/
void binaryTree::preOrderTraversal_Thr(bool withDelay)
{
binaryTreeNode* p = root;
while(p){
while(p->getLeftChildTag() == binaryTreeNode::LINK){
p->visit();
if(withDelay)
waitForSeconds(0.5);
p = p->getLeftChild();
}
p->visit();
if(withDelay)
waitForSeconds(0.5);
p = p->getRightChild();
}
}
/**
* @brief binaryTree::inOrderTraversal_Thr 线索化遍历中序二叉树
* @param withDelay 是否延迟动画
*/
void binaryTree::inOrderTraversal_Thr(bool withDelay)
{
binaryTreeNode* p = root;
while(p){
while(p->getLeftChildTag() == binaryTreeNode::LINK)
p = p->getLeftChild();
p->visit();
if(withDelay)
waitForSeconds(0.5);
while(p->getRightChildTag() == binaryTreeNode::THREAD && p->getRightChild()){
p = p->getRightChild();
p->visit();
if(withDelay)
waitForSeconds(0.5);
}
p = p->getRightChild();
}
}
/**
* @brief binaryTree::createThreadedTree 递归线索化
* @param mode 前/中/后续
* @param cur 当前遍历的结点
* @param withDelay 是否延迟动画
*/
void binaryTree::createThreadedTree(int mode, binaryTreeNode* cur, bool withDelay)
{
if(cur){
cur->visit();
if(withDelay)
waitForSeconds(0.5);
switch(mode){
case PREORDER_TRAVERSAL:
threading(cur);
if(cur->getLeftChildTag() == binaryTreeNode::LINK)
createThreadedTree(mode, cur->getLeftChild(), withDelay);
if(cur->getRightChildTag() == binaryTreeNode::LINK)
createThreadedTree(mode, cur->getRightChild(), withDelay);
break;
case INORDER_TRAVERSAL:
createThreadedTree(mode, cur->getLeftChild(), withDelay);
threading(cur);
createThreadedTree(mode, cur->getRightChild(), withDelay);
break;
case POSTORDER_TRAVERSAL:
createThreadedTree(mode, cur->getLeftChild(), withDelay);
createThreadedTree(mode, cur->getRightChild(), withDelay);
threading(cur);
break;
}
}
}
/**
* @brief binaryTree::createThreadedTree 与上一函数重载,供外部调用
* @param mode 前/中/后续
* @param withDelay 是否延迟动画
*/
void binaryTree::createThreadedTree(int mode, bool withDelay)
{
pre = nullptr;
if(root){
createThreadedTree(mode, root, withDelay);
if(pre && !pre->getRightChild())
pre->setRightChild(nullptr, binaryTreeNode::THREAD);
}
}
/**
* @brief binaryTree::clearThreadedTree
* @param cur 当前遍历到的结点
*/
void binaryTree::clearThreadedTree(binaryTreeNode* cur)
{
// 递归的清除线索
if(cur){
if(cur->getLeftChildTag() == binaryTreeNode::LINK)
clearThreadedTree(cur->getLeftChild());
else
cur->setLeftChild(nullptr, binaryTreeNode::LINK);
if(cur->getRightChildTag() == binaryTreeNode::LINK)
clearThreadedTree(cur->getRightChild());
else
cur->setRightChild(nullptr, binaryTreeNode::LINK);
}
}
/**
* @brief binaryTree::clearThreadedTree 供外部调用,清除线索
*/
void binaryTree::clearThreadedTree()
{
clearThreadedTree(root);
}
/**
* @brief binaryTree::threading 处理某结点的线索化
* @param cur 当前处理的结点
*/
inline void binaryTree::threading(binaryTreeNode *cur)
{
if(!cur->getLeftChild())
cur->setLeftChild(pre, binaryTreeNode::THREAD);
if(pre && !pre->getRightChild())
pre->setRightChild(cur, binaryTreeNode::THREAD);
pre = cur;
}