00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #define lkptr_NULL_POINTERS_ARE_ALONE
00013 #undef lkptr_NULL_POINTERS_ARE_ALONE
00014 #include "Bin_Tree.h"
00015 #include "Bin_Tree_Lib.h"
00016 #include "BUnit.h"
00017
00018 #include <iostream>
00019 #include <vector>
00020 #include <algorithm>
00021 #include <cassert>
00022 #include <iostream>
00023
00024
00025 template <class E>
00026 class test_Bin_Tree : public TestCase {
00027 public:
00028 bool run();
00029 void do_cout();
00030 void test_copyDeep();
00031 void test_homomorfo();
00032 void test_make_FBHCID();
00033 void test_make_ab_no();
00034 void test_swap();
00035 void test_mirror();
00036 void test_changeChild();
00037 void test_heightdepth();
00038 void test_releaseChild();
00039 void test_makeOrphan();
00040 void test_left_right();
00041 void test_no_swap();
00042 void test_move_swap();
00043 void test_multi_child();
00044 void test_isLeft_isRight();
00045 void test_isRoot_isLeaf();
00046 void test_sizeStrong();
00047 void test_AVL_tree();
00048 };
00049
00050
00051
00052 template <class E>
00053 bool test_Bin_Tree<E>::run() {
00054 test_make_FBHCID();
00055 test_make_ab_no();
00056 test_copyDeep();
00057 test_homomorfo();
00058 test_swap();
00059 test_mirror();
00060 test_changeChild();
00061 test_heightdepth();
00062 test_releaseChild();
00063 test_makeOrphan();
00064 test_left_right();
00065 test_no_swap();
00066 test_move_swap();
00067 test_multi_child();
00068 test_isLeft_isRight();
00069 test_isRoot_isLeaf();
00070 test_sizeStrong();
00071 test_AVL_tree();
00072 return wasSuccessful();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 Bin_Tree<char> make_FBHCID( std::ostream& COUT ) {
00088 Bin_Tree<char> T;
00089 orderedInsert( T, 'F' ); IPD ( COUT, T ); COUT << std::endl;
00090 orderedInsert( T, 'B' ); IPD ( COUT, T ); COUT << std::endl;
00091 orderedInsert( T, 'H' ); IPD ( COUT, T ); COUT << std::endl;
00092 orderedInsert( T, 'C' ); IPD ( COUT, T ); COUT << std::endl;
00093 orderedInsert( T, 'I' ); IPD ( COUT, T ); COUT << std::endl;
00094 orderedInsert( T, 'D' ); IPD ( COUT, T ); COUT << std::endl;
00095 return T;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 Bin_Tree<char> make_FBHCID() {
00110 Bin_Tree<char> T;
00111 orderedInsert( T, 'F' );
00112 orderedInsert( T, 'B' );
00113 orderedInsert( T, 'H' );
00114 orderedInsert( T, 'C' );
00115 orderedInsert( T, 'I' );
00116 orderedInsert( T, 'D' );
00117 return T;
00118 }
00119
00120
00121 template <class E>
00122 void test_Bin_Tree<E>::test_make_FBHCID() {
00123 Bin_Tree<char> T = make_FBHCID();
00124 assertTrue( T.size() == strlen( "FBHCID" ) );
00125
00126 assertTrue( T.root().data() == 'F' );
00127 assertTrue( T.root().left().data() == 'B' );
00128 assertTrue( T.root().left().right().data() == 'C' );
00129 assertTrue( T.root().left().right().right().data() == 'D' );
00130 assertTrue( T.root().right().data() == 'H' );
00131 assertTrue( T.root().right().right().data() == 'I' );
00132
00133 std::basic_ostringstream<char> ost;
00134 ost << T;
00135 assertTrue( ost.str() == "(F (B (C (D))) (H (I)))" );
00136 assertTrue( T.ok() );
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 Bin_Tree<char> make_ab_no() {
00154 Bin_Tree<char> T, T1, T2;
00155
00156 T1 = Bin_Tree<char>('b'); {
00157 T1.makeLeftChild( Bin_Tree<char>('f') );
00158 T1.makeRightChild( Bin_Tree<char>('h') );
00159 }
00160
00161 T2.changeRoot('k'); {
00162 T2.makeLeftChild( Bin_Tree<char>('l') );
00163 T.changeRoot('m'); {
00164 T.makeLeftChild( Bin_Tree<char>('n') );
00165 T.makeRightChild( Bin_Tree<char>('o') );
00166 }
00167 T2.makeRightChild( T );
00168 }
00169 T.erase();
00170 T.changeRoot('a'); {
00171 T.makeLeftChild( T1 );
00172 T.makeRightChild( Bin_Tree<char>('e') ); {
00173 T.right().makeLeftChild( Bin_Tree<char>('i') );
00174 T.right().makeRightChild( T2 );
00175 }
00176 }
00177 return T;
00178 }
00179
00180
00181
00182 template <class E>
00183 void test_Bin_Tree<E>::test_make_ab_no() {
00184 std::basic_ostringstream<char> ost;
00185 ost << make_ab_no();
00186 assertTrue( ost.str() == "(a (b (f) (h)) (e (i) (k (l) (m (n) (o)))))" );
00187 assertTrue( make_ab_no().ok() );
00188 }
00189
00190
00191 void toLower( Bin_Tree<char> T ) {
00192 if ( T.isEmpty() ) {
00193 return;
00194 }
00195 T.changeRoot( tolower( *T ) );
00196 toLower( T.left() );
00197 toLower( T.right() );
00198 }
00199
00200
00201 template <class E>
00202 void test_Bin_Tree<E>::test_copyDeep() {
00203 Bin_Tree<char> T, T1, T2;
00204 T.changeRoot( 'A' );
00205 T.makeLeftChild( Bin_Tree<char>( 'B' ) );
00206 T.makeRightChild( Bin_Tree<char>( 'C' ) );
00207 copyDeep( T1, T );
00208
00209 copyDeep( T2, T1 ); {
00210 assertTrue( ! T1.same( T2 ) );
00211 assertTrue( T1 == T2 );
00212 }
00213 T1 = T2; {
00214 assertTrue( T1.same( T2 ) );
00215 toLower( T2 );
00216 assertTrue( T1 == T2 );
00217 }
00218
00219 T1 = make_FBHCID(); {
00220 assertTrue( T1 != T2 );
00221 }
00222
00223 copyDeep( T1, T ); {
00224 assertTrue( ! T1.same( T2 ) );
00225 assertTrue( T1 != T2 );
00226 toLower( T1 );
00227 assertTrue( T1 == T2 );
00228 }
00229 }
00230
00231
00232 template <class E>
00233 void test_Bin_Tree<E>::test_homomorfo() {
00234 Bin_Tree<char> T1 = make_FBHCID();
00235 Bin_Tree<char> T2 = T1, Tmp;
00236 assertTrue( homomorfo( T1 , T2 ) );
00237 copyDeep( T2, T1 );
00238 toLower( T1 );
00239 assertTrue( TestCase::toString( T1 ) == "(f (b (c (d))) (h (i)))" );
00240 assertTrue( TestCase::toString( T2 ) == "(F (B (C (D))) (H (I)))" );
00241 T2.makeRightChild(T1);
00242 assertTrue( toString( T1 ) == "(f (b (c (d))) (h (i)))" );
00243 assertTrue( toString( T2 ) == "(F (B (C (D))) (f (b (c (d))) (h (i))))" );
00244 copyDeep( T1, T2 );
00245 assertTrue( toString( T1 ) == "(F (B (C (D))) (f (b (c (d))) (h (i))))" );
00246 assertTrue( toString( T2 ) == "(F (B (C (D))) (f (b (c (d))) (h (i))))" );
00247 assertTrue( homomorfo( T1 , T2 ) );
00248 assertTrue( homomorfo( T2 , T1 ) );
00249 mirror( T2.left() );
00250 mirror( T2.right() );
00251 assertTrue( homomorfo( T1 , T2 ) );
00252 assertTrue( homomorfo( T2 , T1 ) );
00253 }
00254
00255
00256 template <class E>
00257 void test_Bin_Tree<E>::test_swap() {
00258 Bin_Tree<char> T1 = make_FBHCID();
00259 Bin_Tree<char> T2 = 'A';
00260 assertTrue( T1.father().isEmpty() );
00261
00262 T2.swap(T1);
00263
00264 assertTrue( T1.size() == 1 ); {
00265 std::basic_ostringstream<char> ost;
00266 IPD(ost , T2);
00267 assertTrue( ost.str() == " B C D F H I" );
00268 }
00269 assertTrue( comp( T2.left().father(), T2 ) );
00270 assertTrue( comp( T2.right().father(), T2 ) );
00271 assertTrue( comp( T1.left(), Bin_Tree<char>() ) ) ;
00272 assertTrue( comp( T2.left().left(), Bin_Tree<char>() ) ) ;
00273 assertTrue( comp( T2.left().right().right(), Bin_Tree<char>('D') ) ) ;
00274 assertTrue( comp( T2.right().right(), Bin_Tree<char>('I') ) ) ;
00275 T1.changeRoot( 'H' );
00276 T1.makeRightChild( Bin_Tree<char>('I') );
00277 assertTrue( comp( T2.right(), T1) ) ;
00278 }
00279
00280
00281 template <class E>
00282 void test_Bin_Tree<E>::test_mirror() {
00283 Bin_Tree<char> T1 = make_FBHCID();
00284 assertTrue( T1.count() == 6 && height(T1) == 3 ); {
00285 std::basic_ostringstream<char> ost;
00286 IPD(ost , T1);
00287 assertTrue( ost.str() == " B C D F H I" );
00288 }
00289
00290 mirror( T1 );
00291
00292 assertTrue( T1.count() == 6 && height(T1) == 3 ); {
00293 std::basic_ostringstream<char> ost;
00294 IPD(ost , T1);
00295 assertTrue( ost.str() == " I H F D C B" );
00296 }
00297
00298 Bin_Tree<char> T2 = T1;
00299 assertTrue( comp(T1,T2) ); assertTrue( comp(T2,T1) );
00300
00301 copyDeep(T2,T1);
00302 char ch = T1.data();
00303 T1 = ch+1; assertTrue( ! comp(T1, T2) );
00304 T1 = ch; assertTrue( comp(T1, T2) );
00305
00306 copyDeep(T2,T1);
00307 ch = T1.data();
00308 T1 = ch+1; assertTrue( ! comp(T1, T2) );
00309 T1 = ch; assertTrue( comp(T1, T2) );
00310
00311 mirror( T1 );
00312 mirror( T2 );
00313
00314 assertTrue( comp(T1, T2) );
00315 copyDeep( T2, T1 );
00316 assertTrue( comp(T1, T2) );
00317
00318 assertTrue( T1.count() == 6 && height(T1) == 3 ); {
00319 std::basic_ostringstream<char> ost;
00320 IPD(ost , T1);
00321 assertTrue( ost.str() == " B C D F H I" );
00322 }
00323
00324 T2.left().changeRoot( 'X' );
00325 assertTrue( T2.count() == 6 && height(T2) == 3 ); {
00326 std::basic_ostringstream<char> ost;
00327 IPD(ost , T2);
00328 assertTrue( ost.str() == " X C D F H I" );
00329 }
00330
00331 T2.erase();
00332 assertTrue( !comp(T1, T2) );
00333 }
00334
00335
00336 template <class E>
00337 void test_Bin_Tree<E>::test_changeChild() {
00338 {{
00339 Bin_Tree<char> T, Tcopy;
00340 T = 'A';
00341 T.changeLeftChild( '0' );
00342 T.changeRightChild( '1' );
00343 copyDeep( Tcopy , T );
00344 assertTrue( TestCase::toString(T) == "(A (0) (1))" );
00345 mirror( T );
00346 assertTrue( TestCase::toString(T) == "(A (1) (0))" );
00347 mirror( T ); {
00348 std::basic_ostringstream<char> ost;
00349 ost << T;
00350 assertTrue( ost.str() == "(A (0) (1))" );
00351 }
00352
00353 assertTrue( comp(T, Tcopy) );
00354 }}
00355 {
00356 Bin_Tree<char> T2;
00357 T2 = 'A';
00358 T2.changeLeftChild('0'); {
00359 std::basic_ostringstream<char> ost;
00360 IPD (ost , T2);
00361 assertTrue( ost.str() == " 0 A" );
00362 }
00363
00364 mirror( T2 ); {
00365 std::basic_ostringstream<char> ost;
00366 IPD (ost , T2);
00367 assertTrue( ost.str() == " A 0" );
00368 }
00369 assertTrue( T2.count() == 2 );
00370 }
00371 }
00372
00373
00374 template <class E>
00375 void test_Bin_Tree<E>::test_releaseChild() {
00376 {{
00377 Bin_Tree<E> Paco = E();
00378 Bin_Tree<E> Lola = E();
00379 Bin_Tree<E> Bebe;
00380 Lola.makeLeftChild( E() );
00381 Bebe = Lola.left();
00382 Bebe.makeLeftChild( E() );
00383 Bebe.makeRightChild( E() );
00384 Paco.makeRightChild( Bebe );
00385
00386 Paco.releaseLeftChild();
00387 assertTrue( 4 == Paco.size() );
00388 Paco.releaseRightChild();
00389 assertTrue( 1 == Paco.size() );
00390 assertTrue( Paco.right() == Bin_Tree<E>() );
00391 assertTrue( Bebe != Bin_Tree<E>() );
00392 assertTrue( Bebe.father() == Paco );
00393
00394 assertTrue( 4 == Lola.size() );
00395 assertTrue( Bebe.same( Lola.left() ) );
00396 assertTrue( Lola.left().same( Bebe ) );
00397 assertTrue( Bebe.father() != Bin_Tree<E>() );
00398
00399 Lola.releaseLeftChild();
00400 assertTrue( ! Bebe.same( Lola.left() ) );
00401 assertTrue( 1 == Lola.size() );
00402 }}
00403 }
00404
00405
00406 template <class E>
00407 void test_Bin_Tree<E>::test_makeOrphan() {
00408 using namespace std;
00409 {{
00410 Bin_Tree<E> Paco = E();
00411 Bin_Tree<E> Lola = E();
00412 Bin_Tree<E> Bebe;
00413 Lola.makeLeftChild( E() );
00414 Bebe = Lola.left();
00415 Bebe.makeLeftChild( E() );
00416 Bebe.makeRightChild( E() );
00417 Paco.makeRightChild( Bebe );
00418
00419 assertTrue( Bebe.same( Lola.left() ) );
00420 assertTrue( Bebe.same( Paco.right() ) );
00421 assertTrue( Bebe.father() == Paco );
00422 assertTrue( Bebe.father() != Lola );
00423
00424 Bebe.makeOrphan();
00425 assertTrue( Bebe.father() != Paco );
00426 assertTrue( Bebe.father() == Bin_Tree<E>() );
00427
00428 assertTrue( Bebe.same( Lola.left() ) );
00429 assertTrue( Bebe.same( Paco.right() ) );
00430 assertTrue( Bebe.father() != Paco );
00431 assertTrue( Bebe.father() != Lola );
00432 }}
00433 }
00434
00435
00436 template <class E>
00437 void test_Bin_Tree<E>::test_left_right() {
00438 using namespace std;
00439 {{
00440 Bin_Tree<E> T = E();
00441 { T.left() = E();
00442
00443 }
00444 assertTrue( T.size() == 1 );
00445 assertTrue( T.left().isEmpty() );
00446
00447 T.makeRightChild( E() );
00448 T.right().erase();
00449 assertTrue( T.size() == 2 );
00450 assertTrue( ! T.right().isEmpty() );
00451
00452 {
00453 T.right() = Bin_Tree<E>();
00454 assertTrue( ! T.right().isEmpty() );
00455
00456 assertTrue( Bin_Tree<E>().isEmpty() );
00457
00458
00459 T.makeRightChild( Bin_Tree<E>() );
00460 assertTrue( T.right().isEmpty() );
00461 }
00462
00463 if ( T.right().isEmpty() ) {
00464 if (false) {
00465 *T.right() = E();
00466 }
00467 T.makeRightChild( E() );
00468 }
00469 else {
00470 T.right().changeRoot( E() );
00471 *( T.right() ) = E();
00472 }
00473 T = T.right();
00474 assertTrue ( ! T.father().isEmpty() );
00475 assertTrue( T.size() == 1 );
00476 T = T.father();
00477 assertTrue( T.size() == 2 );
00478 T.right().erase();
00479 assertTrue( T.size() == 2 );
00480
00481 Bin_Tree<E> Child = T.right();
00482 T.right().makeOrphan();
00483 assertTrue( T.right() == Child );
00484 assertTrue( Child.father() != T );
00485 assertTrue ( ! T.isEmpty() );
00486 assertTrue ( ! Child.isEmpty() );
00487 assertTrue ( Child.father().isEmpty() );
00488 }}
00489 }
00490
00491
00492 template <class E>
00493 void test_Bin_Tree<E>::test_no_swap() {
00494 {{
00495 Bin_Tree<E> T = E();
00496 Bin_Tree<E> Right;
00497 T.makeRightChild( E() );
00498
00499 assertTrue( T.size() == 2 );
00500 assertTrue( T.right().isRightChild() );
00501 Right = T.right();
00502 T.left().swap( Right );
00503 assertTrue( T.right().isRightChild() );
00504 assertTrue( T.size() == 2 );
00505
00506 T.makeLeftChild( T.right() );
00507 assertTrue( T.size() == 3 );
00508 assertTrue( T.left().same( T.right()) );
00509 assertTrue( T.right().isRightChild() );
00510 assertTrue( T.left().isLeftChild() );
00511
00512 T.makeLeftChild( T.right() );
00513 T.makeRightChild( Bin_Tree<E>() );
00514 assertTrue( T.size() == 2 );
00515 assertTrue( ! T.left().same( T.right()) );
00516 assertTrue( ! T.right().isRightChild() );
00517 assertTrue( T.left().isLeftChild() );
00518 }}
00519 {
00520 Bin_Tree<E> T = E();
00521 T.makeLeftChild( T );
00522 for ( int i=0; i<11; ++i ) {
00523 T = T.left();
00524 assertTrue( ! T.isEmpty() );
00525 }
00526 assertTrue( ! T.isEmpty() );
00527 }
00528 {
00529 Bin_Tree<E> T = E();
00530 T.makeRightChild( T );
00531 for ( int i=0; i<22; ++i ) {
00532 T = T.right();
00533 assertTrue( ! T.isEmpty() );
00534 }
00535 assertTrue( ! T.isEmpty() );
00536 }
00537 {
00538 Bin_Tree<E> T = E();
00539 T.makeRightChild( E() );
00540 T.right().makeRightChild( E() );
00541
00542 assertTrue( T.size() == 3 );
00543 T.right().right().makeRightChild( T );
00544 for ( int i=0; i<33; ++i ) {
00545 T = T.right();
00546 assertTrue( ! T.isEmpty() );
00547 }
00548 assertTrue( ! T.isEmpty() );
00549 }
00550 if ( false) {
00551
00552
00553 Bin_Tree<E> T = E();
00554 Bin_Tree<E> Right;
00555 T.makeRightChild( E() );
00556
00557 T.makeLeftChild( T.right() );
00558 assertTrue( T.size() == 2 );
00559 assertTrue( ! T.left().same( T.right()) );
00560 assertTrue( T.left().isLeftChild() );
00561 assertTrue( ! T.right().isRightChild() );
00562
00563 T.makeRightChild( T.left() );
00564 assertTrue( T.size() == 2 );
00565 assertTrue( ! T.right().same( T.left()) );
00566 assertTrue( T.right().isRightChild() );
00567 assertTrue( ! T.left().isLeftChild() );
00568
00569 assertTrue( T.size() == 2 );
00570 assertTrue( T.right().isRightChild() );
00571 Right = T.right();
00572 T.left().swap( Right );
00573 assertTrue( T.right().isRightChild() );
00574 assertTrue( T.size() == 2 );
00575 }
00576 }
00577
00578
00579 template <class E>
00580 void test_Bin_Tree<E>::test_multi_child() {
00581 {{
00582
00583
00584 Bin_Tree<E> Paco = E();
00585 Bin_Tree<E> Lola = E();
00586 Bin_Tree<E> Bebe;
00587 Lola.makeLeftChild( E() );
00588 Bebe = Lola.left();
00589 Bebe.makeLeftChild( E() );
00590 Bebe.makeRightChild( E() );
00591 Paco.makeRightChild( Bebe );
00592
00593
00594
00595
00596 assertTrue( 3 == Bebe.size() );
00597 assertTrue( 4 == Paco.size() );
00598 assertTrue( 4 == Lola.size() );
00599
00600 assertTrue( Paco.right().same( Bebe ) );
00601 assertTrue( Lola.left() .same( Bebe ) );
00602
00603 assertTrue( Bebe.father().same( Paco ) );
00604 assertTrue( ! Bebe.father().same( Lola ) );
00605
00606
00607 Bebe.erase();
00608 assertTrue( Bebe.isEmpty() );
00609
00610 Bebe = Lola.left();
00611 assertTrue( ! Bebe.isEmpty() );
00612 assertTrue( Bebe.father().same( Paco ) );
00613 assertTrue( ! Bebe.father().same( Lola ) );
00614
00615 if ( false ) {
00616 Bebe.left().makeRightChild( Paco );
00617 Bebe.right().makeLeftChild( Lola );
00618 }
00619 }}
00620 }
00621
00622
00623 template <class E>
00624 void test_Bin_Tree<E>::test_move_swap() {
00625 {{
00626 Bin_Tree<E> Paco = E(), Lola = E();
00627 Paco.makeLeftChild( E() );
00628 Paco.makeRightChild( E() );
00629
00630 assertTrue( Paco.size() == 3 );
00631 assertTrue( Lola.size() == 1 );
00632 Paco.swap( Lola );
00633 assertTrue( Paco.size() == 1 );
00634 assertTrue( Lola.size() == 3 );
00635
00636 Lola.move( Paco );
00637 assertTrue( Paco.size() == 0 );
00638 assertTrue( Lola.size() == 1 );
00639 }}
00640 }
00641
00642
00643 template <class E>
00644 void test_Bin_Tree<E>::test_isLeft_isRight() {
00645 {{
00646 Bin_Tree<E> T = E();
00647 T.makeLeftChild( E() );
00648 T.makeRightChild( E() );
00649
00650 assertTrue( ! T.isLeftChild() );
00651 assertTrue( T.left().isLeftChild() );
00652 assertTrue( T.right().isRightChild() );
00653 assertTrue( !T.left().left().isLeftChild() );
00654 assertTrue( !T.right().right().isRightChild() );
00655 }}
00656 }
00657
00658
00659 template <class E>
00660 void test_Bin_Tree<E>::test_isRoot_isLeaf() {
00661 {{
00662 Bin_Tree<E> T = E();
00663 T.makeLeftChild( E() );
00664 T.makeRightChild( E() );
00665
00666 assertTrue( T.isRoot() && ! T.isLeaf() );
00667 assertTrue( T.left().isLeaf() );
00668 assertTrue( T.right().isLeaf() );
00669 assertTrue( !T.left().left().isLeaf() );
00670 assertTrue( !T.right().right().isRoot() );
00671 }}
00672 }
00673
00674