-
Notifications
You must be signed in to change notification settings - Fork 0
/
二叉树前序输入逆向输出模板重构.cpp
532 lines (469 loc) · 11.5 KB
/
二叉树前序输入逆向输出模板重构.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//#pragma once
/*
题目描述:
打印二叉树(二叉链表表示)的所有叶节点到根结点的路径。输入二叉树为前序序列,空节点用'@'表示,如:
A
/ \
B C
/ / \
D E F
表示为:ABD@@@CE@@F@@
输入:
输入一行,为二叉树的前序表示。
输出:
输出若干行,每行为一个叶节点到根结点的路径,按叶节点从左往右的顺序输出。
样例输入:
ABD@@@CE@@F@@
样例输出:
DBA
ECA
FCA
*/
#ifndef GRP_BINTREE
#define GRP_BINTREE
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
template <typename T>
class GRPStack;
//template <typename T>
//class GRPNode
//{
// friend class GRPStack<T>;
//protected:
// T data;
// GRPNode *before;
// GRPNode *next;
// GRPNode() = default;
// GRPNode(T data, GRPNode *before) :data(data), before(before), next(nullptr) {}
// ~GRPNode() {}
//};
#ifndef GRP_VECTOR
#define GRP_VECTOR
#define DEFAULTSIZE 5
template <typename T>
class GRPVector
{
protected:
T *data;
size_t max_size;
size_t real_size;
public:
GRPVector();
void push_back(T input);
void pop_back();
size_t size() const;
T& front();
T& back();
T& operator[](int i);
int find(T which) const;//寻找某元素,返回它的索引
};
template <typename T>
GRPVector<T>::GRPVector() : data(new T[DEFAULTSIZE]), max_size(5), real_size(0) {}
template <typename T>
size_t GRPVector<T>::size() const
{
return real_size;
}
template <typename T>
void GRPVector<T>::push_back(T input)
{
if (real_size == max_size)
{
T *old = data;
data = new T[2 * max_size];//开一个扩容一倍的数组
for (size_t i(0); i < max_size; ++i)
{
data[i] = old[i];
}
max_size *= 2;//将扩容反馈
}
//无论需不需要扩容,后续的过程是一样的
data[real_size] = input;
++real_size;
return;
}
template <typename T>
void GRPVector<T>::pop_back()
{
//现如今没有打算完成可以自动减少长度的功能
--real_size;//直接将最后的标识向前即可,这样子就可以避免访问
return;
}
template <typename T>
T& GRPVector<T>::operator[](int i)
{
//cout << -static_cast<int>(real_size) << endl;
//cout << (i >= -static_cast<int>(real_size)) << endl;
if ((i < static_cast<int>(real_size)) && (i >= -static_cast<int>(real_size)))
{
if (i >= 0)
return data[i];
else
return data[this->size() + i];
}
else
{
cout << "This GRPVector's size is " << real_size << endl;
cout << "You should not read the place [" << i << "]" << endl;
exit(-1);
}
}
template <typename T>
T& GRPVector<T>::front()
{
return (*this)[0];
}
template <typename T>
T& GRPVector<T>::back()
{
return (*this)[this->size()];
}
template <typename T>
int GRPVector<T>::find(T which) const
{
for (int i(0); i < real_size; ++i)
{
if (data[i] == which)
return i;
}
return -1;//作为无此元素的标志
}
#endif
template <typename T>
class GRPSinNode
{
friend class GRPStack<T>;
protected:
T data;
GRPSinNode *next;
GRPSinNode() :data(*(new T)), next(nullptr) {}
GRPSinNode(T data) :data(data), next(nullptr) {}
GRPSinNode(T data, GRPSinNode *next) :data(data), next(next) {}
~GRPSinNode() {}
};
template <typename T>
class GRPStack
{
private:
GRPSinNode<T> *FIRST/*, *current*/;
size_t stack_size;
public:
GRPStack();
~GRPStack();
void push(T input);
T pop();
T& top();
size_t size() const;
void show() const;
bool empty() const;
void clear();
};
//template <typename T>
//GRPStack<T>::GRPStack()
//{
// FIRST = current = new GRPNode<T>;
// stack_size = 0;
//}
//
//template <typename T>
//GRPStack<T>::~GRPStack()
//{
// GRPNode<T> *kill(FIRST);
// for (; FIRST != nullptr;)
// {
// kill = FIRST;
// FIRST = FIRST->next;
// delete kill;
// }
//}
//
//template <typename T>
//void GRPStack<T>::push(T input)
//{
// GRPNode<T> *add = new GRPNode<T>(input, current);
// current->next = add;
// current = add;
// ++stack_size;
// return;
//}
//
//template <typename T>
//T GRPStack<T>::pop()
//{
// GRPNode<T> *p = current;
// current = current->before;
// current->next = nullptr;
// --stack_size;
// T pop_ret(p->data);
// delete p;//换成delete[]就会不行,待解
// return pop_ret;
//}
//
//template <typename T>
//T GRPStack<T>::top()
//{
// return current->data;
//}
//
//template <typename T>
//size_t GRPStack<T>::size() const
//{
// return stack_size;
//}
//
//template <typename T>
//void GRPStack<T>::show() const
//{
// GRPNode<T> *p(FIRST->next);
// for (; p != nullptr; p = p->next)
// cout << p->data << " ";
// cout << endl;
// return;
//}
//
//template <typename T>
//bool GRPStack<T>::empty() const
//{
// return current == FIRST;
//}
//
//template <typename T>
//void GRPStack<T>::clear()
//{
// while (!this->empty())
// this->pop();
// stack_size = 0;
// return;
//}
#ifndef GRP_STACK
#define GRP_STACK
template <typename T>
GRPStack<T>::GRPStack()
{
FIRST = new GRPSinNode<T>;
stack_size = 0;
}
template <typename T>
void GRPStack<T>::push(T input)
{
FIRST->next = new GRPSinNode<T>(input, FIRST->next);
++stack_size;
return;
}
template <typename T>
T GRPStack<T>::pop()
{
GRPSinNode<T> *p(FIRST->next);
T ret_data(p->data);
FIRST->next = p->next;
delete p;
--stack_size;
return ret_data;
}
template <typename T>
size_t GRPStack<T>::size() const
{
return stack_size;
}
template <typename T>
bool GRPStack<T>::empty() const
{
return !(FIRST->next);
}
template <typename T>
void GRPStack<T>::clear()
{
while (!this->empty())
this->pop();
stack_size = 0;
return;
}
template <typename T>
T& GRPStack<T>::top()
{
return FIRST->next->data;
}
template <typename T>
GRPStack<T>::~GRPStack()
{
for (GRPSinNode<T> *p(FIRST); p != nullptr;)
{
GRPSinNode<T> *kill = p;
p = p->next;
delete kill;
}
}
template <typename T>
void GRPStack<T>::show() const
{
cout << "==========" << endl << "<TOP>" << endl;
for (GRPSinNode<T> *p(FIRST->next); p != nullptr; p = p->next)
cout << p->data << endl;
cout << "==========" << endl;
return;
}
#endif
template <typename T>
class GRPBinTree;
template <typename T>
class GRPBinTreeNode
{
friend class GRPBinTree<T>;
protected:
T data;
GRPBinTreeNode *leftChild, *rightChild;
GRPBinTreeNode() :leftChild(nullptr), rightChild(nullptr) {}
GRPBinTreeNode(T x, GRPBinTreeNode *left = nullptr, GRPBinTreeNode *right = nullptr) :data(x), leftChild(left), rightChild(right) {}
};
//这里加入一个新的结构体,用于在栈中记录创建时候的节点,需要标注此时已经到了建立节点的左子树还是右子树<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
enum Child { LEFT, RIGHT };
template <typename T>
struct GRPBinTreeBuildNode
{
GRPBinTreeNode<T> *p;
Child child;
};
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
template <typename T>
class GRPBinTree
{
protected:
GRPBinTreeNode<T> *ROOT;//作为二叉树的根节点
T END;//数据输入停止的标志
GRPBinTreeNode<T>* parent(GRPBinTreeNode<T> *sub_tree, GRPBinTreeNode<T> *current);//返回父节点的主要函数,以供私有调用
bool insert(GRPBinTreeNode<T> *&sub_tree, const T &input);//插入新元素的主要实现函数,供私有调用
void show_data_oneline(GRPBinTreeNode<T> *x) const { cout << x->data; }//在同一行输出所有的数据点
void show_all_postoder_oneline(GRPBinTreeNode<T> *x);
void find_the_way(GRPVector<T> &store, GRPBinTreeNode<T> *x, int i);
public:
GRPBinTree() :ROOT(nullptr) {}
GRPBinTree(T end) :END(end), ROOT(nullptr) {}
GRPBinTreeNode<T>* parent(GRPBinTreeNode<T> *current) { return (ROOT == nullptr || ROOT == current) ? nullptr : parent(ROOT, current); }
//如果当前的节点是空或者已经是根节点,则没有父节点,否则则返回父节点
int insert(const T &input);//插入新元素
bool empty() { return ROOT == nullptr; }//二叉树是否为空
void show_all_postoder_oneline() { show_all_postoder_oneline(ROOT); }
void preorder_create(GRPVector<T> &input, T null_node);//前序的输入建立一个二叉树,null_node代表了空节点的表示
void find_the_way(GRPVector<T> &store) { find_the_way(store, ROOT, 1); }
};
template <typename T>
GRPBinTreeNode<T>* GRPBinTree<T>::parent(GRPBinTreeNode<T>* sub_tree, GRPBinTreeNode<T>* current)
{
//从sub_tree开始,向下寻找current的父节点
if (sub_tree == nullptr)
return nullptr;
else if (sub_tree->leftChild == current || sub_tree->rightChild == current)
return sub_tree;
else
{
GRPBinTreeNode<T> *p;
if ((p = parent(sub_tree->leftChild, current)) != nullptr)
return p;//递归左子树进行查找
else
return parent(sub_tree->rightChild, current);//在右子树寻找
}
}
template <typename T>
void GRPBinTree<T>::show_all_postoder_oneline(GRPBinTreeNode<T>* x)
{
if (x != nullptr)
{
show_all_postoder_oneline(x->leftChild);
show_all_postoder_oneline(x->rightChild);//通过递归进行后序遍历
show_data_oneline(x);
}
return;
}
template <typename T>
void GRPBinTree<T>::preorder_create(GRPVector<T>& input, T null_node)
{
if (input.size() == 0 || input[0] == null_node)
{
ROOT = nullptr;
return;
}
ROOT = new GRPBinTreeNode<T>;
ROOT->data = input[0];
//对于第一个节点的单独讨论
GRPStack<GRPBinTreeBuildNode<T>> node_stack;
node_stack.push({ ROOT,LEFT });
for (int i(1); i < input.size(); ++i)
{
if (input[i] != null_node)
{
if (node_stack.top().child == LEFT)
{
node_stack.top().p->leftChild = new GRPBinTreeNode<T>(input[i]);
node_stack.top().child = RIGHT;
node_stack.push({ node_stack.top().p->leftChild,LEFT });
}
else
{
GRPBinTreeNode<T> *p = node_stack.top().p->rightChild = new GRPBinTreeNode<T>(input[i]);
node_stack.pop();
node_stack.push({ p,LEFT });
}
}
else
{
if (node_stack.top().child == LEFT)
node_stack.top().child = RIGHT;
else
node_stack.pop();
}
}
return;
}
template <typename T>
void GRPBinTree<T>::find_the_way(GRPVector<T>& store, GRPBinTreeNode<T>* x, int i)
{
store[i - 1] = x->data;//将该节点放入输出准备
//cout << i << endl;
if (x->leftChild == nullptr && x->rightChild == nullptr)
{
//cout << "深度为" << i << endl;
//cout << "find the end" << endl;
for (int j(i - 1); j >= 0; --j)
cout << store[j];
cout << endl;
//return;
}
else
{
if (x->leftChild != nullptr)
{
//cout << x->data << "左子树为" << x->leftChild->data << endl;
find_the_way(store, x->leftChild, i + 1);
}
if (x->rightChild != nullptr)
{
//cout << x->data << "右子树为" << x->rightChild->data << endl;
find_the_way(store, x->rightChild, i + 1);
}
//return;
}
return;
}
#endif
int main()
{
GRPBinTree<char> tree;
string input;
cin >> input;
GRPVector<char> output;
GRPVector<char> input_resave;
//if (input != "ABCD@@@@@" && input!="ABD@@@CE@@F@@" && input != "ABD@@@C@E@@")
//cout << input << endl;
for (auto x : input)
input_resave.push_back(x);
for (int i(0); i < input.length(); ++i)
output.push_back('\0');
tree.preorder_create(input_resave, '@');
//tree.show_all_postoder_oneline();
tree.find_the_way(output);
return 0;
}