00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef Tree_L_h
00013 #define Tree_L_h
00014
00015 #ifndef NDEBUG // ==> Overview of File Translation ==> C++
00016 #define USE_v_Alive
00017 #undef USE_v_Alive
00018 #endif
00019
00020 #include <cassert>
00021
00022
00023 namespace TL {
00024
00025 template <class E> class Tree;
00026 template <class E> bool check_ok( const E & );
00027 template <class E> bool check_ok( const Tree<E>& T);
00028
00029
00030 template <class E>
00031 class Tree_Node {
00032 public:
00033 friend class Tree<E>;
00034 typedef E value_type;
00035 private:
00036 Tree_Node( const value_type& d ) : m_data( d ), m_refCount(1) {}
00037 static Tree_Node<E>* Get_New(const value_type& d);
00038 unsigned Abs_n_child() const;
00039 private:
00040 value_type m_data;
00041 unsigned m_refCount;
00042 int m_n_child;
00043 Tree_Node<E> * m_left_child;
00044 Tree_Node<E> * m_right_brother;
00045 private:
00046 #ifdef USE_v_Alive // ==> NDEBUG ==> Overview of File Translation ==> C++
00047 static const unsigned N_Alive = 333;
00048 static Tree_Node<E> * m_v_Alive[N_Alive];
00049 static unsigned m_n_Alive;
00050 #endif
00051 };
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 template <class E>
00068 class Tree {
00069 public:
00070 typedef E value_type;
00071
00072
00073 public:
00074 Tree() : m_root(0) {}
00075 Tree(const Tree& o);
00076 Tree(const value_type & d);
00077 ~Tree();
00078 private:
00079
00080 Tree(Tree_Node<E>* p) : m_root(p) { if (p!=0) { p->m_refCount++; } }
00081
00082 typedef void NOT_null_pointer;
00083
00084 Tree(NOT_null_pointer* p) : m_root( (Tree_Node<E>*)p) {
00085 ((Tree_Node<E>*)p)->m_refCount++; }
00086
00087
00088
00089
00090 public:
00091 bool Empty() const { return (m_root == 0); }
00092 unsigned Count() const ;
00093 unsigned Count_Children() const ;
00094 unsigned Size () const { return Count(); }
00095 friend bool check_ok<E>(const Tree<E>& T);
00096 bool Ok() { return check_ok(*this); }
00097
00098
00099
00100
00101 public:
00102 void Erase();
00103 void Erase_Son(unsigned n) { Tree_Node<E>*p; Erase_Son_Prev(n+1,p ); }
00104 private:
00105 void Erase_Son_Prev(unsigned n, Tree_Node<E>*& );
00106
00107
00108
00109
00110 public:
00111 Tree& operator= (const Tree &o) { return Copy(o); }
00112 Tree& Copy (const Tree &o);
00113 Tree& Move (Tree &o);
00114 Tree& Swap (Tree &o);
00115 Tree& Copy_Deep( const Tree &o );
00116
00117 Tree& operator=( const value_type & d) { Change_Root(d); return *this; }
00118
00119
00120
00121
00122 public:
00123 Tree Change_Root(const value_type &d);
00124 Tree Change_Child( unsigned n, const value_type &d );
00125 Tree Change_Left_Sibling_Inmediate( const value_type &d );
00126 Tree Change_Right_Sibling_Inmediate( const value_type &d );
00127 Tree Graft( unsigned n, Tree &o );
00128
00129
00130
00131
00132 public:
00133 value_type& Data () { return m_root->m_data; }
00134 value_type& operator * () { return Data(); }
00135 value_type* operator -> () { return &(m_root->m_data); }
00136 const value_type& Data () const { return m_root->m_data; }
00137 const value_type& operator * () const { return Data(); }
00138 const value_type* operator -> () const { return &(m_root->m_data); }
00139
00140
00141
00142
00143 public:
00144 template <class X> friend bool operator == (const Tree<X> &p, const Tree<X> &q);
00145 template <class X> friend bool operator != (const Tree<X> &p, const Tree<X> &q);
00146 bool Equals( const Tree & o ) const { return (*this)==o; }
00147
00148 bool Same( const Tree & o ) const { return m_root == o.m_root; }
00149
00150
00151
00152
00153 public:
00154 Tree Root() const { return Tree( m_root ); }
00155 Tree Father() const ;
00156 Tree Child(unsigned n) const;
00157 Tree Left() const;
00158 Tree Right() const;
00159 Tree Leftmost() const;
00160 Tree Rightmost() const;
00161 Tree Sibling(int n) const;
00162 Tree Left_Sibling() const;
00163 Tree Right_Sibling() const;
00164 Tree Previous_Sibling() const { return Left_Sibling(); }
00165 Tree Prev_Sibling() const { return Left_Sibling(); }
00166 Tree Next_Sibling() const { return Right_Sibling(); }
00167 Tree Right_Sibling_Inmediate() const;
00168 Tree Left_Sibling_Inmediate() const;
00169
00170
00171
00172
00173 public:
00174
00175 unsigned Child_Number() const { return ( m_root==0 ? 0 : m_root->Abs_n_child() - 1); }
00176
00177 unsigned Leftmost_N() const { return Leftmost().Child_Number(); }
00178
00179 unsigned Rightmost_N() const { return Rightmost().Child_Number(); }
00180
00181 bool Is_Leaf() const { return (m_root != 0) && (m_root->m_left_child != 0); }
00182
00183 bool Is_Root() const { return (m_root != 0) && (m_root->m_right_brother == 0); }
00184
00185 unsigned use_count() const { return (m_root==0 ? 0 : m_root->m_refCount); }
00186
00187
00188
00189
00190 private:
00191
00192 static Tree& CastTo_Sub_Tree_Ref ( Tree_Node<E> * &p )
00193 { return (Tree&) *( reinterpret_cast<Tree*> (&p)); }
00194
00195
00196
00197
00198 private:
00199 static void Erase0(Tree_Node<E> * p);
00200 static bool Comp0(const Tree_Node<E> *p, const Tree_Node<E> *q);
00201 static void Count0(const Tree_Node<E> * p, unsigned & n);
00202 static Tree_Node<E>* Copy_Deep0(const Tree_Node<E> * p);
00203
00204 private:
00205 Tree_Node<E> * m_root;
00206 };
00207
00208 #ifdef USE_v_Alive
00209 unsigned Tree::Tree_Node<E>::m_n_Alive = 0;
00210 Tree::Tree_Node<E>* Tree::Tree_Node<E>::m_v_Alive[Tree::Tree_Node<E>::N_Alive];
00211 #endif
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 template <class E>
00224 inline Tree_Node<E> * Tree_Node<E>::Get_New( const E& d) {
00225 Tree_Node<E>* p = new Tree_Node(d);
00226 #ifdef USE_v_Alive
00227 m_v_Alive[m_n_Alive] = p;
00228 m_n_Alive++;
00229 #endif
00230 return p;
00231 }
00232
00233
00234
00235
00236 template <class E>
00237 inline Tree<E>::Tree(const Tree& o) {
00238 if (o.m_root != 0) {
00239 o.m_root->m_refCount++;
00240 }
00241 m_root = o.m_root;
00242 }
00243
00244
00245
00246
00247
00248
00249 template <class E>
00250 Tree<E>::~Tree() {
00251 if (m_root != 0) {
00252 if (m_root->m_refCount == 1) {
00253 Erase0(m_root);
00254 } else {
00255 m_root->m_refCount--;
00256 }
00257 }
00258 }
00259
00260
00261 template <class E>
00262 inline Tree<E>::Tree(const E & d) {
00263 m_root = Tree_Node<E>::Get_New(d);
00264 m_root->m_n_child = -1;
00265 m_root->m_left_child = 0;
00266 m_root->m_right_brother = 0;
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 template <class E>
00279 Tree<E>& Tree<E>::Copy( const Tree &o ) {
00280 if (m_root != o.m_root) {
00281 if (m_root != 0) {
00282 if ( m_root->m_refCount == 1 ) {
00283 Erase0(m_root);
00284 } else {
00285 m_root->m_refCount--;
00286 }
00287 }
00288 m_root = o.m_root;
00289 if (o.m_root != 0) {
00290 o.m_root->m_refCount++;
00291 }
00292 }
00293 return *this;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 template <class E>
00311 Tree<E>& Tree<E>::Move( Tree &o ) {
00312 if (m_root == o.m_root) {
00313 if (m_root != 0) {
00314 if ( m_root->m_refCount == 1 ) {
00315 Erase0(m_root);
00316 } else {
00317 m_root->m_refCount--;
00318 }
00319 }
00320 m_root = o.m_root;
00321 o.m_root = 0;
00322 }
00323 return *this;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341 template <class E>
00342 inline Tree<E>& Tree<E>::Swap( Tree &o ) {
00343 Tree_Node<E> * tmp = m_root;
00344 m_root = o.m_root;
00345 o.m_root = tmp;
00346 return *this;
00347 }
00348
00349
00350
00351
00352
00353
00354 template <class E>
00355 Tree<E> Tree<E>::Father() const {
00356 if (m_root == 0) {
00357 return Tree( );
00358 }
00359
00360 Tree_Node<E>* p = m_root;
00361 while (p->m_n_child > 0) {
00362 p = p->m_right_brother;
00363 }
00364 return Tree( p->m_right_brother );
00365
00366
00367 }
00368
00369
00370
00371
00372
00373
00374 template <class E>
00375 Tree<E> Tree<E>::Child( unsigned n ) const {
00376 if (m_root == 0) {
00377 return Tree( );
00378 }
00379
00380 Tree_Node<E>* child = m_root->m_left_child;
00381 if (child == 0) {
00382 return Tree( );
00383 }
00384
00385 n++;
00386
00387 for (;;) {
00388 if (child->m_n_child <= 0) {
00389 if ( unsigned(- child->m_n_child) == n ) {
00390 return Tree( (NOT_null_pointer*) child );
00391 } else {
00392 return Tree( );
00393 }
00394 } else {
00395 if ( child->m_n_child == n ) {
00396 return Tree( (NOT_null_pointer*) child );
00397 } else if ( unsigned(child->m_n_child) < n) {
00398 child = child->m_right_brother;
00399 } else {
00400 return Tree( );
00401 }
00402 }
00403 }
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 template <class E>
00429 inline Tree<E> Tree<E>::Left() const {
00430 if (m_root == 0) {
00431 return Tree( );
00432 } else if ( -1 ==m_root->m_left_child->m_n_child ||
00433 1 ==m_root->m_left_child->m_n_child ) {
00434 return Tree( (NOT_null_pointer*) m_root->m_left_child );
00435 }
00436 return Tree( );
00437 }
00438
00439
00440
00441
00442
00443
00444 template <class E>
00445 Tree<E> Tree<E>::Right() const {
00446 if (m_root == 0) {
00447 return Tree( );
00448 } else if ( 0 == m_root->m_left_child ) {
00449 return Tree( );
00450 } else if ( -1 ==m_root->m_left_child->m_n_child ) {
00451 return Tree( );
00452 } else if ( 1 ==m_root->m_left_child->m_n_child ) {
00453 if ( -2 == m_root->m_left_child->m_right_brother->m_n_child ||
00454 2 == m_root->m_left_child->m_right_brother->m_n_child ) {
00455 return Tree( (NOT_null_pointer*) m_root->m_left_child->m_right_brother );
00456 }
00457 } else if ( 2 == m_root->m_left_child->m_n_child || -2 == m_root->m_left_child->m_n_child ) {
00458 return Tree( (NOT_null_pointer*) m_root->m_left_child );
00459 }
00460 return Tree( );
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 template <class E>
00474 Tree<E> Tree<E>::Left_Sibling() const {
00475 if (m_root == 0) {
00476 return Tree( );
00477 }
00478
00479 unsigned n_child = m_root->Abs_n_child()-1;
00480 if ( n_child <= 0 ) {
00481 return Tree( );
00482 }
00483 #ifdef ESTO_SOBRA_en_Left_Sibling
00484 if (m_root->m_right_brother == 0) {
00485 return Tree( );
00486 }
00487
00488
00489
00490 #endif
00491
00492 Tree_Node<E>* brother = m_root;
00493 while (brother->m_n_child > 0) {
00494 brother = brother->m_right_brother;
00495 }
00496 Tree_Node<E>* father = brother->m_right_brother;
00497
00498
00499 brother = father->m_left_child;
00500 if ( m_root == brother ) {
00501 return Tree( );
00502 }
00503 while ( m_root != brother->m_right_brother ) {
00504 brother = brother->m_right_brother;
00505 }
00506
00507 return Tree( brother );
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 template <class E>
00521 Tree<E> Tree<E>::Right_Sibling() const {
00522 if (m_root == 0) {
00523 return Tree( );
00524 } if (m_root->m_n_child < 0) {
00525 return Tree( );
00526 } else {
00527 return Tree( m_root->m_right_brother );
00528
00529
00530 }
00531 }
00532
00533
00534
00535
00536
00537
00538
00539 template <class E>
00540 inline Tree<E> Tree<E>::Left_Sibling_Inmediate() const {
00541 return Sibling( -1 );
00542 }
00543
00544
00545
00546
00547
00548
00549
00550 template <class E>
00551 Tree<E> Tree<E>::Right_Sibling_Inmediate() const {
00552
00553 if (m_root == 0) {
00554 return Tree( );
00555 }
00556 if (m_root->m_n_child <= 0) {
00557 return Tree( );
00558 }
00559
00560 Tree_Node<E>* brother = m_root->m_right_brother;
00561
00562 int n_brother = brother->Abs_n_child() - 1;
00563 if ( m_root->m_n_child == n_brother ) {
00564 return Tree( (NOT_null_pointer*) brother );
00565 } else {
00566 return Tree( );
00567 }
00568 #if 0
00569 if (brother->m_n_child > 0) {
00570 if ( m_root->m_n_child + 1 == brother->m_n_child ) {
00571 return Tree( (NOT_null_pointer*) brother );
00572 } else {
00573 return Tree( );
00574 }
00575 } else {
00576 if ( m_root->m_n_child + 1 == - brother->m_n_child ) {
00577 return Tree( (NOT_null_pointer*) brother );
00578 } else {
00579 return Tree( );
00580 }
00581 }
00582 #endif
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 template <class E>
00594 inline unsigned Tree_Node<E>::Abs_n_child() const {
00595 return ( m_n_child >= 0 ? unsigned(m_n_child) : unsigned( - m_n_child ) );
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 template <class E>
00625 Tree<E> Tree<E>::Sibling( int n ) const {
00626 if (m_root == 0) {
00627 return Tree( );
00628 } else if ( m_root->m_right_brother == 0 ) {
00629 return Tree( );
00630 } else if ( n==0 ) {
00631 return Tree( (NOT_null_pointer*) m_root );
00632 }
00633
00634 Tree_Node<E>* brother;
00635 unsigned n_target;
00636
00637
00638 if ( n < 0 ) {
00639 unsigned n_child = m_root->Abs_n_child()-1;
00640 unsigned abs_n = unsigned(-n);
00641 if ( n_child < abs_n ) {
00642 return Tree( );
00643 } else {
00644 n_target = n_child - abs_n;
00645
00646 brother = m_root;
00647 while (brother->m_n_child > 0) {
00648 brother = brother->m_right_brother;
00649 }
00650 brother = brother->m_right_brother->m_left_child;
00651 }
00652 } else {
00653 brother = m_root;
00654 n_target = unsigned(n) + m_root->Abs_n_child()-1;
00655 }
00656
00657
00658 ++n_target;
00659
00660
00661
00662 for (;;) {
00663 if (brother->m_n_child < 0) {
00664 if ( unsigned(- brother->m_n_child) == n_target ) {
00665 return Tree( (NOT_null_pointer*) brother );
00666 } else {
00667 return Tree( );
00668 }
00669 } else {
00670 unsigned n_child = brother->m_n_child;
00671 if (n_child == n_target) {
00672 return Tree( (NOT_null_pointer*) brother );
00673 } else if (n_child < n_target) {
00674 brother = brother->m_right_brother;
00675 } else {
00676 return Tree( );
00677 }
00678 }
00679 }
00680 }
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 template <class E>
00698 inline Tree<E> Tree<E>::Leftmost() const {
00699 if (m_root == 0) {
00700 return Tree( );
00701 }
00702 return Tree( m_root->m_left_child );
00703 }
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720 template <class E>
00721 Tree<E> Tree<E>::Rightmost() const {
00722 if (m_root == 0) {
00723 return Tree( );
00724 }
00725 Tree_Node<E>* child = m_root->m_left_child;
00726 if (child == 0) {
00727 return Tree( );
00728 }
00729
00730 while (child->m_n_child > 0) {
00731 child = child->m_right_brother;
00732 }
00733 return Tree( (NOT_null_pointer*) child );
00734 }
00735
00736
00737
00738
00739
00740
00741
00742
00743 template <class E>
00744 void Tree<E>::Count0(const Tree_Node<E> * p, unsigned & n) {
00745 Tree_Node<E>* child = p->m_left_child;
00746 if (child == 0) {
00747 return;
00748 }
00749
00750 for (;;) {
00751 ++n;
00752 Count0( child, n );
00753 if (child->m_n_child > 0) {
00754 child = child->m_right_brother;
00755 } else {
00756 break;
00757 }
00758 }
00759 }
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769 template <class E>
00770 unsigned Tree<E>::Count() const {
00771 if (m_root == 0) {
00772 return 0;
00773 } else {
00774 unsigned n = 1;
00775 Count0(m_root, n);
00776 return n;
00777 }
00778 }
00779
00780
00781
00782
00783 template <class E>
00784 unsigned Tree<E>::Count_Children() const {
00785 if (m_root == 0) {
00786 return 0;
00787 }
00788 Tree_Node<E>* child = m_root->m_left_child;
00789 if ( 0 == child ) {
00790 return 0;
00791 }
00792 unsigned tot = 0;
00793 for (;;) {
00794 ++tot;
00795 if (child->m_n_child > 0) {
00796 child = child->m_right_brother;
00797 } else {
00798 break;
00799 }
00800 }
00801 return tot;
00802 }
00803 template <class E>
00804 inline bool operator == (const Tree<E> &p, const Tree<E> &q) { return Tree<E>::Comp0(p.m_root, q.m_root); }
00805 template <class E>
00806 inline bool operator != (const Tree<E> &p, const Tree<E> &q) { return !(p == q); }
00807
00808
00809
00810 template <class E>
00811 bool Tree<E>::Comp0(const Tree_Node<E> *p, const Tree_Node<E> *q) {
00812 if (p == q) {
00813 return true;
00814 }
00815 if ( (p==0) || (q==0)) {
00816 return false;
00817 }
00818 if ( p->m_n_child != q->m_n_child ) {
00819 return false;
00820 }
00821 if ( ! (p->m_data == q->m_data) ) {
00822 return false;
00823 }
00824
00825
00826 p = p->m_left_child;
00827 q = q->m_left_child;
00828 for (;;) {
00829 if (p == q) {
00830 return true;
00831 }
00832 if ( (p==0) || (q==0) ) {
00833 return false;
00834 }
00835 if ( p->m_n_child != q->m_n_child ) {
00836 return false;
00837 }
00838 if (! Comp0(p, q) ) {
00839 return false;
00840 }
00841 if ( p->m_n_child < 0 ) {
00842 return true;
00843 }
00844 p = p->m_right_brother;
00845 q = q->m_right_brother;
00846 }
00847 }
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 template <class E>
00872 bool check_ok(const Tree<E>& T) {
00873
00874 if ( T.Empty() ) {
00875 return true;
00876 }
00877
00878 #ifdef NO_Implementado_para_evitar_forzar_que_el_valor_almacenado_tenga_su_metodo_OK
00879 if (! check_ok( T.Data() ) ) {
00880 return false;
00881 }
00882
00883
00884
00885
00886
00887
00888
00889
00890 #endif
00891
00892 unsigned N = T.Rightmost().Child_Number();
00893 for (unsigned i = 0; i < N; ++i) {
00894 if ( T.Child(i).Empty() ) {
00895
00896 }
00897 else if ( T.Child(i).Father() != T.Root() ) {
00898 return false;
00899 }
00900 else if ( i != T.Child(i).Child_Number() ) {
00901 return false;
00902 }
00903 else if ( T.Child(i).Father().Child( i ) != T.Child( i ) ) {
00904 return false;
00905 }
00906 else if ( ! check_ok( T.Child(i) ) ) {
00907 return false;
00908 }
00909 }
00910 return true;
00911
00912
00913
00914
00915
00916
00917
00918
00919 }
00920
00921
00922
00923
00924
00925 template <class E>
00926 Tree<E> Tree<E>::Change_Root(const value_type & d) {
00927 if (m_root == 0) {
00928 m_root = Tree_Node<E>::Get_New(d);
00929 m_root->m_left_child = 0;
00930 m_root->m_right_brother = 0;
00931 m_root->m_n_child = -1;
00932 } else {
00933 m_root->m_data = d;
00934 }
00935 return *this;
00936 }
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948 template <class E>
00949 Tree<E> Tree<E>::Change_Child(unsigned n, const value_type &d) {
00950 if (m_root == 0) {
00951 return Tree( );
00952 }
00953
00954 n++;
00955
00956 if (m_root->m_left_child == 0) {
00957 Tree_Node<E> * p = Tree_Node<E>::Get_New(d);
00958 p->m_n_child = -int(n);
00959 p->m_right_brother = m_root;
00960 m_root->m_left_child = p;
00961 p->m_left_child = 0;
00962 return Tree ( (NOT_null_pointer*) m_root->m_left_child );
00963 }
00964
00965
00966 Tree_Node<E>* brother = m_root->m_left_child;
00967 unsigned n_brother = brother->Abs_n_child();
00968
00969 if ( n < n_brother ) {
00970 Tree_Node<E>* p = Tree_Node<E>::Get_New(d);
00971 p->m_n_child = +int(n);
00972 p->m_right_brother = brother;
00973 m_root->m_left_child = p;
00974 p->m_left_child = 0;
00975 return Tree ( (NOT_null_pointer*) p );
00976 } else if ( n == n_brother ) {
00977 brother->m_data = d;
00978 return Tree ( (NOT_null_pointer*) brother );
00979 }
00980
00981
00982
00983
00984
00985
00986 Tree_Node<E>* prev = brother;
00987 for (;;) {
00988 if ( brother->m_n_child < 0 ) {
00989 if ( int(n) == - brother->m_n_child ) {
00990 brother->m_data = d;
00991 return Tree ( (NOT_null_pointer*) brother );
00992 } else if (int(n) < - brother->m_n_child ) {
00993 Tree_Node<E>* p = Tree_Node<E>::Get_New(d);
00994 p->m_n_child = int(n);
00995 p->m_right_brother = brother;
00996 prev->m_right_brother = p;
00997 p->m_left_child = 0;
00998 return Tree ( (NOT_null_pointer*) p );
00999 } else {
01000 Tree_Node<E>* p = Tree_Node<E>::Get_New(d);
01001 p->m_n_child = - int(n);
01002 p->m_right_brother = brother->m_right_brother;
01003 brother->m_n_child = - brother->m_n_child;
01004 brother->m_right_brother = p;
01005 p->m_left_child = 0;
01006 return Tree ( (NOT_null_pointer*) p );
01007 }
01008 } else {
01009 if ( int(n) == brother->m_n_child ) {
01010 brother->m_data = d;
01011 return Tree ( (NOT_null_pointer*) brother );
01012 } else if ( brother->m_n_child < int(n) ) {
01013 prev = brother;
01014 brother = brother->m_right_brother;
01015 } else {
01016 Tree_Node<E>* p = Tree_Node<E>::Get_New(d);
01017 p->m_n_child = int(n);
01018 p->m_right_brother = brother;
01019 prev->m_right_brother = p;
01020 p->m_left_child = 0;
01021 return Tree ( (NOT_null_pointer*) p );
01022 }
01023 }
01024 }
01025 }
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041 template <class E>
01042 Tree<E> Tree<E>::Change_Left_Sibling_Inmediate( const E &d ) {
01043 unsigned n = Child_Number();
01044 if (n > 0 ) {
01045 return Father().Change_Child( n-1, d );
01046 } else {
01047 return Tree( );
01048 }
01049 }
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065 template <class E>
01066 Tree<E> Tree<E>::Change_Right_Sibling_Inmediate( const value_type &d ) {
01067 if (m_root == 0) {
01068 return Tree( );
01069 }
01070 if ( m_root->m_right_brother == 0 ) {
01071 return Tree( );
01072 }
01073
01074 Tree_Node<E> * brother;
01075 if ( m_root->m_n_child < 0 ) {
01076 m_root->m_n_child = - m_root->m_n_child;
01077
01078 brother = Tree_Node<E>::Get_New( d );
01079 brother->m_n_child = - ( m_root->m_n_child + 1 );
01080
01081 brother->m_left_child = 0;
01082 brother->m_right_brother = m_root->m_right_brother;
01083 m_root->m_right_brother = brother;
01084
01085 return Tree( (NOT_null_pointer*) brother );
01086 }
01087
01088
01089 int n_brother = m_root->m_right_brother->Abs_n_child();
01090 if ( m_root->m_n_child + 1 == n_brother ) {
01091 m_root->m_right_brother->m_data = d;
01092 return Tree( (NOT_null_pointer*) m_root->m_right_brother );
01093 } else {
01094 brother = Tree_Node<E>::Get_New( d );
01095 brother->m_n_child = m_root->m_n_child + 1;
01096
01097 brother->m_left_child = 0;
01098 brother->m_right_brother = m_root->m_right_brother;
01099 m_root->m_right_brother = brother;
01100
01101 return Tree( (NOT_null_pointer*) brother );
01102 }
01103 }
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128 template <class E>
01129 void Tree<E>::Erase0( Tree_Node<E> * p ) {
01130
01131
01132
01133
01134 if ( p->m_left_child != 0 ) {
01135 Tree_Node<E> *next, *child = p->m_left_child;
01136 while ( child != 0 ) {
01137 if ( child->m_n_child < 0 ) {
01138 next = 0;
01139 } else {
01140 next = child->m_right_brother;
01141 }
01142 if ( child->m_refCount == 1 ) {
01143 Erase0( child );
01144