Changeset 72
- Timestamp:
- Jan 7, 2010, 9:02:47 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
proiecte/Parallel-DT/R8/Src/trees.c
r26 r72 5 5 /* */ 6 6 /*************************************************************************/ 7 8 7 9 8 #include "defns.i" … … 11 10 #include "extern.i" 12 11 13 14 12 #define Tab "| " 15 13 #define TabSize 4 16 14 #define Width 80 /* approx max width of printed trees */ 17 15 18 /* If lines look like getting too long while a tree is being 19 printed, subtrees are broken off and printed separately after 20 the main tree is finished */ 21 22 short Subtree; /* highest subtree to be printed */ 23 Tree Subdef[100]; /* pointers to subtrees */ 24 25 FILE *TRf = 0, *fopen(); /* file pointer for tree i/o */ 26 char Fn[500]; /* file name */ 27 28 16 /* If lines look like getting too long while a tree is being 17 printed, subtrees are broken off and printed separately after 18 the main tree is finished */ 19 20 short Subtree; /* highest subtree to be printed */ 21 Tree Subdef[100]; /* pointers to subtrees */ 22 23 FILE *TRf = 0, *fopen(); /* file pointer for tree i/o */ 24 char Fn[500]; /* file name */ 29 25 30 26 /*************************************************************************/ … … 34 30 /*************************************************************************/ 35 31 36 37 PrintTree(T) 38 /* ---------- */ 39 Tree T; 40 { 41 short s; 42 43 Subtree=0; 44 printf("Decision Tree:\n"); 45 Show(T, 0); 46 printf("\n"); 47 48 ForEach(s, 1, Subtree) 49 { 50 printf("\n\nSubtree [S%d]\n", s); 51 Show(Subdef[s], 0); 32 PrintTree(T) 33 /* ---------- */ 34 Tree T; { 35 short s; 36 37 Subtree = 0; 38 printf("Decision Tree:\n"); 39 Show(T, 0); 52 40 printf("\n"); 53 } 54 printf("\n"); 55 } 56 57 41 42 ForEach(s, 1, Subtree) { 43 printf("\n\nSubtree [S%d]\n", s); 44 Show(Subdef[s], 0); 45 printf("\n"); 46 } 47 printf("\n"); 48 } 58 49 59 50 /*************************************************************************/ … … 63 54 /*************************************************************************/ 64 55 65 66 Show(T, Sh) 67 /* ---- */ 68 Tree T; 69 short Sh; 70 { 71 DiscrValue v, MaxV; 72 short MaxLine(); 73 74 if ( T->NodeType ) 75 { 76 /* See whether separate subtree needed */ 77 78 if ( T != Nil && Sh && Sh * TabSize + MaxLine(T) > Width ) 79 { 80 if ( Subtree < 99 ) 81 { 82 Subdef[++Subtree] = T; 83 printf("[S%d]", Subtree); 84 } 85 else 86 { 87 printf("[S??]"); 88 } 89 } 90 else 91 { 92 MaxV = T->Forks; 93 94 /* Print simple cases first */ 95 96 ForEach(v, 1, MaxV) 97 { 98 if ( ! T->Branch[v]->NodeType ) 99 { 100 ShowBranch(Sh, T, v); 101 } 102 } 103 104 /* Print subtrees */ 105 106 ForEach(v, 1, MaxV) 107 { 108 if ( T->Branch[v]->NodeType ) 109 { 110 ShowBranch(Sh, T, v); 111 } 112 } 113 } 114 } 115 else 116 { 117 printf(" %s (%.1f", ClassName[T->Leaf], T->Items); 118 if ( T->Errors > 0 ) printf("/%.1f", T->Errors); 119 printf(")"); 120 } 121 } 122 123 56 Show(T, Sh) 57 /* ---- */ 58 Tree T;short Sh; { 59 DiscrValue v, MaxV; 60 short MaxLine(); 61 62 if (T->NodeType) { 63 /* See whether separate subtree needed */ 64 65 if (T != Nil && Sh && Sh * TabSize + MaxLine(T) > Width) { 66 if (Subtree < 99) { 67 Subdef[++Subtree] = T; 68 printf("[S%d]", Subtree); 69 } else { 70 printf("[S??]"); 71 } 72 } else { 73 MaxV = T->Forks; 74 75 /* Print simple cases first */ 76 77 ForEach(v, 1, MaxV) { 78 if (!T->Branch[v]->NodeType) { 79 ShowBranch(Sh, T, v); 80 } 81 } 82 83 /* Print subtrees */ 84 85 ForEach(v, 1, MaxV) { 86 if (T->Branch[v]->NodeType) { 87 ShowBranch(Sh, T, v); 88 } 89 } 90 } 91 } else { 92 printf(" %s (%.1f", ClassName[T->Leaf], T->Items); 93 if (T->Errors > 0) 94 printf("/%.1f", T->Errors); 95 printf(")"); 96 } 97 } 124 98 125 99 /*************************************************************************/ … … 129 103 /*************************************************************************/ 130 104 131 132 ShowBranch(Sh, T, v) 133 /* ----------- */ 134 short Sh; 135 Tree T; 136 DiscrValue v; 137 { 138 DiscrValue Pv, Last; 139 Attribute Att; 140 Boolean FirstValue; 141 short TextWidth, Skip, Values=0, i; 142 143 Att = T->Tested; 144 145 switch ( T->NodeType ) 146 { 105 ShowBranch(Sh, T, v) 106 /* ----------- */ 107 short Sh;Tree T;DiscrValue v; { 108 DiscrValue Pv, Last; 109 Attribute Att; 110 Boolean FirstValue; 111 short TextWidth, Skip, Values = 0, i; 112 113 Att = T->Tested; 114 115 switch (T->NodeType) { 147 116 case BrDiscr: 148 117 149 150 151 152 118 Indent(Sh, Tab); 119 120 printf("%s = %s:", AttName[Att], AttValName[Att][v]); 121 break; 153 122 154 123 case ThreshContin: 155 124 156 Indent(Sh, Tab); 157 158 printf("%s %s %g ", 159 AttName[Att], ( v == 1 ? "<=" : ">" ), T->Cut); 160 161 if ( T->Lower != T->Upper ) 162 { 163 printf("[%g,%g]", T->Lower, T->Upper); 164 } 165 166 printf(":"); 167 break; 125 Indent(Sh, Tab); 126 127 printf("%s %s %g ", AttName[Att], (v == 1 ? "<=" : ">"), T->Cut); 128 129 if (T->Lower != T->Upper) { 130 printf("[%g,%g]", T->Lower, T->Upper); 131 } 132 133 printf(":"); 134 break; 168 135 169 136 case BrSubset: 170 137 171 /* Count values at this branch */ 172 173 ForEach(Pv, 1, MaxAttVal[Att]) 174 { 175 if ( In(Pv, T->Subset[v]) ) 176 { 177 Last = Pv; 178 Values++; 179 } 180 } 181 if ( ! Values ) return; 182 183 Indent(Sh, Tab); 184 185 if ( Values == 1 ) 186 { 187 printf("%s = %s:", AttName[Att], AttValName[Att][Last]); 188 break; 189 } 190 191 printf("%s in {", AttName[Att]); 192 FirstValue = true; 193 Skip = TextWidth = strlen(AttName[Att]) + 5; 194 195 ForEach(Pv, 1, MaxAttVal[Att]) 196 { 197 if ( In(Pv, T->Subset[v]) ) 198 { 199 if ( ! FirstValue && 200 TextWidth + strlen(AttValName[Att][Pv]) + 11 > Width ) 201 { 202 Indent(Sh, Tab); 203 ForEach(i, 1, Skip) putchar(' '); 204 205 TextWidth = Skip; 206 FirstValue = true; 207 } 208 209 printf("%s%c", AttValName[Att][Pv], Pv == Last ? '}' : ','); 210 TextWidth += strlen(AttValName[Att][Pv]) + 1; 211 FirstValue = false; 212 } 213 } 214 putchar(':'); 215 } 216 217 Show(T->Branch[v], Sh+1); 218 } 219 220 138 /* Count values at this branch */ 139 140 ForEach(Pv, 1, MaxAttVal[Att]) { 141 if (In(Pv, T->Subset[v])) { 142 Last = Pv; 143 Values++; 144 } 145 } 146 if (!Values) 147 return; 148 149 Indent(Sh, Tab); 150 151 if (Values == 1) { 152 printf("%s = %s:", AttName[Att], AttValName[Att][Last]); 153 break; 154 } 155 156 printf("%s in {", AttName[Att]); 157 FirstValue = true; 158 Skip = TextWidth = strlen(AttName[Att]) + 5; 159 160 ForEach(Pv, 1, MaxAttVal[Att]) { 161 if (In(Pv, T->Subset[v])) { 162 if (!FirstValue && TextWidth + strlen(AttValName[Att][Pv]) + 11 163 > Width) { 164 Indent(Sh, Tab); 165 ForEach(i, 1, Skip) 166 putchar(' '); 167 168 TextWidth = Skip; 169 FirstValue = true; 170 } 171 172 printf("%s%c", AttValName[Att][Pv], Pv == Last ? '}' : ','); 173 TextWidth += strlen(AttValName[Att][Pv]) + 1; 174 FirstValue = false; 175 } 176 } 177 putchar(':'); 178 } 179 180 Show(T->Branch[v], Sh + 1); 181 } 221 182 222 183 /*************************************************************************/ … … 229 190 /*************************************************************************/ 230 191 231 232 192 short MaxLine(St) 233 /* -------- */ 234 Tree St; 235 { 236 Attribute a; 237 DiscrValue v, MaxV, Next; 238 short Ll, MaxLl=0; 239 240 a = St->Tested; 241 242 MaxV = St->Forks; 243 ForEach(v, 1, MaxV) 244 { 245 Ll = ( St->NodeType == 2 ? 4 : strlen(AttValName[a][v]) ) + 1; 246 247 /* Find the appropriate branch */ 248 249 Next = v; 250 251 if ( ! St->Branch[Next]->NodeType ) 252 { 253 Ll += strlen(ClassName[St->Branch[Next]->Leaf]) + 6; 254 } 255 MaxLl = Max(MaxLl, Ll); 256 } 257 258 return strlen(AttName[a]) + 4 + MaxLl; 259 } 260 261 193 /* -------- */ 194 Tree St; { 195 Attribute a; 196 DiscrValue v, MaxV, Next; 197 short Ll, MaxLl = 0; 198 199 a = St->Tested; 200 201 MaxV = St->Forks; 202 ForEach(v, 1, MaxV) { 203 Ll = (St->NodeType == 2 ? 4 : strlen(AttValName[a][v])) + 1; 204 205 /* Find the appropriate branch */ 206 207 Next = v; 208 209 if (!St->Branch[Next]->NodeType) { 210 Ll += strlen(ClassName[St->Branch[Next]->Leaf]) + 6; 211 } 212 MaxLl = Max(MaxLl, Ll); 213 } 214 215 return strlen(AttName[a]) + 4 + MaxLl; 216 } 262 217 263 218 /*************************************************************************/ … … 267 222 /*************************************************************************/ 268 223 269 Indent(Sh, Mark) 270 /* ------ */ 271 short Sh; 272 char *Mark; 273 { 274 printf("\n"); 275 while ( Sh-- ) printf("%s", Mark); 276 } 277 278 224 Indent(Sh, Mark) 225 /* ------ */ 226 short Sh;char *Mark; { 227 printf("\n"); 228 while (Sh--) 229 printf("%s", Mark); 230 } 279 231 280 232 /*************************************************************************/ … … 284 236 /*************************************************************************/ 285 237 286 287 SaveTree(T, Extension) 288 /* --------- */ 289 Tree T; 290 String Extension; 291 { 292 static char *LastExt=""; 293 294 if ( strcmp(LastExt, Extension) ) 295 { 296 LastExt = Extension; 297 298 if ( TRf ) fclose(TRf); 299 300 strcpy(Fn, FileName); 301 strcat(Fn, Extension); 302 if ( ! ( TRf = fopen(Fn, "w") ) ) 303 Error(0, Fn, " for writing"); 304 } 305 306 putc('\n', TRf); 307 OutTree(T); 308 309 SaveDiscreteNames(); 310 } 311 312 238 SaveTree(T, Extension) 239 /* --------- */ 240 Tree T;String Extension; { 241 static char *LastExt = ""; 242 243 if (strcmp(LastExt, Extension)) { 244 LastExt = Extension; 245 246 if (TRf) 247 fclose(TRf); 248 249 strcpy(Fn, FileName); 250 strcat(Fn, Extension); 251 if (!(TRf = fopen(Fn, "w"))) 252 Error(0, Fn, " for writing"); 253 } 254 255 putc('\n', TRf); 256 OutTree(T); 257 258 SaveDiscreteNames(); 259 } 313 260 314 261 /*************************************************************************/ … … 318 265 /*************************************************************************/ 319 266 320 321 OutTree(T) 322 /* -------- */ 323 Tree T; 324 { 325 DiscrValue v; 326 int Bytes; 327 328 StreamOut((char *) &T->NodeType, sizeof(short)); 329 StreamOut((char *) &T->Leaf, sizeof(ClassNo)); 330 StreamOut((char *) &T->Items, sizeof(ItemCount)); 331 StreamOut((char *) &T->Errors, sizeof(ItemCount)); 332 StreamOut((char *) T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 333 334 if ( T->NodeType ) 335 { 336 StreamOut((char *) &T->Tested, sizeof(Attribute)); 337 StreamOut((char *) &T->Forks, sizeof(short)); 338 339 switch ( T->NodeType ) 340 { 341 case BrDiscr: 342 break; 343 344 case ThreshContin: 345 StreamOut((char *) &T->Cut, sizeof(float)); 346 StreamOut((char *) &T->Lower, sizeof(float)); 347 StreamOut((char *) &T->Upper, sizeof(float)); 348 break; 349 350 case BrSubset: 351 Bytes = (MaxAttVal[T->Tested]>>3) + 1; 352 ForEach(v, 1, T->Forks) 353 { 354 StreamOut((char *) T->Subset[v], Bytes); 355 } 356 break; 357 } 358 359 ForEach(v, 1, T->Forks) 360 { 361 OutTree(T->Branch[v]); 362 } 363 } 364 } 365 366 267 OutTree(T) 268 /* -------- */ 269 Tree T; { 270 DiscrValue v; 271 int Bytes; 272 273 StreamOut((char *) &T->NodeType, sizeof(short)); 274 StreamOut((char *) &T->Leaf, sizeof(ClassNo)); 275 StreamOut((char *) &T->Items, sizeof(ItemCount)); 276 StreamOut((char *) &T->Errors, sizeof(ItemCount)); 277 StreamOut((char *) T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 278 279 if (T->NodeType) { 280 StreamOut((char *) &T->Tested, sizeof(Attribute)); 281 StreamOut((char *) &T->Forks, sizeof(short)); 282 283 switch (T->NodeType) { 284 case BrDiscr: 285 break; 286 287 case ThreshContin: 288 StreamOut((char *) &T->Cut, sizeof(float)); 289 StreamOut((char *) &T->Lower, sizeof(float)); 290 StreamOut((char *) &T->Upper, sizeof(float)); 291 break; 292 293 case BrSubset: 294 Bytes = (MaxAttVal[T->Tested] >> 3) + 1; 295 ForEach(v, 1, T->Forks) { 296 StreamOut((char *) T->Subset[v], Bytes); 297 } 298 break; 299 } 300 301 ForEach(v, 1, T->Forks) { 302 OutTree(T->Branch[v]); 303 } 304 } 305 } 367 306 368 307 /*************************************************************************/ … … 372 311 /*************************************************************************/ 373 312 374 375 313 Tree GetTree(Extension) 376 /* -------- */ 377 String Extension; 378 { 379 Tree Hold, InTree(); 380 static char *LastExt=""; 381 382 if ( strcmp(LastExt, Extension) ) 383 { 384 LastExt = Extension; 385 386 if ( TRf ) fclose(TRf); 387 388 strcpy(Fn, FileName); 389 strcat(Fn, Extension); 390 if ( ! ( TRf = fopen(Fn, "r") ) ) Error(0, Fn, ""); 391 } 392 393 if ( ! TRf || getc(TRf) == EOF ) return Nil; 394 395 Hold = InTree(); 396 397 RecoverDiscreteNames(); 398 399 return Hold; 400 } 401 402 314 /* -------- */ 315 String Extension; { 316 Tree Hold, InTree(); 317 static char *LastExt = ""; 318 319 if (strcmp(LastExt, Extension)) { 320 LastExt = Extension; 321 322 if (TRf) 323 fclose(TRf); 324 325 strcpy(Fn, FileName); 326 strcat(Fn, Extension); 327 if (!(TRf = fopen(Fn, "r"))) 328 Error(0, Fn, ""); 329 } 330 331 if (!TRf || getc(TRf) == EOF) 332 return Nil; 333 334 Hold = InTree(); 335 336 RecoverDiscreteNames(); 337 338 return Hold; 339 } 403 340 404 341 /*************************************************************************/ … … 407 344 /* */ 408 345 /*************************************************************************/ 409 410 346 411 347 Tree InTree() 412 348 /* ------- */ 413 349 { 414 Tree T; 415 DiscrValue v; 416 int Bytes; 417 418 T = (Tree) malloc(sizeof(TreeRec)); 419 420 StreamIn((char *) &T->NodeType, sizeof(short)); 421 StreamIn((char *) &T->Leaf, sizeof(ClassNo)); 422 StreamIn((char *) &T->Items, sizeof(ItemCount)); 423 StreamIn((char *) &T->Errors, sizeof(ItemCount)); 424 425 T->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount)); 426 StreamIn((char *) T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 427 428 if ( T->NodeType ) 429 { 430 StreamIn((char *) &T->Tested, sizeof(Attribute)); 431 StreamIn((char *) &T->Forks, sizeof(short)); 432 433 switch ( T->NodeType ) 434 { 435 case BrDiscr: 436 break; 437 438 case ThreshContin: 439 StreamIn((char *) &T->Cut, sizeof(float)); 440 StreamIn((char *) &T->Lower, sizeof(float)); 441 StreamIn((char *) &T->Upper, sizeof(float)); 442 break; 443 444 case BrSubset: 445 T->Subset = (Set *) calloc(T->Forks + 1, sizeof(Set)); 446 447 Bytes = (MaxAttVal[T->Tested]>>3) + 1; 448 ForEach(v, 1, T->Forks) 449 { 450 T->Subset[v] = (Set) malloc(Bytes); 451 StreamIn((char *) T->Subset[v], Bytes); 452 } 453 } 454 455 T->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree)); 456 ForEach(v, 1, T->Forks) 457 { 458 T->Branch[v] = InTree(); 459 } 460 } 461 462 return T; 463 } 464 465 350 Tree T; 351 DiscrValue v; 352 int Bytes; 353 354 T = (Tree) malloc(sizeof(TreeRec)); 355 356 StreamIn((char *) &T->NodeType, sizeof(short)); 357 StreamIn((char *) &T->Leaf, sizeof(ClassNo)); 358 StreamIn((char *) &T->Items, sizeof(ItemCount)); 359 StreamIn((char *) &T->Errors, sizeof(ItemCount)); 360 361 T->ClassDist = (ItemCount *) calloc(MaxClass + 1, sizeof(ItemCount)); 362 StreamIn((char *) T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 363 364 if (T->NodeType) { 365 StreamIn((char *) &T->Tested, sizeof(Attribute)); 366 StreamIn((char *) &T->Forks, sizeof(short)); 367 368 switch (T->NodeType) { 369 case BrDiscr: 370 break; 371 372 case ThreshContin: 373 StreamIn((char *) &T->Cut, sizeof(float)); 374 StreamIn((char *) &T->Lower, sizeof(float)); 375 StreamIn((char *) &T->Upper, sizeof(float)); 376 break; 377 378 case BrSubset: 379 T->Subset = (Set *) calloc(T->Forks + 1, sizeof(Set)); 380 381 Bytes = (MaxAttVal[T->Tested] >> 3) + 1; 382 ForEach(v, 1, T->Forks) { 383 T->Subset[v] = (Set) malloc(Bytes); 384 StreamIn((char *) T->Subset[v], Bytes); 385 } 386 } 387 388 T->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree)); 389 ForEach(v, 1, T->Forks) { 390 T->Branch[v] = InTree(); 391 } 392 } 393 394 return T; 395 } 466 396 467 397 /*************************************************************************/ … … 471 401 /*************************************************************************/ 472 402 473 474 StreamOut(s, n) 475 /* --------- */ 476 String s; 477 int n; 478 { 479 while ( n-- ) putc(*s++, TRf); 480 } 481 482 483 484 StreamIn(s, n) 485 /* --------- */ 486 String s; 487 int n; 488 { 489 while ( n-- ) *s++ = getc(TRf); 490 } 491 492 403 StreamOut(s, n) 404 /* --------- */ 405 String s;int n; { 406 while (n--) 407 putc(*s++, TRf); 408 } 409 410 StreamIn(s, n) 411 /* --------- */ 412 String s;int n; { 413 while (n--) 414 *s++ = getc(TRf); 415 } 493 416 494 417 /*************************************************************************/ … … 498 421 /*************************************************************************/ 499 422 500 501 ReleaseTree(Node) 502 /* ------- */ 503 Tree Node; 504 { 505 DiscrValue v; 506 507 if ( Node->NodeType ) 508 { 509 ForEach(v, 1, Node->Forks) 510 { 511 ReleaseTree(Node->Branch[v]); 512 } 513 514 cfree(Node->Branch); 515 516 if ( Node->NodeType == BrSubset ) 517 { 518 cfree(Node->Subset); 519 } 520 521 } 522 523 cfree(Node->ClassDist); 524 cfree(Node); 525 } 526 527 423 ReleaseTree(Node) 424 /* ------- */ 425 Tree Node; { 426 DiscrValue v; 427 428 if (Node->NodeType) { 429 ForEach(v, 1, Node->Forks) { 430 ReleaseTree(Node->Branch[v]); 431 } 432 433 cfree(Node->Branch); 434 435 if (Node->NodeType == BrSubset) { 436 cfree(Node->Subset); 437 } 438 439 } 440 441 cfree(Node->ClassDist); 442 cfree(Node); 443 } 528 444 529 445 /*************************************************************************/ … … 533 449 /*************************************************************************/ 534 450 535 536 451 Tree Leaf(ClassFreq, NodeClass, Cases, Errors) 537 /* ---- */ 538 ItemCount *ClassFreq; 539 ClassNo NodeClass; 540 ItemCount Cases, Errors; 541 { 542 Tree Node; 543 544 Node = (Tree) calloc(1, sizeof(TreeRec)); 545 546 Node->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount)); 547 memcpy(Node->ClassDist, ClassFreq, (MaxClass+1) * sizeof(ItemCount)); 548 549 Node->NodeType = 0; 550 Node->Leaf = NodeClass; 551 Node->Items = Cases; 552 Node->Errors = Errors; 553 554 return Node; 555 } 556 557 452 /* ---- */ 453 ItemCount *ClassFreq;ClassNo NodeClass;ItemCount Cases, Errors; { 454 Tree Node; 455 456 Node = (Tree) calloc(1, sizeof(TreeRec)); 457 458 Node->ClassDist = (ItemCount *) calloc(MaxClass + 1, sizeof(ItemCount)); 459 memcpy(Node->ClassDist, ClassFreq, (MaxClass + 1) * sizeof(ItemCount)); 460 461 Node->NodeType = 0; 462 Node->Leaf = NodeClass; 463 Node->Items = Cases; 464 Node->Errors = Errors; 465 466 return Node; 467 } 558 468 559 469 /*************************************************************************/ … … 563 473 /*************************************************************************/ 564 474 565 566 Sprout(Node, Branches) 567 /* ------ */ 568 Tree Node; 569 DiscrValue Branches; 570 { 571 Node->Forks = Branches; 572 573 Node->Branch = (Tree *) calloc(Branches+1, sizeof(Tree)); 574 } 575 576 475 Sprout(Node, Branches) 476 /* ------ */ 477 Tree Node;DiscrValue Branches; { 478 Node->Forks = Branches; 479 480 Node->Branch = (Tree *) calloc(Branches + 1, sizeof(Tree)); 481 } 577 482 578 483 /*************************************************************************/ … … 582 487 /*************************************************************************/ 583 488 584 585 TreeSize(Node) 586 /* -------- */ 587 Tree Node; 588 { 589 int Sum=0; 590 DiscrValue v; 591 592 if ( Node->NodeType ) 593 { 594 ForEach(v, 1, Node->Forks) 595 { 596 Sum += TreeSize(Node->Branch[v]); 597 } 598 } 599 600 return Sum + 1; 601 } 602 603 489 TreeSize(Node) 490 /* -------- */ 491 Tree Node; { 492 int Sum = 0; 493 DiscrValue v; 494 495 if (Node->NodeType) { 496 ForEach(v, 1, Node->Forks) { 497 Sum += TreeSize(Node->Branch[v]); 498 } 499 } 500 501 return Sum + 1; 502 } 604 503 605 504 /*************************************************************************/ … … 609 508 /*************************************************************************/ 610 509 611 612 510 Tree CopyTree(T) 613 /* --------- */ 614 Tree T; 615 { 616 DiscrValue v; 617 Tree New; 618 619 New = (Tree) malloc(sizeof(TreeRec)); 620 memcpy(New, T, sizeof(TreeRec)); 621 622 New->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount)); 623 memcpy(New->ClassDist, T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 624 625 if ( T->NodeType ) 626 { 627 New->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree)); 628 ForEach(v, 1, T->Forks) 629 { 630 New->Branch[v] = CopyTree(T->Branch[v]); 631 } 632 } 633 634 return New; 635 } 636 637 511 /* --------- */ 512 Tree T; { 513 DiscrValue v; 514 Tree New; 515 516 New = (Tree) malloc(sizeof(TreeRec)); 517 memcpy(New, T, sizeof(TreeRec)); 518 519 New->ClassDist = (ItemCount *) calloc(MaxClass + 1, sizeof(ItemCount)); 520 memcpy(New->ClassDist, T->ClassDist, (MaxClass + 1) * sizeof(ItemCount)); 521 522 if (T->NodeType) { 523 New->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree)); 524 ForEach(v, 1, T->Forks) { 525 New->Branch[v] = CopyTree(T->Branch[v]); 526 } 527 } 528 529 return New; 530 } 638 531 639 532 /*************************************************************************/ … … 643 536 /*************************************************************************/ 644 537 645 646 SaveDiscreteNames() 538 SaveDiscreteNames() 647 539 /* ----------------- */ 648 540 { 649 Attribute Att; 650 DiscrValue v; 651 int Length; 652 653 ForEach(Att, 0, MaxAtt) 654 { 655 if ( SpecialStatus[Att] != DISCRETE ) continue; 656 657 StreamOut((char *) &MaxAttVal[Att], sizeof(int)); 658 659 ForEach(v, 1, MaxAttVal[Att]) 660 { 661 Length = strlen(AttValName[Att][v]) + 1; 662 663 StreamOut((char *) &Length, sizeof(int)); 664 StreamOut((char *) AttValName[Att][v], Length); 665 } 666 } 667 } 668 669 541 Attribute Att; 542 DiscrValue v; 543 int Length; 544 545 ForEach(Att, 0, MaxAtt) { 546 if (SpecialStatus[Att] != DISCRETE) 547 continue; 548 549 StreamOut((char *) &MaxAttVal[Att], sizeof(int)); 550 551 ForEach(v, 1, MaxAttVal[Att]) { 552 Length = strlen(AttValName[Att][v]) + 1; 553 554 StreamOut((char *) &Length, sizeof(int)); 555 StreamOut((char *) AttValName[Att][v], Length); 556 } 557 } 558 } 670 559 671 560 /*************************************************************************/ … … 675 564 /*************************************************************************/ 676 565 677 678 RecoverDiscreteNames() 566 RecoverDiscreteNames() 679 567 /* -------------------- */ 680 568 { 681 Attribute Att; 682 DiscrValue v; 683 int Length; 684 685 ForEach(Att, 0, MaxAtt) 686 { 687 if ( SpecialStatus[Att] != DISCRETE ) continue; 688 689 StreamIn(&MaxAttVal[Att], sizeof(int)); 690 691 ForEach(v, 1, MaxAttVal[Att]) 692 { 693 StreamIn(&Length, sizeof(int)); 694 695 AttValName[Att][v] = (char *) malloc(Length); 696 StreamIn(AttValName[Att][v], Length); 697 } 698 } 699 } 569 Attribute Att; 570 DiscrValue v; 571 int Length; 572 573 ForEach(Att, 0, MaxAtt) { 574 if (SpecialStatus[Att] != DISCRETE) 575 continue; 576 577 StreamIn(&MaxAttVal[Att], sizeof(int)); 578 579 ForEach(v, 1, MaxAttVal[Att]) { 580 StreamIn(&Length, sizeof(int)); 581 582 AttValName[Att][v] = (char *) malloc(Length); 583 StreamIn(AttValName[Att][v], Length); 584 } 585 } 586 }
Note: See TracChangeset
for help on using the changeset viewer.