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