/* returns number of nodes in this subtree */ public int nodeCount() { if (leaf) { return 1; } else { return 1 + leftChild.nodeCount() + rightChild.nodeCount(); } }
/* returns number of leaves in this subtree */ public int leafCount() { if (leaf) { return 1; } else { return leftChild.leafCount() + rightChild.leafCount(); } }
/* Recursive, returns an array list * containing this node and all child nodes */ public ArrayList<avPair> enumeratePairs() { ArrayList<avPair> myAV = new ArrayList<avPair>(); if (leaf) { myAV.add(this); return myAV; } else { myAV.add(this); myAV.addAll(leftChild.enumeratePairs()); myAV.addAll(rightChild.enumeratePairs()); return myAV; } }
/* The main tree traversal function; if node is a leaf, * return classification; otherwise, determine whether * instance is higher or lower than this attribute-value * pair and returns testInstance of proper child */ public boolean testInstance(Instance in) { if (majority) { return majClass; } if (leaf) { return classification; } else { if (in.get((int) attribute) <= value) { return leftChild.testInstance(in); } else { return rightChild.testInstance(in); } } }