import java.lang.*; abstract class TreeNode { public int key; public TreeNode leftchild; public TreeNode rightchild; public TreeNode(int k, TreeNode left, TreeNode right) { key = k; leftchild = left; rightchild = right; } } class BlueNode extends TreeNode { public BlueNode(int k, TreeNode left, TreeNode right) { super(k, left, right); } } class GreenNode extends TreeNode { public GreenNode(int k, TreeNode left, TreeNode right) { super(k, left, right); } } class Visitor { public void visit(TreeNode node) { System.out.print(node.key + ": "); if (node instanceof BlueNode) System.out.println("Blue"); else if (node instanceof GreenNode) System.out.println("Green"); if (node.leftchild != null) visit(node.leftchild); if (node.rightchild != null) visit(node.rightchild); } } public class SimpleVisitor { public static void main(String [] args) { TreeNode root = new BlueNode(1, new GreenNode(21, new BlueNode(31, null, null), new GreenNode(32, null, null)), new GreenNode(22, new BlueNode(33, null, null), new GreenNode(34, null, null))); Visitor v = new Visitor(); v.visit(root); } }