figure presents sample node implementation it includes two classes derived from class varbinnodenamed leafnode and intlnode class intlnode accesses its children through pointers of type varbinnode function traverse illustrates the use of these classes when traverse calls method isleafjava' runtime environment determines which subclass this particular instance of root happens to be and calls that subclass' version of isleaf method isleaf then provides the actual node type to its caller the other member functions for the derived subclasses are accessed by type-casting the base class pointer as appropriateas shown in function traverse there is another approach that we can take to represent separate leaf and internal nodesalso using virtual base class and separate node classes for the two types this is to implement nodes using the composite design pattern this approach is noticeably different from the one of figure in that the node classes themselves implement the functionality of traverse figure shows the implementation herebase class varbinnode declares member function traverse that each subclass must implement each subclass then implements its own appropriate behavior for its role in traversal the whole traversal process is called by invoking traverse on the root nodewhich in turn invokes traverse on its children when comparing the implementations of figures and each has advantages and disadvantages the first does not require that the node classes know about the traverse function with this approachit is easy to add new methods to the tree class that do other traversals or other operations on nodes of the tree howeverwe see that traverse in figure does need to be familiar with each node subclass adding new node subclass would therefore require modifications to the traverse function in contrastthe approach of figure requires that any new operation on the tree that requires traversal also be implemented in the node subclasses on the other handthe approach of figure avoids the need for the traverse function to know anything about the distinct abilities of the node subclasses those subclasses handle the responsibility of performing traversal on themselves secondary benefit is that there is no need for traverse to explicitly enumerate all of the different node subclassesdirecting appropriate action for each with only two node classes this is minor point but if there were many such subclassesthis could become bigger problem disadvantage is that the traversal operation must not be called on null pointerbecause there is no object to catch the call this problem could be avoided by using flyweight to implement empty nodes typicallythe version of figure would be preferred in this example if traverse is member function of the tree classand if the node subclasses are