-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.h
More file actions
1356 lines (1344 loc) · 61.5 KB
/
Tree.h
File metadata and controls
1356 lines (1344 loc) · 61.5 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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by Telephone on 2019/12/29 0029.
//
/// @file Tree.h
#ifndef TREE_TREE_H
#define TREE_TREE_H
#include <LinkedList.h>
#include <cstring>
#include <utility>
#include <LQueue.h>
#include <LStack.h>
#include <cmath>
#include <functional>
#include "telephone_ds_define.h"
/// namespace of common tree
namespace Telephone_DS::treeBase::Tree
{
/**
* @brief common tree
* @tparam T type of data in tree-node
*/
template <typename T>
class Tree
{
private:
/**
* @brief tree-node of common tree
* @tparam U type of data in tree-node
* @warning this is a private class defined in class Tree
*/
template <typename U>
class TreeNode
{
public:
/// data of tree-node
U data;
private:
/// pointer to parent node
TreeNode<U> *parent = nullptr;
/**
* a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers to
* child nodes
*/
linkBase::LinkedList::LinkedList<TreeNode<U> *> children;
public:
/**
* @brief general constructor(copy)
* @param d data of the new node
* @note use copy-constructor of type U to copy the data to the new node
* @par init
* - parent = nullptr
* - data = specified data
* - child-nodes list = empty
*/
explicit TreeNode(U const &d)
: data(d)
{
}
/**
* @brief general constructor(move)
* @param d data of the new node
* @note use move-constructor of type U to move the data to the new node
* @par init
* - parent = nullptr
* - data = specified data
* - child-nodes list = empty
*/
explicit TreeNode(U &&d)
: data(std::move(d))
{
}
/**
* @brief copy-constructor
* @param src
* @note use copy-constructor of type U and child-nodes @link
* Telephone_DS::linkBase::LinkedList::LinkedList list @endlink to copy data and
* child-nodes list from the node passed in by parameter
* @par Result:
* current node will have the same data, parent pointer and child-nodes list as the
* node passed in by parameter
*/
TreeNode(TreeNode<U> const &src)
: data(src.data), parent(src.parent), children(src.children)
{
}
/**
* @brief move-constructor
* @param src
* @note use move-constructor of type U and child-nodes @link
* Telephone_DS::linkBase::LinkedList::LinkedList list @endlink to move data and
* child-nodes list from the node passed in by parameter
* @par Result:
* current node will have the same data, parent pointer and child-nodes list as the
* node passed in by parameter
*/
TreeNode(TreeNode<U> &&src) noexcept
: data(std::move(src.data)), parent(src.parent), children(std::move(src.children))
{
}
/**
* @brief copy-assignment
* @param right
* @return left reference of the node being assigned
* @note use copy-assignment of type U and child-nodes @link
* Telephone_DS::linkBase::LinkedList::LinkedList list @endlink to copy data and
* child-nodes list from the node passed in by parameter
* @par Result:
* current node will have the same data, parent pointer and child-nodes list as the
* node passed in by parameter
*/
TreeNode<U> &operator=(TreeNode<U> const &right)
{
if (this == &right)
return *this;
data = right.data;
parent = right.parent;
children = right.children;
return *this;
}
/**
* @brief move-assignment
* @param right
* @return left reference of the node being assigned
* @note use move-assignment of type U and child-nodes @link
* Telephone_DS::linkBase::LinkedList::LinkedList list @endlink to move data and
* child-nodes list from the node passed in by parameter
* @par Result:
* current node will have the same data, parent pointer and child-nodes list as the
* node passed in by parameter
*/
TreeNode<U> &operator=(TreeNode<U> &&right) noexcept
{
data = std::move(right.data);
parent = right.parent;
children = std::move(right.children);
return *this;
}
/**
* @brief destructor
* @note this function will only do two things:
* -# call the destructor of data
* -# call the destructor of child-nodes list
*/
virtual ~TreeNode() = default;
/**
* @brief get child nodes
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to child nodes
* @note changing the returned list won't actually effect the child-nodes list in
* current node , but the child nodes of current node can be effected through the
* pointers in the returned list
* @par Order:
* the returned list is tha same as the child-nodes list
*/
virtual auto getChildren() -> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
return children;
}
/**
* @brief get the first child node in the child-nodes list
* @return
* - if the child-nodes list is not empty , return a pointer to the first child node
* in the child-nodes list;
* - or else , return nullptr
*/
virtual auto getFirstChild() -> TreeNode<U> *
{
if (children.isEmpty())
{
return nullptr;
}
return children.at(0);
}
/**
* @brief get the last child node in the child-nodes list
* @return
* - if the child-nodes list is not empty , return a pointer to the last child node
* in the child-nodes list;
* - or else , return nullptr
*/
virtual auto getLastChild() -> TreeNode<U> *
{
if (children.isEmpty())
{
return nullptr;
}
return children.at(children.len() - 1);
}
/**
* @brief traverse current node's child-nodes list and then get child nodes containing
* the specified data
* @param value the specified data
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to child nodes containing the specified data
* @par How to compare:
* - if the type of node's data part is floating-point(float , double , long double)
* number , use floating-point error (1e-6);
* - or else , use operator ==
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenOfData(U const &value) -> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> res;
if (typeid(U) != typeid(double) && typeid(U) != typeid(float) && typeid(U) != typeid(long double))
{
for (int i = 0; i < children.len(); ++i)
{
TreeNode<U> *temp = children.at(i);
if (temp->data == value)
{
res.addAfter(res.len() - 1, temp);
}
}
}
else
{
for (int i = 0; i < children.len(); ++i)
{
TreeNode<U> *temp = children.at(i);
if (std::fabs(temp->data - value) <= 1e-6)
{
res.addAfter(res.len() - 1, temp);
}
}
}
return std::move(res);
}
/**
* @brief traverse current node's child-nodes list and then get indexes of child
* nodes containing the specified data
* @param value the specified data
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of indexes
* of child nodes containing the specified data
* @par How to compare:
* - if the type of node's data part is floating-point(float , double , long double)
* number , use floating-point error (1e-6);
* - or else , use operator ==
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenIndexOfData(U const &value) -> linkBase::LinkedList::LinkedList<int>
{
linkBase::LinkedList::LinkedList<int> res;
if (typeid(U) != typeid(double) && typeid(U) != typeid(float) && typeid(U) != typeid(long double))
{
for (int i = 0; i < children.len(); ++i)
{
if (children.at(i)->data == value)
{
res.addAfter(res.len() - 1, i);
}
}
}
else
{
for (int i = 0; i < children.len(); ++i)
{
if (std::fabs(children.at(i)->data - value) <= 1e-6)
{
res.addAfter(res.len() - 1, i);
}
}
}
return std::move(res);
}
/**
* @brief traverse current node's child-nodes list and then get child nodes
* containing the specified data
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which won't
* change the data objects to be compared
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to child nodes containing the specified data
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a const lvalue
* reference bound to type U which means one data object , the second one is a const
* lvalue reference bound to type U which means another data object
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenOfData(U const &value, std::function<int(U const &, U const
&)>
equal)
-> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> res;
for (int i = 0; i < children.len(); ++i)
{
TreeNode<U> *temp = children.at(i);
if (equal(value, temp->data))
{
res.addAfter(res.len() - 1, temp);
}
}
return std::move(res);
}
/**
* @brief traverse current node's child-nodes list and then get indexes of child
* nodes containing the specified data
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which won't
* change the data objects to be compared
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of indexes
* of child nodes containing the specified data
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a const lvalue
* reference bound to type U which means one data object , the second one is a const
* lvalue reference bound to type U which means another data object
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenIndexOfData(U const &value, std::function<int(U const &, U const &)> equal)
-> linkBase::LinkedList::LinkedList<int>
{
linkBase::LinkedList::LinkedList<int> res;
for (int i = 0; i < children.len(); ++i)
{
if (equal(value, children.at(i)->data))
{
res.addAfter(res.len() - 1, i);
}
}
return std::move(res);
}
/**
* @brief traverse current node's child-nodes list and then get child nodes
* containing the specified data
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which will
* change the data objects to be compared
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to child nodes containing the specified data
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a lvalue
* reference bound to type U which means one data object , the second one is a lvalue
* reference bound to type U which means another data object
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenOfData(U &value, std::function<int(U &, U &)> equal) -> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> res;
for (int i = 0; i < children.len(); ++i)
{
TreeNode<U> *temp = children.at(i);
if (equal(value, temp->data))
{
res.addAfter(res.len() - 1, temp);
}
}
return std::move(res);
}
/**
* @brief traverse current node's child-nodes list and then get indexes of child
* nodes containing the specified data
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which will
* change the data objects to be compared
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of indexes
* of child nodes containing the specified data
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a lvalue
* reference bound to type U which means one data object , the second one is a lvalue
* reference bound to type U which means another data object
* @par Order:
* the returned list is consistent with the child-nodes list in sequential order of
* nodes
*/
virtual auto getChildrenIndexOfData(U &value, std::function<int(U &, U &)> equal) -> linkBase::LinkedList::LinkedList<int>
{
linkBase::LinkedList::LinkedList<int> res;
for (int i = 0; i < children.len(); ++i)
{
if (equal(value, children.at(i)->data))
{
res.addAfter(res.len() - 1, i);
}
}
return std::move(res);
}
/**
* @brief get parent node
* @return a pointer to parent node
* @note changing the returned value won't actually effect the parent pointer in
* current node , but the parent node of current node can be effected through the
* returned pointer
*/
virtual auto getParent() -> TreeNode<U> *
{
return parent;
}
/**
* @brief get the child node of specified index in the child-nodes list
* @param index
* @return
* - if the index exists , return a nonnull pointer to the specified node;
* - or else , return nullptr
*/
virtual auto getChildOfIndex(int index) -> TreeNode<U> *
{
TreeNode<U> *res = nullptr;
if (index >= 0 && index < children.len())
{
res = children.at(index);
}
return res;
}
/**
* @brief get the number of child nodes
* @return the number of child nodes
*/
virtual int childNum()
{
return children.len();
}
/**
* @brief whether current node is a leaf-node
* @return
* - if current node is a leaf-node , return 1;
* - or else , return 0
*/
virtual int isLeaf()
{
return children.isEmpty();
}
/**
* @brief get all leaf-nodes of current node
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to each leaf-node
* @note if current node is a leaf-node , the function will return an empty list
* @par Order:
* take current node as a tree-root-node , and then place this tree in this way:
* <ol>
* <li>root-up
* <li>among all the child nodes of a node , the child node with least index in the
* child-nodes list placed in the far left
* </ol>
* in this case , the order of the leaf-nodes in the returned list is:
* up-to-down and left-to-right
*/
virtual auto getLeafNodes() -> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
linkBase::Queue::Queue<TreeNode<U> *> nodes;
linkBase::LinkedList::LinkedList<TreeNode<U> *> leaves;
if (this->isLeaf())
{
return std::move(leaves);
}
nodes.push(this);
while (!nodes.isEmpty())
{
int times = nodes.len();
for (int i = 0; i < times; ++i)
{
if (nodes.front()->isLeaf())
{
leaves.addAfter(leaves.len() - 1, nodes.front());
}
else
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> childrenTemp = std::move(nodes.front()->getChildren());
for (int j = 0; j < childrenTemp.len(); ++j)
{
nodes.push(childrenTemp.at(j));
}
}
nodes.pop();
}
}
return std::move(leaves);
}
/**
* @brief get current node and all recursive child nodes of current node
* @return a [list](@ref Telephone_DS::linkBase::LinkedList::LinkedList) of pointers
* to current node and each recursive child nodes of current node
* @par Order:
* take current node as a tree-root-node , and then place this tree in this way:
* <ol>
* <li>root-up
* <li>among all the child nodes of a node , the child node with least index in the
* child-nodes list placed in the far left
* </ol>
* in this case , the order of the nodes in the returned list is:
* up-to-down and left-to-right
*/
virtual auto getAllNodes() -> linkBase::LinkedList::LinkedList<TreeNode<U> *>
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> res;
linkBase::Queue::Queue<TreeNode<U> *> row;
row.push(this);
while (!row.isEmpty())
{
int times = row.len();
for (int i = 0; i < times; ++i)
{
TreeNode<U> *f = row.front();
linkBase::LinkedList::LinkedList<TreeNode<U> *> childrenList = std::move(f->getChildren());
for (int j = 0; j < childrenList.len(); ++j)
{
row.push(childrenList.at(j));
}
res.addAfter(res.len() - 1, f);
row.pop();
}
}
return std::move(res);
}
/**
* @brief create a new node with specified data and then add a pointer to the new node
* to the end of the child-nodes list
* @param dat specified data
* @note use copy-constructor of type T to copy the data to the new node
*/
virtual void addChildWithData(U const &dat)
{
children.addAfter(children.len() - 1, new TreeNode<U>(dat));
children.at(children.len() - 1)->setParent(*this);
}
/**
* @brief create a new node with specified data and then add a pointer to the new node
* to the end of the child-nodes list
* @param dat specified data
* @note use move-constructor of type T to move the data to the new node
*/
virtual void addChildWithData(U &&dat)
{
children.addAfter(children.len() - 1, new TreeNode<U>(std::move(dat)));
children.at(children.len() - 1)->setParent(*this);
}
/**
* @brief create a new node with specified data and then insert a pointer to it
* before the node of specified index in the child-nodes list
* @param index
* @param dat specified data
* @return
* - if the index doesn't exist , return -1;
* - or else , return 0
* @warning the index must exist , or else the insertion will fail
* @par After inserting:
* the new child node's parent will be set to current node
* @note use copy-constructor of type T to copy the data to the new node
*/
virtual int insertChildBeforeWithData(int index, U const &dat)
{
if (index < 0 || index >= children.len())
return -1;
children.addBefore(index, new TreeNode<U>(dat));
children.at(index)->setParent(*this);
return 0;
}
/**
* @brief create a new node with specified data and then insert a pointer to it
* before the node of specified index in the child-nodes list
* @param index
* @param dat specified data
* @return
* - if the index doesn't exist , return -1;
* - or else , return 0
* @warning the index must exist , or else the insertion will fail
* @par After inserting:
* the new child node's parent will be set to current node
* @note use move-constructor of type T to move the data to the new node
*/
virtual int insertChildBeforeWithData(int index, U &&dat)
{
if (index < 0 || index >= children.len())
return -1;
children.addBefore(index, new TreeNode<U>(std::move(dat)));
children.at(index)->setParent(*this);
return 0;
}
/**
* @brief create a new node with specified data and then insert a pointer to it
* after the node of specified index in the child-nodes list
* @param index
* @param dat specified data
* @return
* - if the index doesn't exist , return -1;
* - or else , return 0
* @warning the index must exist , or else the insertion will fail
* @par After inserting:
* the new child node's parent will be set to current node
* @note use copy-constructor of type T to copy the data to the new node
*/
virtual int insertChildAfterWithData(int index, U const &dat)
{
if (index < 0 || index >= children.len())
return -1;
children.addAfter(index, new TreeNode<U>(dat));
children.at(index + 1)->setParent(*this);
return 0;
}
/**
* @brief create a new node with specified data and then insert a pointer to it
* after the node of specified index in the child-nodes list
* @param index
* @param dat specified data
* @return
* - if the index doesn't exist , return -1;
* - or else , return 0
* @warning the index must exist , or else the insertion will fail
* @par After inserting:
* the new child node's parent will be set to current node
* @note use move-constructor of type T to move the data to the new node
*/
virtual int insertChildAfterWithData(int index, U &&dat)
{
if (index < 0 || index >= children.len())
return -1;
children.addAfter(index, new TreeNode<U>(std::move(dat)));
children.at(index + 1)->setParent(*this);
return 0;
}
/**
* @brief remove the first node in the child-nodes list , just like calling the @link
* ~TreeNode() destructor @endlink of the first child-node
* @return
* - if the child-nodes list is empty , return -1;
* - or else , return 0
* @warning the child-nodes list must not be empty , or else the remove will fail
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeFirstChild()
{
if (children.isEmpty())
return -1;
linkBase::LinkedList::LinkedList<TreeNode<U> *> xChildren = std::move(children.at(0)->getChildren());
delete children.at(0);
children.deleteFrom(0, 1);
for (int i = 0; i < xChildren.len(); ++i)
{
xChildren.at(i)->setParent(*this);
children.addAfter(children.len() - 1, xChildren.at(i));
}
return 0;
}
/**
* @brief remove the last node in the child-nodes list , just like calling the @link
* ~TreeNode() destructor @endlink of the last child-node
* @return
* - if the child-nodes list is empty , return -1;
* - or else , return 0
* @warning the child-nodes list must not be empty , or else the remove will fail
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeLastChild()
{
if (children.isEmpty())
return -1;
linkBase::LinkedList::LinkedList<TreeNode<U> *> xChildren = std::move(children.at(children.len() - 1)->getChildren());
delete children.at(children.len() - 1);
children.deleteFrom(children.len() - 1, 1);
for (int i = 0; i < xChildren.len(); ++i)
{
xChildren.at(i)->setParent(*this);
children.addAfter(children.len() - 1, xChildren.at(i));
}
return 0;
}
/**
* @brief remove the node of specified index in the child-nodes list , just like
* calling the @link ~TreeNode() destructor @endlink of the specified child-node
* @param index the specified index
* @return
* - if the index doesn't exist , return -1;
* - or else , return 0
* @warning the index must exist , or else the remove will fail
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeChildOfIndex(int index)
{
if (index < 0 || index >= children.len())
return -1;
linkBase::LinkedList::LinkedList<TreeNode<U> *> xChildren = std::move(children.at(index)->getChildren());
delete children.at(index);
children.deleteFrom(index, 1);
for (int i = 0; i < xChildren.len(); ++i)
{
xChildren.at(i)->setParent(*this);
children.addAfter(children.len() - 1, xChildren.at(i));
}
return 0;
}
/**
* @brief remove those child nodes containing the specified data , just like calling
* the @link ~TreeNode() destructor @endlink of them
* @param value the specified data
* @return the number of removed nodes
* @note if there is no node containing the specified data , this function will do
* nothing and return 0
* @par How to compare:
* - if the type of node's data part is floating-point(float , double , long double)
* number , use floating-point error (1e-6);
* - or else , use operator ==
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeChildrenOfData(U const &value)
{
int removedNum = 0;
linkBase::LinkedList::LinkedList<int> rmNodeIndexes =
std::move(getChildrenIndexOfData(value));
for (int i = rmNodeIndexes.len() - 1; i >= 0; --i)
{
removeChildOfIndex(rmNodeIndexes.at(i));
removedNum++;
}
return removedNum;
}
/**
* @brief remove those child nodes containing the specified data , just like calling
* the @link ~TreeNode() destructor @endlink of them
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which won't
* change the data objects to be compared
* @return the number of removed nodes
* @note if there is no node containing the specified data , this function will do
* nothing and return 0
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a const lvalue
* reference bound to type U which means one data object , the second one is a const
* lvalue reference bound to type U which means another data object
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeChildrenOfData(U const &value, std::function<int(U const &, U const &)> equal)
{
int removedNum = 0;
linkBase::LinkedList::LinkedList<int> rmNodeIndexes =
std::move(getChildrenIndexOfData(value, equal));
for (int i = rmNodeIndexes.len() - 1; i >= 0; --i)
{
removeChildOfIndex(rmNodeIndexes.at(i));
removedNum++;
}
return removedNum;
}
/**
* @brief remove those child nodes containing the specified data , just like calling
* the @link ~TreeNode() destructor @endlink of them
* @param value the specified data
* @param equal function to judge whether one data is equal to another , which will
* change the data objects to be compared
* @return the number of removed nodes
* @note if there is no node containing the specified data , this function will do
* nothing and return 0
* @par How to compare:
* call a function using the callable object passed in by the parameter to compare
* whether the data part of each child node is equal to the specified data:
* - if equal , the function should return 1
* - if not equal , the function should return 0
* - the function should have two formal parameters , the first one is a lvalue
* reference bound to type U which means one data object , the second one is a lvalue
* reference bound to type U which means another data object
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeChildrenOfData(U &value, std::function<int(U &, U &)> equal)
{
int removedNum = 0;
linkBase::LinkedList::LinkedList<int> rmNodeIndexes =
std::move(getChildrenIndexOfData(value, equal));
for (int i = rmNodeIndexes.len() - 1; i >= 0; --i)
{
removeChildOfIndex(rmNodeIndexes.at(i));
removedNum++;
}
return removedNum;
}
/**
* @brief remove all child nodes , just like calling the @link ~TreeNode() destructor
* @endlink of them
* @return the number of removed nodes
* @note if the child-nodes list is empty , this function will do nothing and return 0
* @par After removing:
* as for the child nodes of the node being removed:
* - pointers to them will be added to the end of current node's child-nodes list
* - their parent will be set to current node
*/
virtual int removeAllChild()
{
int removedNum = 0;
for (int i = children.len() - 1; i >= 0; --i)
{
removeChildOfIndex(i);
removedNum++;
}
return removedNum;
}
/**
* @brief take current node as a tree-root-node , traverse the tree by rows(include
* the root-node)
* @param doSomething a function to call when this function traverses each node
* @par Order:
* take current node as a tree-root-node , and then place this tree in this way:
* <ol>
* <li>root-up
* <li>among all the child nodes of a node , the child node with least index in the
* child-nodes list placed in the far left
* </ol>
* in that case , this function will traverse all nodes of the tree row by row from
* up to down while traversing from left to right in a row
* @par Action:
* As the function traverses each node , each time it will call the callable object
* passed in by the parameter. For the callable object passed in by the parameter ,
* there are the following requirements:
* -# return void
* -# the <B>first</B> formal parameter is an lvalue reference bound to a
* TreeNode<U> , which means the node being traversed currently
* -# the <B>second</B> formal parameter is a int , which means the index of the row
* being traversed currently(place the tree as described above , assume that the
* index of the top row is 0 , and row-index increases downward)
* -# the <B>third</B> formal parameter is a int , which means the index of the node
* in the current traversed row(place the tree as described above , number each node
* in the current traversed row , assume that the node-index of the most-left node is
* 0 , and node-index increases rightward)
* - the formal parameter list can be like this:
* (TreeNode<U> &nowNode , int rowIndex , int nodeIndex)
*/
virtual void traverseAsTree(std::function<void(TreeNode<U> &, int, int)> doSomething)
{
linkBase::Queue::Queue<TreeNode<U> *> row;
row.push(this);
int rIndex = -1;
while (!row.isEmpty())
{
rIndex++;
int times = row.len();
for (int i = 0; i < times; ++i)
{
TreeNode<U> *f = row.front();
linkBase::LinkedList::LinkedList<TreeNode<U> *> childrenList = std::move(f->getChildren());
for (int j = 0; j < childrenList.len(); ++j)
{
row.push(childrenList.at(j));
}
doSomething(*f, rIndex, i);
row.pop();
}
}
}
/**
* @brief get the depth of current node(the root-depth is 0)
* @return the depth of current node(the root-depth is 0)
*/
virtual int depth()
{
int depth = 0;
TreeNode<U> *temp = this;
while (temp->parent != nullptr)
{
temp = temp->parent;
depth++;
}
return depth;
}
/**
* @brief get the height of current node(every leaf-node has a height of 0)
* @return the height of current node(every leaf-node has a height of 0)
* @note the height of current node is the longest path length between a leaf-node
* and current node among all leaf-nodes
*/
virtual int height()
{
linkBase::Queue::Queue<TreeNode<U> *> nodeQueue;
nodeQueue.push(this);
int height = -1;
while (!nodeQueue.isEmpty())
{
height++;
int times = nodeQueue.len();
for (int i = 0; i < times; ++i)
{
linkBase::LinkedList::LinkedList<TreeNode<U> *> childrenList =
std::move(nodeQueue.front()->getChildren());
for (int j = 0; j < childrenList.len(); ++j)
{
nodeQueue.push(childrenList.at(j));
}
nodeQueue.pop();
}
}
return height;
}
private:
/**
* @brief set the parent node of current node to the specified node
* @param node the specified node
* @note this function won't add the pointer of current node to the specified node's
* child-nodes list. Actually, this function will do nothing besides setting current
* node's parent node to the specified node
*/
virtual void setParent(TreeNode<U> &node)
{
parent = &node;
}
/**
* @brief you can access the private member function of class @link
* Telephone_DS::treeBase::Tree::Tree::TreeNode TreeNode @endlink in class @link
* Telephone_DS::treeBase::Tree::Tree Tree @endlink
* @note this is to prevent users from calling some member functions of @link
* Telephone_DS::treeBase::Tree::Tree::TreeNode TreeNode @endlink
*/
friend class Tree;
};
public:
TreeNode<T> root;
public:
/**
* @brief general constructor(copy)
* @param data data of the root-node
* @note use copy-constructor of type T to copy the data to the root-node
* @par init
* - root = a new node containing specified data with null parent-pointer and empty
* child-nodes list
*/
explicit Tree(T const &data)
: root(data)
{
}
/**
* @brief general constructor(move)
* @param data data of the root-node
* @note use move-constructor of type T to move the data to the root-node
* @par init
* - root = a new node containing specified data with null parent-pointer and empty
* child-nodes list
*/
explicit Tree(T &&data)
: root(std::move(data))
{
}
/**
* @brief copy-constructor
* @param src
* @note use copy-constructor of type T to copy data of all nodes from the tree passed in
* by parameter to current tree
* @par Result:
* current tree will have a same struct of nodes as the tree passed in by parameter , and
* the nodes of current tree will contain the same data as the nodes of the tree passed
* in by parameter
*/
Tree(Tree<T> const &src)
: root(src.root.data)
{
linkBase::Queue::Queue<TreeNode<T> *> row;
row.push(&root);
src.root.traverseAsTree(