Drzewo Enum Java ID3
Próbuję stworzyć drzewo binarne, które jest uproszczoną wersją algorytmu ID3. Aby to zrobić, próbowałem użyć wyliczeń, ponieważ istnieje kilka odniesień uczących hierarchii enum, ale mam problem z przeniesieniem wyliczeń do funkcji, których potrzebuję, aby utworzyć drzewo. Ustawiłem wszystko, czego potrzebowałem dla drzewa, najlepiej jak potrafiłem, ale mam kłopoty z początkową konstrukcją drzewa.
Najpierw zrobiłem sześć wyliczeń, każde z własnym plikiem, więc nie musiałbym pisać „main.enumname” wszędzie. Te pięć pierwszych wyliczeń reprezentuje diagnostykę samochodów.
<code>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} </code>
Następnie zrobiłem jeszcze dwie enumy. Jeden dla różnych wyników diagnostycznych i jeden dla różnych „tematów” diagnostyki samochodów.
<code>public enum problems {battery, starter, solenoid, outofgas, flooding} public enum features {lightstatus, soundstatus, fuelstats, scents, turn, problems} </code>
Następnie posortowałem w drzewie pięć przykładów danych dotyczących różnych diagnostyki samochodów.
<code>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); </code>
Umieszczam różne diagnostyki samochodów, nazywane Cechami, w tablicy ArrayList do celów tasowania, ponieważ będą one losowo używane do budowy drzewa.
<code>//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); </code>
Ale jak napisać drzewo, które: pobiera enum funkcji, które sprawia, że temat roota pasuje do enum, a wszystkie różne statusy enum dzieci? Na przykład, jeśli soundstatus jest rootem, powinien zrobić cztery dzieci, które są normalne, wyją, skrzeczą i kliknij. W ten sposób mogę dopasować dźwięki przykładowe do dźwięków dzieci. To do tej pory mój węzeł.
<code>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; } } </code>