参照J48,添加两个变量
/*the node's id*/
private int m_id;
//static count to assign the ids
private static int ms_count=0;
添加构造函数
public Id3Visualise()
{
m_id=ms_count++;
}
继承Drawable接口
public class Id3Visualise
extends Classifier
implements TechnicalInformationHandler, Sourcable,Drawable
实现绘图相关函数
/**
* return a string that describe a id3 tree
* the out put is in dotty format
* @return the graph dsicribed by a string
*/
public String graph()
{
StringBuffer text=new StringBuffer();
text.append("digraph ID3Tree{\n");
text.append(graph(0));
return text.toString()+"}\n";
}
/**
* Returns the type of graph this classifier
* represents.
* @return Drawable.TREE
*/
public int graphType()
{
return Drawable.TREE;
}
/**
* return a string that describe a id3 tree the out put is in dotty format
*
* @param level
* the level at which the tree is to be printed
*/
public String graph(int level)
{
StringBuffer text = new StringBuffer();
if (m_Attribute == null)
{
if (Instance.isMissingValue(m_ClassValue))
{
text.append("N" + m_id + " [label=\"null\" " + "shape=box style=filled");
}
else
{
text.append("N" + m_id + " [label=\""+m_ClassAttribute.value((int)m_ClassValue)+
"\" " + "shape=box style=filled");
}
text.append("]\n");
}
else
{
text.append("N"+m_id+" [label=\""+m_Attribute.name()+"\" ");
text.append("]\n");
for(int j=0;j<m_Attribute.numValues();j++)
{
text.append("N"+m_id+"->"+"N"+m_Successors[j].m_id+"[label=\"= "+
m_Attribute.value(j)+"\"]\n");
text.append(m_Successors[j].graph(level+1));
}
}
return text.toString();
}