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