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