Java Reference
In-Depth Information
because it is very low level; as a result we keep the design simpler and easier
to understand.
The isLeaf() method checks if the node is a leaf node, i.e. that it has no
children. The addChild() method adds a new child node: it can be used to
build the tree. The follow() method returns the child node which is linked
through the arc with the given label.
public class Node {
private String label;
private Arc firstArc;
public Node(String label) {
this .label # label;
}
public String label() { return label; }
public void addChild(String arcLabel, Node child){
firstArc # new Arc(arcLabel,child,firstArc);
}
public boolean isLeaf() { return firstArc ## null ; }
public Node follow(String arcLabel) {
for (Arc arc # firstArc; arc! # null ; arc # arc.nextArc())
if (arcLabel.equals(arc.label()))
return arc.destination();
return null ;
}
}
Class Arc is very simple: it has a read-only attribute (label) and a navigable
many-to-one association (destination) with the class Node . It can be straight-
forwardly implemented as follows:
public class Arc {
private Node destination;
private String label;
private Arc nextArc;
public Arc(String label, Node destination, Arc nextArc) {
this .label # label;
this .destination # destination;
this .nextArc # nextArc;
}
public String label() {
return label;
}
public Node destination() {
return destination;
}
public Arc nextArc(){
return nextArc;
}
}
Search WWH ::




Custom Search