Java Reference
In-Depth Information
4.5.1
Analysis
The tree that is described by means of nodes and arcs is a complex data
structure. The goal here is to find an easy-to-understand representation. It
has to be textual and must contain the same information originally present
in the decision tree. For instance, the decision tree shown in Figure 4.1 can
be represented as shown in Figure 4.9.
4.5.2
Design
The standard way of generating a textual representation of an object (or
group of objects) is through the toString() method. Since we are interested in
representing the whole tree, a recursive version of such a method is
required.
Figure 4.10 shows how to extend the class structure defined in the previous
iteration. Two methods are provided: one is private ( toString(String indent) )
and implements the recursive algorithm which is executed for each indent
level, the other is public ( toString() ) and returns a representation of the whole
tree rooted in the node.
The behaviour of the recursive algorithm can be depicted using a
sequence diagram, as shown in Figure 4.11. The sequence diagram is based
on the static structure described in Figure 4.7.
A client of class Node invokes the public toString() method, which in turn
calls the private toString() recursive method on the same node, passing
string “ ” as argument corresponding to the first indent level. Then the
recursive method invokes itself on the node's children; child node n 1 is
shown in the figure. The recursion proceeds until the method is invoked on
leaves, such as l 1 and l 2 in the figure, which just represent themselves and
return control.
4.5.3
Implementation
The implementation of the toString() method for class Item requires the
concatenation of strings. Since String objects are non-modifiable, every
[AC
(yes) --> [ABS
(yes) --> [high]
(no) --> [medium]
]
(no) --> [ABS
(yes) --> [medium]
(no) --> [low]
]
]
Figure 4.9 Textual representation of a decision tree
 
Search WWH ::




Custom Search