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