public void buildBinaryDecTree(Node node2, ArrayList<Podzial> podzialy, ArrayList<Object[]> objects) {
System.out.println("=================================================");
System.out.println("Rekurencja nr: " + this.ileRazy);
String leftAttrValue;
ArrayList rightAttrValue = new ArrayList();
ArrayList<Object[]> tmpObjectsLeft = new ArrayList();
ArrayList<Object[]> tmpObjectsRight = new ArrayList();
ArrayList wrtAtrybutowDec = new ArrayList();
/* wybieramy najlepszy atrybut */
Podzial podzialMax = wyliczMaxPodzial2(podzialy, objects);
System.out.println("Przed utworzeniem obiektu : " + node2);
node2 = utworzObiekt(this.idWezel, podzialMax.attribute);
System.out.println("Utworzenie obiektu z id wezlem " + node2.key + node2.nazwa);
++this.idWezel;
// utworzenie tablicy wartości
// atrybutów decyzyjnych, przeważnie to dwa będą
for (Object[] obiekt : objects) {
Object tmpAttr = obiekt[(int) this.decisionAttribute.get(1)];
if (!wrtAtrybutowDec.contains(tmpAttr)) {
wrtAtrybutowDec.add(tmpAttr);
}
}
leftAttrValue = podzialMax.getLeftSubTree();
rightAttrValue = podzialMax.getRightSubTree2();
node2.descToLeft = (String) leftAttrValue;
node2.descToRight = (String) podzialMax.getRightSubTree();
System.out.println("Opis lewej strzalki " + node2.descToLeft);
System.out.println("Opis prawej strzalki " + node2.descToRight);
// pobranie rekordów, które mają taką samą wartość atrybutu lewego
// jak z podziału max z lewego
for (Object[] obiekt : objects) {
Object tmpAttr = obiekt[podzialMax.nrAtrybutu];
if (tmpAttr.equals(leftAttrValue)) {
tmpObjectsLeft.add(obiekt);
}
}
// pobranie rekordów, które mają taką samą wartość atrybutu prawego
// jak z podziału max z prawego
for (Object rightAttrVal : rightAttrValue) {
for (Object[] obiekt : objects) {
Object tmpAttr = obiekt[podzialMax.nrAtrybutu];
if (tmpAttr.equals(rightAttrVal)) {
tmpObjectsRight.add(obiekt);
}
}
}
Object tmpLeafAttrDecLeft = null;
Boolean hasLeafLeft = false;
// sprawdzenie czy węzeł lewy jest jednorodny
// czy wymaga dalszego podziału
for (Object wrtAtrybutDec : wrtAtrybutowDec) {
for (Object[] tmpObject : tmpObjectsLeft) {
Object tmpAttrDec = tmpObject[(int) this.decisionAttribute.get(1)];
if (wrtAtrybutDec.equals(tmpAttrDec)) {
tmpLeafAttrDecLeft = wrtAtrybutDec;
hasLeafLeft = true;
} else {
tmpLeafAttrDecLeft = null;
hasLeafLeft = false;
break;
}
}
if (hasLeafLeft) {
break;
}
}
// sprawdzenie czy węzeł prawy jest jednorodny
// czy wymaga dalszego podziału
Object tmpLeafAttrDecRight = null;
Boolean hasLeafRight = false;
for (Object wrtAtrybutDec : wrtAtrybutowDec) {
for (Object[] tmpObject : tmpObjectsRight) {
Object tmpAttrDec = tmpObject[(int) this.decisionAttribute.get(1)];
if (wrtAtrybutDec.equals(tmpAttrDec)) {
tmpLeafAttrDecRight = wrtAtrybutDec;
hasLeafRight = true;
} else {
tmpLeafAttrDecRight = null;
hasLeafRight = false;
break;
}
}
if (hasLeafRight) {
break;
}
}
if (this.ileRazy == 0) {
this.kopia = node2;
System.out.println("Utworzenie kopii");
} else if (this.ileRazy == 1) {
this.kopia2 = node2;
}
this.ileRazy = this.ileRazy + 1;
// czy lewy wezeł bedzie lisciem
// czy wymaga dalszej rozbudowy? (rekurencja)
if (tmpLeafAttrDecLeft != null && hasLeafLeft) {
//node2.left = new Node(this.idWezel, (String) tmpLeafAttrDecLeft);
node2.left = utworzObiekt(this.idWezel, (String) tmpLeafAttrDecLeft);
++this.idWezel;
System.out.println("Wezeł lewy" + node2.left.nazwa + " jest lisciem");
//System.out.println("Dla podzialu " + podzialMax.attribute);
//System.out.println("Wezel lewy jest lisciem " + node.left.nazwa);
//showInfoToLeft(node, tmpObjectsLeft);
} else {
//showInfoToLeft(node, tmpObjectsLeft);
ArrayList tmpPodzialy = usunPodzial(podzialy, podzialMax);
ArrayList tmpObjects = usunRekordy(objects, rightAttrValue, podzialMax.nrAtrybutu);
//System.out.println("Dla podzialu " + podzialMax.attribute);
//System.out.println("Wezel lewy jest wezlem do dalszej rozbudowy");
buildBinaryDecTree(node2.left, tmpPodzialy, tmpObjects);
}
// czy prawy wezeł bedzie lisciem
// czy wymaga dalszej rozbudowy? (rekurencja)
if (tmpLeafAttrDecRight != null && hasLeafRight) {
node2.right = utworzObiekt(this.idWezel, (String) tmpLeafAttrDecRight);
//node2.right = new Node(this.idWezel, (String) tmpLeafAttrDecRight);
++this.idWezel;
System.out.println("Wezeł prawy" + node2.right.nazwa + " jest lisciem");
//System.out.println("Dla podzialu " + podzialMax.attribute);
//System.out.println("Wezel prawy jest lisciem " + node.right.nazwa);
//showInfoToRight(node, tmpObjectsRight);
} else {
//showInfoToRight(node, tmpObjectsRight);
ArrayList tmpPodzialy = usunPodzial(podzialy, podzialMax);
ArrayList attrValue = new ArrayList();
attrValue.add(leftAttrValue);
ArrayList tmpObjects = usunRekordy(objects, attrValue, podzialMax.nrAtrybutu);
//System.out.println("Dla podzialu " + podzialMax.attribute);
//System.out.println("Wezel prawy jest wezlem do dalszej rozbudowy ");
buildBinaryDecTree(node2.right, tmpPodzialy, tmpObjects);
}
}
A metoda utworzObiekt(...) to jest kod poniższy:
private Node utworzObiekt(int idWezel, String nazwa) {
return new Node(idWezel, nazwa);
}