⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trees.c

📁 一个简洁好用的SVM代码
💻 C
📖 第 1 页 / 共 2 页
字号:
		Bytes = (MaxAttVal[T->Tested]>>3) + 1;

		ForEach(v, 1, T->Forks)

		{

		    StreamOut((char *) T->Subset[v], Bytes);

		}

		break;

	}



	ForEach(v, 1, T->Forks)

	{

	    OutTree(T->Branch[v]);

	}

    }

}







/*************************************************************************/

/*									 */

/*	Retrieve entire decision tree with extension Extension		 */

/*									 */

/*************************************************************************/





Tree GetTree(Extension)

/*   --------  */

    String Extension;

{

    Tree Hold, InTree();

    static char *LastExt="";



    if ( strcmp(LastExt, Extension) )

    {

	LastExt = Extension;



	if ( TRf ) fclose(TRf);



	strcpy(Fn, FileName);

	strcat(Fn, Extension);

	if ( ! ( TRf = fopen(Fn, "r") ) ) Error(0, Fn, "");

    }



    if ( ! TRf || getc(TRf) == EOF ) return Nil;



    Hold = InTree();



    RecoverDiscreteNames();



    return Hold;

}







/*************************************************************************/

/*									 */

/*	Retrieve tree from saved characters				 */

/*									 */

/*************************************************************************/





Tree InTree()

/*   -------  */

{

    Tree T;

    DiscrValue v;

    int Bytes;



    T = (Tree) malloc(sizeof(TreeRec));



    StreamIn((char *) &T->NodeType, sizeof(short));

    StreamIn((char *) &T->Leaf, sizeof(ClassNo));

    StreamIn((char *) &T->Items, sizeof(ItemCount));

    StreamIn((char *) &T->Errors, sizeof(ItemCount));



    T->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount));

    StreamIn((char *) T->ClassDist, (MaxClass + 1) * sizeof(ItemCount));



    if ( T->NodeType )

    {

	StreamIn((char *) &T->Tested, sizeof(Attribute));

	StreamIn((char *) &T->Forks, sizeof(short));



	switch ( T->NodeType )

	{

	    case BrDiscr:

		break;



	    case ThreshContin:

		StreamIn((char *) &T->Cut, sizeof(float));

		StreamIn((char *) &T->Lower, sizeof(float));

		StreamIn((char *) &T->Upper, sizeof(float));

		break;



	    case BrSubset:

		T->Subset = (Set *) calloc(T->Forks + 1, sizeof(Set));



		Bytes = (MaxAttVal[T->Tested]>>3) + 1;

		ForEach(v, 1, T->Forks)

		{

		    T->Subset[v] = (Set) malloc(Bytes);

		    StreamIn((char *) T->Subset[v], Bytes);

		}

	}



	T->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree));

	ForEach(v, 1, T->Forks)

	{

	    T->Branch[v] = InTree();

	}

    }



    return T;

}







/*************************************************************************/

/*									 */

/*	Stream characters to/from file TRf from/to an address		 */

/*									 */

/*************************************************************************/





    StreamOut(s, n)

/*  ---------  */

    String s;

    int n;

{

    while ( n-- ) putc(*s++, TRf);

}







    StreamIn(s, n)

/*  ---------  */

    String s;

    int n;

{

    while ( n-- ) *s++ = getc(TRf);

}







/*************************************************************************/

/*									 */

/*	Free up space taken up by tree Node				 */

/*									 */

/*************************************************************************/





    ReleaseTree(Node)

/*  -------  */

    Tree Node;

{

    DiscrValue v;



    if ( Node->NodeType )

    {

	ForEach(v, 1, Node->Forks)

	{

	    ReleaseTree(Node->Branch[v]);

	}



	cfree(Node->Branch);



	if ( Node->NodeType == BrSubset )

	{

	    cfree(Node->Subset);

	}



    }



    cfree(Node->ClassDist);

    cfree(Node);

}







/*************************************************************************/

/*									 */

/*	Construct a leaf in a given node				 */

/*									 */

/*************************************************************************/





Tree Leaf(ClassFreq, NodeClass, Cases, Errors)

/*   ----  */

    ItemCount *ClassFreq;

    ClassNo NodeClass;

    ItemCount Cases, Errors;

{

    Tree Node;



    Node = (Tree) calloc(1, sizeof(TreeRec));



    Node->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount));

    memcpy(Node->ClassDist, ClassFreq, (MaxClass+1) * sizeof(ItemCount));

    

    Node->NodeType	= 0; 

    Node->Leaf		= NodeClass;

    Node->Items		= Cases;

    Node->Errors	= Errors;



    return Node; 

}







/*************************************************************************/

/*									 */

/*	Insert branches in a node 	                 		 */

/*									 */

/*************************************************************************/





    Sprout(Node, Branches)

/*  ------  */

    Tree Node;

    DiscrValue Branches;

{

    Node->Forks = Branches;

    

    Node->Branch = (Tree *) calloc(Branches+1, sizeof(Tree));

}







/*************************************************************************/

/*									 */

/*	Count the nodes in a tree					 */

/*									 */

/*************************************************************************/



	

    TreeSize(Node)

/*  --------  */

    Tree Node;

{

    int Sum=0;

    DiscrValue v;



    if ( Node->NodeType )

    {

	ForEach(v, 1, Node->Forks)

	{

	    Sum += TreeSize(Node->Branch[v]);

	}

    }



    return Sum + 1;

}







/*************************************************************************/

/*									 */

/*	Return a copy of tree T						 */

/*									 */

/*************************************************************************/





Tree CopyTree(T)

/*   ---------  */

    Tree T;

{

    DiscrValue v;

    Tree New;



    New = (Tree) malloc(sizeof(TreeRec));

    memcpy(New, T, sizeof(TreeRec));



    New->ClassDist = (ItemCount *) calloc(MaxClass+1, sizeof(ItemCount));

    memcpy(New->ClassDist, T->ClassDist, (MaxClass + 1) * sizeof(ItemCount));



    if ( T->NodeType )

    {

	New->Branch = (Tree *) calloc(T->Forks + 1, sizeof(Tree));

	ForEach(v, 1, T->Forks)

	{

	    New->Branch[v] = CopyTree(T->Branch[v]);

	}

    }



    return New;

}







/*************************************************************************/

/*									 */

/*	Save attribute values read with "discrete N"			 */

/*									 */

/*************************************************************************/





    SaveDiscreteNames()

/*  -----------------  */

{

    Attribute Att;

    DiscrValue v;

    int Length;



    ForEach(Att, 0, MaxAtt)

    {

	if ( SpecialStatus[Att] != DISCRETE ) continue;



	StreamOut((char *) &MaxAttVal[Att], sizeof(int));



	ForEach(v, 1, MaxAttVal[Att])

	{

	    Length = strlen(AttValName[Att][v]) + 1;



	    StreamOut((char *) &Length, sizeof(int));

	    StreamOut((char *) AttValName[Att][v], Length);

	}

    }

}







/*************************************************************************/

/*									 */

/*	Recover attribute values read with "discrete N"			 */

/*									 */

/*************************************************************************/





    RecoverDiscreteNames()

/*  --------------------  */

{

    Attribute Att;

    DiscrValue v;

    int Length;



    ForEach(Att, 0, MaxAtt)

    {

	if ( SpecialStatus[Att] != DISCRETE ) continue;



	StreamIn(&MaxAttVal[Att], sizeof(int));



	ForEach(v, 1, MaxAttVal[Att])

	{

	    StreamIn(&Length, sizeof(int));



	    AttValName[Att][v] = (char *) malloc(Length);

	    StreamIn(AttValName[Att][v], Length);

	}

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -