00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef Tree_Ex_h
00013 #define Tree_Ex_h
00014
00015 #include "Tree_L.h"
00016
00017 #include <iostream>
00018 #include <iomanip>
00019 #include <cassert>
00020 #include <string>
00021
00022
00023 namespace std {}
00024
00025 using namespace std;
00026
00027
00028
00029
00030
00031
00032
00033 template <class E>
00034 void My_Count0(const Tree<E>& T, unsigned & n) {
00035 for (unsigned i=T.Leftmost_N(); i <= T.Rightmost_N(); ++i) {
00036 if (! T.Child(i).Empty() ) {
00037 ++n;
00038 My_Count0( T.Child(i), n );
00039 }
00040 }
00041 }
00042
00043
00044 template <class E>
00045 unsigned My_Count(const Tree<E>& T) {
00046 if (T.Empty()) {
00047 return 0;
00048 } else {
00049 unsigned n = 1;
00050 My_Count0(T, n);
00051 return n;
00052 }
00053 }
00054
00055
00056 template <class E>
00057 unsigned Count_Children ( const Tree<E> & T ) {
00058 Tree<E> L_Child = T.Leftmost();
00059 if ( L_Child.Empty() ) {
00060 return 0;
00061 }
00062
00063 unsigned n = 1;
00064 Tree<E> R_Child = T.Rightmost();
00065 Tree<E>& Child = L_Child;
00066 for (;;) {
00067 if (Child == R_Child) {
00068 break;
00069 } else {
00070 ++n;
00071 }
00072 Child = Child.Right_Sibling();
00073 }
00074 return n;
00075 }
00076
00077 template <class E>
00078 bool Comp( const Tree<E>& p, const Tree<E>& q ) {
00079 if ( p.Same(q) ) {
00080 return true;
00081 }
00082 if ( p.Empty() || q.Empty() ) {
00083 return false;
00084 }
00085 if ( p.Child_Number() != q.Child_Number() ) {
00086 return false;
00087 }
00088 if ( ! (p.Data() == q.Data()) ) {
00089 return false;
00090 }
00091
00092
00093 unsigned rp = p.Rightmost_N(), lp = p.Leftmost_N();
00094 unsigned rq = q.Rightmost_N(), lq = q.Leftmost_N();
00095 if ((lp != lq) || (rp != rq)) {
00096 return false;
00097 }
00098 for (unsigned i=lp; i<=rp ; ++i) {
00099 if (! Comp(p.Child(i), q.Child(i)) ) {
00100 return false;
00101 }
00102 }
00103
00104 return true;
00105 }
00106
00107
00108
00109
00110
00111 template <class E>
00112 void Print( const Tree<E>& T, unsigned indent=0, unsigned num=0 ) {
00113 if ( T.Empty() ) {
00114 return;
00115 }
00116
00117 cout << endl;
00118 for (unsigned i = 0; i < indent; ++i) {
00119 cout << ' ' << ' ';
00120 }
00121
00122 cout << '[' << setfill('0') << setw(2) << num << "]==> " << *T;
00123
00124 Tree<E> Child = T.Leftmost();
00125 while ( ! Child.Empty() ) {
00126 Print( Child, indent+1, Child.Child_Number() );
00127 Child = Child.Right_Sibling();
00128 }
00129 }
00130
00131
00132
00133 string DOS_string(string& s) {
00134
00135 if (s == "¦ ") { return "À "; }
00136 else if (s == "+--") { return "ÃÄÄ"; }
00137 else if (s == "+--") { return "ÀÄÄ"; }
00138 else return s;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 template <class E>
00165 void Ztree_Print( const Tree<E> & T, string s="", unsigned level = 0, bool last_child = true) {
00166 if ( T.Empty() ) {
00167 return;
00168 }
00169
00170 cout << s << T.Data() << endl;
00171
00172 Tree<E> L_Child = T.Leftmost();
00173 if ( L_Child.Empty() ) {
00174 return;
00175 }
00176 if (level > 0) {
00177 if (last_child) {
00178 s.resize ( s.size () - 3 );
00179 s += " ";
00180 } else {
00181 s.resize ( s.size () - 3 );
00182 s += "| ";
00183 }
00184 }
00185
00186 s += "| ";
00187 Tree<E> R_Child = T.Rightmost();
00188 Tree<E>& Child = L_Child;
00189 for (;;) {
00190 if (Child == R_Child) {
00191 s.resize ( s.size () - 3 );
00192 s += "+--";
00193 Ztree_Print(Child, s, level+1, true);
00194 s.resize ( s.size () - 3 );
00195 s += " ";
00196 break;
00197 } else {
00198 s.resize ( s.size () - 3 );
00199 s += "|--";
00200 Ztree_Print(Child, s, level+1, false);
00201 s.resize ( s.size () - 3 );
00202 s += " ";
00203 }
00204 Child = Child.Right_Sibling();
00205 }
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 template <class E>
00225 void Mirror( Tree<E>& T ) {
00226 if ( T.Empty() ) {
00227 return;
00228 }
00229
00230 Tree<E> Child = T.Leftmost();
00231 while ( ! Child.Empty() ) {
00232 Mirror( Child );
00233 Child = Child.Right_Sibling();
00234 }
00235
00236 unsigned N = T.Rightmost_N();
00237 Tree<E> Ti, Tn_i;
00238 unsigned i, Mid = (N+1) / 2;
00239 for (i=0; i<Mid; ++i) {
00240 #if (0)
00241 {
00242 cout << endl; Print(T);
00243 Tree<E> Ti = T.Child( i );
00244 Tree<E> Tn_i = T.Child( N - i );
00245 cout << endl;
00246 string sTi ="[]"; string sTn_i = "[]";
00247 if (!Ti.Empty()) sTi=*Ti;
00248 if (!Tn_i.Empty()) sTn_i=*Tn_i; unsigned DT = 2+Depth(T);
00249 for (unsigned j=0; j<DT; ++j) { cout << " "; }
00250 cout << "[\"" << *T << "\"].Graft(" << i << ",\"" << sTn_i << "\")" << endl;
00251 for (unsigned j=0; j<DT; ++j) { cout << " "; }
00252 cout << "[\"" << *T << "\"].Graft(" << N-i << ",\"" << sTi << "\")" << endl;
00253 }
00254 #endif
00255 Ti = T.Child( i );
00256 Tn_i = T.Child( N - i );
00257 T.Graft( i, Tn_i );
00258 T.Graft( N - i, Ti );
00259 }
00260
00261 }
00262
00263
00264
00265
00266 template <class E>
00267 bool Ancestor( const Tree<E> & Child, const Tree<E> & Father ) {
00268 if ( Child.Empty() ) {
00269 return false;
00270 }
00271 Tree<E> T = Child.Father();
00272 while ( ! T.Empty() ) {
00273 if ( T.Same( Father ) ) {
00274 return true;
00275 }
00276 T = T.Father();
00277 }
00278 return false;
00279 }
00280
00281
00282
00283
00284 template <class E>
00285 inline bool Is_Leaf( const Tree<E> & T ) {
00286 return ( ! T.Empty() && T.Leftmost().Empty() );
00287 }
00288
00289
00290
00291
00292 template <class E>
00293 inline bool Is_Root( const Tree<E> & T ) {
00294 return ( !T.Empty() && !T.Father().Empty() );
00295 }
00296
00297
00298
00299
00300
00301 template <class E>
00302 void Height0( const Tree<E> & T, unsigned &max, unsigned actual) {
00303 if ( T.Empty() ) {
00304 if (max < actual) {
00305 max = actual;
00306 }
00307 return;
00308 } else {
00309 for (unsigned i=T.Leftmost_N(); i <= T.Rightmost_N(); ++i) {
00310 Height0(T.Child(i), max, actual+1);
00311 }
00312 return;
00313 }
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 template <class E>
00341 inline unsigned Height( const Tree<E> & T ) {
00342 unsigned actual, max;
00343 max = 0;
00344 actual = 0;
00345 Height0( T, max, actual );
00346 max = ( max > 0 ? max-1 : 0 );
00347 return max;
00348 }
00349
00350
00351
00352
00353
00354 template <class E>
00355 unsigned Depth( const Tree<E> & T ) {
00356 if ( T.Empty() ) {
00357 return 0;
00358 }
00359 unsigned n = 0;
00360 Tree<E> F = T;
00361 do {
00362 F = F.Father();
00363 ++n;
00364 } while ( ! F.Empty() );
00365 return n-1;
00366 }
00367
00368
00369 template <class E>
00370 unsigned Arity( Tree<E> & T ) {
00371 if ( T.Empty() ) {
00372 return 0;
00373 }
00374 unsigned max = 0, n_children = 0;
00375 Tree<E> Child = T.Leftmost();
00376 while ( ! Child.Empty() ) {
00377 unsigned n = Arity( Child );
00378 max = max > n ? max : n;
00379 n_children++;
00380 Child = Child.Right_Sibling();
00381 }
00382 return max > n_children ? max : n_children;
00383 }
00384
00385
00386 template <class E>
00387 bool Is_Binary( const Tree<E> & T ) {
00388 return Arity(T) <= 2;
00389 }
00390
00391 #include <climits>
00392
00393
00394
00395
00396
00397 template <class E>
00398 unsigned Length( const Tree<E> &n1, Tree<E> &n2 ) {
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 bool Funcion_NO_implementada = false;
00423 assert( Funcion_NO_implementada );
00424 return UINT_MAX;
00425 }
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 template <class E>
00450 void New_Root( Tree<E>& T, Tree<E> &sT ) {
00451 bool Funcion_NO_implementada = false;
00452 assert( Funcion_NO_implementada );
00453 return;
00454
00455 Tree<E> F = T.Father();
00456 if ( F.Empty() ) {
00457 return;
00458 }
00459 unsigned n = sT.Child_Number();
00460 if (n == 0) {
00461 return;
00462 }
00463 Tree<E> saveS = sT;
00464 Tree<E> newT = T;
00465 F.Graft(n, Tree<E>( ) );
00466 sT.Graft(n-1, newT);
00467 T.Move( sT );
00468 sT = saveS;
00469 }
00470
00471 #endif // Tree_Ex_h
00472
00473