ID3 Java 枚举树
Posted
技术标签:
【中文标题】ID3 Java 枚举树【英文标题】:ID3 Java Enum Tree 【发布时间】:2012-04-19 17:01:46 【问题描述】:我正在尝试创建一个非二元学习树,它是 ID3 算法的简化版本。为此,我尝试使用枚举,因为有几个参考资料教授枚举层次结构,但是我在将枚举转移到制作树所需的函数时遇到了麻烦。我已经尽我所能为树设置了我需要的一切,但是我在树的初始构建时遇到了问题。
首先,我创建了六个枚举,每个都有自己的文件,所以我不需要到处写“main.enumname”。前五个枚举代表汽车诊断。
public enum fuelstats notempty, empty
public enum lightstatus Dim, Normal
public enum scents normal, gas
public enum soundstatus Normal, Howl, Screech, Click
public enum turn no, yes
接下来,我又做了两个枚举。一种用于不同的诊断结果,另一种用于汽车诊断的不同“主题”。
public enum problems battery, starter, solenoid, outofgas, flooding
public enum features lightstatus, soundstatus, fuelstats, scents, turn, problems
然后我制作了五个不同汽车诊断的数据示例,以便在树中进行排序。
Example example1 = new Example(lightstatus.Dim, soundstatus.Howl, turn.yes, fuelstats.notempty, scents.normal, problems.battery);
Example example2 = new Example(lightstatus.Normal, soundstatus.Screech, turn.no, fuelstats.notempty, scents.normal, problems.starter);
Example example3 = new Example(lightstatus.Normal, soundstatus.Click, turn.no, fuelstats.notempty, scents.normal, problems.solenoid);
Example example4 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.empty, scents.normal, problems.outofgas);
Example example5 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.notempty, scents.gas, problems.flooding);
//make an array list of Examples.
ArrayList<Example> Examples = new ArrayList<Example>();
Examples.add(example1);
Examples.add(example2);
Examples.add(example3);
Examples.add(example4);
Examples.add(example5);
我将各种汽车诊断信息(称为 Features)放在一个 ArrayList 中以进行洗牌,因为它们将被随机用于构建树。
//This ArrayList holds the Enums for shuffling purposes.
ArrayList<features> Features = new ArrayList<features>();
Features.add(features.soundstatus);
Features.add(features.lightstatus);
Features.add(features.turn);
Features.add(features.scents);
Features.add(features.fuelstats);
// Shuffle the elements in the list
Collections.shuffle(Features);
//The Features Array List is now a shuffled tree.
//We will do a single loop that will serve as our stack.
//First we take the top of the list and assign it to the root.
Tree id3 = new Tree(Features.get(0),Examples);
但是我如何写一棵树: 接受一个特征枚举,使根的主题与枚举匹配,并且枚举的所有不同状态都是子项?例如,如果 soundstatus 是根,它应该生成四个子项,即 Normal、Howl、Screech 和 Click。这样我就可以将示例声音与孩子的声音相匹配。到目前为止,这是我的节点。
public class Node
ArrayList<Node> children;
/* Constructor*/
public Node(ArrayList<Node> ExampleList)
this.ExampleList = ExampleList;
this.parent = parent;
this.children = children;
public ArrayList<Node> getChildren()
return children;
public void addChild(Node n)
children.add(n);
private ArrayList<Node> children;
Enum phrase;
private boolean isUsed;
Node parent;
public void setUsed(boolean isUsed)
this.isUsed = isUsed;
public boolean isUsed()
return isUsed;
//This method states if the node is a leaf
public boolean isLeaf()
if (this.getChildren() == null)
return true;
else
return false;
【问题讨论】:
【参考方案1】:您可以将子类添加到功能中:
import java.util.*;
interface hasEnumChildren
Class clazz();
enum fuelstats
notempty,empty
enum lightstatus
Dim,Normal
enum scents
normal,gas
enum soundstatus
Normal,Howl,Screech,Click
enum turn
no,yes
enum problems
battery,starter,solenoid,outofgas,flooding
enum features implements hasEnumChildren
lightstatus(lightstatus.class),soundstatus(soundstatus.class),fuelstats(fuelstats.class),scents(scents.class),turn(turn.class),problems(problems.class);
features(Class clazz)
this.clazz=clazz;
final Class clazz;
@Override public Class clazz()
return clazz;
public class So10233099
public static void main(String[] args)
System.out.println(Arrays.asList(features.lightstatus.clazz().getEnumConstants()));
【讨论】:
【参考方案2】:我有一个类似的问题,构建枚举的层次结构。但就我而言,类的层次结构也可以解决问题。如果你有兴趣,这里是我的帖子: How to build an hierarchy tree of categories in java using enums or any other way?
现在,仅涉及枚举层次结构,正如您在我的帖子中看到的那样,我发现这可能对您有用:
http://alexradzin.blogspot.hk/2010/10/hierarchical-structures-with-java-enums_05.html
特别是:
public enum OsType
OS(null),
Windows(OS),
WindowsNT(Windows),
WindowsNTWorkstation(WindowsNT),
WindowsNTServer(WindowsNT),
Windows2000(Windows),
Windows2000Server(Windows2000),
Windows2000Workstation(Windows2000),
WindowsXp(Windows),
WindowsVista(Windows),
Windows7(Windows),
Windows95(Windows),
Windows98(Windows),
Unix(OS)
@Override
public boolean supportsXWindows()
return true;
,
Linux(Unix),
AIX(Unix),
HpUx(Unix),
SunOs(Unix),
;
private OsType parent = null;
private OsType(OsType parent)
this.parent = parent;
希望对你有帮助!
【讨论】:
以上是关于ID3 Java 枚举树的主要内容,如果未能解决你的问题,请参考以下文章