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