首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ID3 Java枚举树

ID3 Java枚举树
EN

Stack Overflow用户
提问于 2012-04-20 01:01:47
回答 2查看 756关注 0票数 0

我正在尝试创建一个非二进制学习树,它是ID3算法的简化版本。为此,我尝试使用枚举,因为有几个教导枚举层次结构的参考资料,但我在将枚举传输到创建树所需的函数时遇到了问题。我已经尽我所能地设置了树所需的一切,但在树的初始构造方面遇到了问题。

首先,我创建了六个枚举,每个枚举都有自己的文件,这样我就不需要到处写"main.enumname“。前五个枚举表示汽车诊断。

代码语言:javascript
运行
复制
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}

接下来,我又做了两个枚举。一个用于不同的诊断结果,另一个用于汽车诊断的不同“主题”。

代码语言:javascript
运行
复制
public enum problems {battery, starter, solenoid, outofgas, flooding}
public enum features {lightstatus, soundstatus, fuelstats, scents, turn, problems}

然后,我制作了五个不同汽车诊断的数据示例,并在树中进行排序。

代码语言:javascript
运行
复制
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);

出于混洗的目的,我将各种汽车诊断信息(称为功能)放在一个ArrayList中,因为它们将被随机用于构建树。

代码语言:javascript
运行
复制
//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。这样我就可以将示例声音与孩子的声音进行匹配。到目前为止,这是我的节点。

代码语言:javascript
运行
复制
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;
    }

}
EN

回答 2

Stack Overflow用户

发布于 2012-04-20 02:51:30

您可以将子类添加到要素:

代码语言:javascript
运行
复制
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()));
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-11-25 14:41:57

我也遇到过类似的问题,构建枚举的层次结构。但在我的例子中,类的层次结构也可以做到这一点。如果你感兴趣,这里有我的帖子: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

特别是:

代码语言:javascript
运行
复制
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;
}

我希望它能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10233099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档