#include "header.i" void Show(Tree T, int Sh); /***********************/ /* Print header for T2 */ /***********************/ #define RELEASE "1.0" void PrintHeader() /* ----------- */ { char *ctime(), TitleLine[80]; long clock, time(); int Underline; clock = time(0); sprintf(TitleLine, "T2 [release %s] %s", RELEASE, ""); printf("\n%s\t%s", TitleLine, ctime(&clock)); Underline = strlen(TitleLine); while ( Underline-- ) putchar('-'); putchar('\n'); } /*************************************************************************/ /* Display entire decision tree T */ /*************************************************************************/ void PrintTree(Tree T) /* --------- */ { printf("\nDecision Tree:\n"); Show(T, 0); printf("\n"); } /*************************************************************************/ /* Print report of errors */ /*************************************************************************/ void Evaluate(Tree Root) /* -------- */ { ItemNo i; ItemCount RawErrors; int TreeSize(Tree); printf("\n"); printf("\tSize of Tree Errors\n\n"); RawErrors = 0; ForEach(i, 0, MaxItem) { if ( Classify(Item[i], Root) != Class(Item[i]) ) RawErrors += Weight(Item[i]); } printf("\t%12d %5.1f(%4.1f%%)\n", TreeSize(Root), RawErrors, 100.0*RawErrors / (MaxItem+1.0)); } /*************************************************************************/ /* Display the tree T with offset Sh */ /*************************************************************************/ #define Tab "| " void ShowBranch(int Sh, Tree T, DiscrValue v); void Show(T, Sh) /* ---- */ Tree T; int Sh; { DiscrValue v; if ( T->NodeType ) { ForEach(v, 0, T->Forks) ShowBranch(Sh, T, v); } else { printf(" %s (%.1f", ClassName[T->BestClass], T->Freq); if ( T->Error > 0 ) printf("/%.1f", T->Error); printf(")"); } } /*************************************************************************/ /* Print a node T with offset Sh, branch value v, and continue */ /*************************************************************************/ void Indent(int Sh, char *Mark); void ShowBranch(Sh, T, v) /* ---------- */ int Sh; Tree T; DiscrValue v; { Attribute Att; Att = T->Tested; if( v == 0 ) { Indent(Sh, Tab); printf("%s = %s:", AttName[Att], "Unknown"); } else { switch ( T->NodeType ) { case BrDiscr: Indent(Sh, Tab); printf("%s = %s:", AttName[Att], AttValName[Att][v]); break; case BrIntervals: Indent(Sh, Tab); if( v == 1 && T->Forks > 1 ) { printf("%s in (-infty,%g) ", AttName[Att], T->Cut[v]); } else if( v == T->Forks && T->Forks > 1 ) { printf("%s in [%g,+infty) ", AttName[Att], T->Cut[v-1]); } else if( v == 1 && T->Forks == 1 ) { printf("%s in (-infty,+infty) ", AttName[Att]); } else { printf("%s in [%g,%g) ", AttName[Att], T->Cut[v-1], T->Cut[v]); } printf(":"); break; } } Show(T->Branch[v], Sh+1); } /*************************************************************************/ /* Indent Sh columns */ /*************************************************************************/ void Indent(Sh, Mark) /* ------ */ int Sh; char *Mark; { printf("\n"); while ( Sh-- ) printf("%s", Mark); } /*************************************************************************/ /* Count the nodes in a tree */ /*************************************************************************/ int TreeSize(Tree Node) /* -------- */ { int Sum=0; DiscrValue v; if ( Node->NodeType != 0) { ForEach(v, 0, Node->Forks) { Sum += TreeSize(Node->Branch[v]); } } return Sum + 1; }