Java Reference
In-Depth Information
The class DecisionTree provides the access point for using a decision tree,
in particular it embeds the structural representation of the decision tree
(made up of Node and Arc objects) and provides the assignCategory() method
which performs the classification task on an item. The association to the
root node is implemented, as usual, by a private attribute ( root ); since it has
to be navigated only by methods defined inside the class, there is no getter
method. The assignCategory() method implements Algorithm 4.1. The algo-
rithm requires two input arguments: an item to be classified and the root of
the decision tree. The second input for this algorithm is implicit, it can
be found through the root association, and thus it does not appear as an
argument of the method.
public class DecisionTree
implements Classifier {
private Node root;
public DecisionTree(Node tree){
root # tree;
// Assign a category to an item.
// The algorithm takes as input an item and a decision tree.
// The output is the name of the category.
public String assignCategory(Item item){
Node current;
// 1) start at the root node
current # root;
// 2) repeat while the current node is not a leaf
while (! current.isLeaf() ){
// a) the node label is the name of the feature to be
// considered
String featureName # current.label();
// b) consider the value of the item's feature
String value # item.value(featureName);
// c) follow the arc corresponding to the value
Node reached # current.follow(value);
// d) the node reached becomes the current node
current # reached;
// 3) the label of the leaf node is the class of the item
return current.label();
We break up the test into three test cases: one ( TestItem ) to test the class
Item and the related classes, another ( TestTree ) to test the classes used to
represent the tree, and finally one ( TestClassifier ) to verify the algorithm that
Search WWH ::

Custom Search