id3 java,ID3 Java枚举树

I'm trying to make a non-binary learning tree that's a simplified version of the ID3 algorithm. To do this, I tried to use enums, because there are several references teaching enum hierarchies, but I'm having trouble with the transfer of enums to the functions I need to make the tree. I've set up everything I need for the tree as best as I could, but I'm having trouble with the initial construction of the tree.

First, I made six enums, each with their own file so I wouldn't need to write "main.enumname" everywhere. These first five enums represent car diagnostics.

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}

Next, I made two more enums. One for the different diagnostic results, and one for the different "topics" of car diagnostics.

public enum problems {battery, starter, solenoid, outofgas, flooding}

public enum features {lightstatus, soundstatus, fuelstats, scents, turn, problems}

I then made five data examples of different car diagnostics to be sorted in the tree.

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 Examples = new ArrayList();

Examples.add(example1);

Examples.add(example2);

Examples.add(example3);

Examples.add(example4);

Examples.add(example5);

I put the various car diagnostics, called Features, in an ArrayList for shuffling purposes, because they will be randomly used to build the tree.

//This ArrayList holds the Enums for shuffling purposes.

ArrayList Features = new ArrayList();

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);

But how do I write a tree that:

Takes in a feature enum that makes the subject of the root match the enum, and all of the different statuses of the enum the children? For example, if soundstatus is the root, it should make four children that are Normal, Howl, Screech, and Click. That way I can match the Example sounds with the children sounds. This is my node so far.

public class Node

{

ArrayList children;

/* Constructor*/

public Node(ArrayList ExampleList)

{

this.ExampleList = ExampleList;

this.parent = parent;

this.children = children;

}

public ArrayList getChildren()

{

return children;

}

public void addChild(Node n)

{

children.add(n);

}

private ArrayList 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;

}

}

解决方案

I had a similar problem, building an hierarchy of enums. But in my case, an hierarchy of classes could also do the trick. In case you are interested here is my post:

How to build an hierarchy tree of categories in java using enums or any other way?

Now, concerning only enum hierarchy, as you can see on my post, I found this that may work for you:

In particular:

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;

}

I hope it helps!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值