An implementation of the ID3 Decision Tree algorithm in Java from scratch.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

221 lines
4.6 KiB

package com.AI;
import com.DBpackage.MyDatabase;
import java.util.ArrayList;
class Node
{
private String attributeName;
private ArrayList<Value> valuesList;
class Value
{
private String value;
private Node next;
public Value()
{
next = null;
}
String getValue() {
return value;
}
void setValue(String value) {
this.value = value;
}
Node getNext() {
return next;
}
void setNext(Node next) {
this.next = next;
}
}
public Node()
{
valuesList = new ArrayList<>();
}
public Node(String attributeName)
{
this.setAttributeName(attributeName);
valuesList = new ArrayList<>();
}
public String getAttributeName()
{
return attributeName;
}
public void setAttributeName(String attributeName)
{
this.attributeName = attributeName;
}
public ArrayList<Value> getValuesList() {
return valuesList;
}
public void setValuesList(ArrayList<Value> valuesList) {
this.valuesList = valuesList;
}
void setVal(String value, Node obj)
{
Value valueObj = new Value();
valueObj.setValue(value);
valueObj.setNext(obj);
//obj.getValuesList().get(0).setPrevious(this);
//if(obj.getPrevious().getValuesList().get(0).isVisited() == true);
valuesList.add(valueObj);
}
}
class NaryTree
{
private Node head;
NaryTree()
{
head = null;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
//////////////////////////////////////////////////////////////////////////////////
/*Node createNode(Node h, Node parent, String edgeName, Node child) {
if (head == null)
{
System.out.println("Initializing head node");
// h = new Node(parentName);
// this.setHead(h);
// System.out.println("Head: " + h.getAttributeName());
// return h;
head = parent;
return head;
}
else
{
createLink(parent, child, edgeName);
return child;
}
}*/
Node createNode(Node h, Node parent, String edgeName, Node child) {
if (head == null)
{
System.out.println("Initializing head node");
// h = new Node(parentName);
// this.setHead(h);
// System.out.println("Head: " + h.getAttributeName());
// return h;
head = parent;
return head;
}
else
{
createLink(parent, child, edgeName);
return child;
}
}
private void createLink(Node parent, Node child, String value)
{
// System.out.println("Edge: " + value + " Child: " + child.getAttributeName() );
parent.setVal(value, child);
}
//////////////////////////////////////////////////////////////////////////////////
private void traverse(Node node)
{
if(node == null)
{
System.out.println();
return;
}
System.out.print(" " + node.getAttributeName() + " ");
if(node.getAttributeName().equalsIgnoreCase("Yes") ||
node.getAttributeName().equalsIgnoreCase("No"))
System.out.println();
if(node.getValuesList() == null) {
System.out.print(node.getAttributeName());
return;
}
for(Node.Value v: node.getValuesList())
{
System.out.print(" " + v.getValue());
traverse(v.getNext());
}
}
void displayTree()
{
System.out.println("Displaying tree(pre-order):");
System.out.println("------------------");
if(head!=null) {
traverse(head);
}
else
{
System.out.println("Head node not created/initialized");
}
System.out.println();
System.out.print("------------------");
System.out.println();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
private void traverseDBTest(Node node, MyDatabase myDatabase)
{
if(node == null)
{
System.out.println();
return;
}
//System.out.print(" " + node.getAttributeName());
if(node.getValuesList() == null) {
// System.out.print(node.getAttributeName());
return;
}
for(Node.Value value : node.getValuesList())
{
// System.out.print(" " + sn.getValue());
if(value.getNext().getAttributeName() == null)
System.out.println("null");
else {
myDatabase.insertTree(node.getAttributeName(), value.getValue(), value.getNext().getAttributeName());
System.out.println("Parent: " + node.getAttributeName()
+ " Edge: " + value.getValue()
+ " Child: " + value.getNext().getAttributeName());
}
traverseDBTest(value.getNext(),myDatabase);
}
//System.out.println("Node named" + nodeName + "does not exist in the tree");
}
void dbTest(MyDatabase myDatabase)
{
if(head!=null) {
traverseDBTest(head, myDatabase);
}
else
{
System.out.println("Head node not created/initialized");
}
System.out.println();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
}