/** A NodeOperator that prints each node. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * http://www.corewebprogramming.com/. * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ class PrintOperator implements NodeOperator { public void operateOn(Node node) { System.out.println(node.getNodeValue()); } } /** A sample tree representing a parse tree of * the sentence "Java hackers hack Java", using * some simple context-free grammar. */ public class TreeTest { public static void main(String[] args) { Node adjective = new Node(" Adjective", new Leaf(" Java")); Node noun1 = new Node(" Noun", new Leaf(" hackers")); Node verb = new Node(" TransitiveVerb", new Leaf(" hack")); Node noun2 = new Node(" Noun", new Leaf(" Java")); Node np = new Node(" NounPhrase", adjective, noun1); Node vp = new Node(" VerbPhrase", verb, noun2); Node sentence = new Node("Sentence", np, vp); PrintOperator printOp = new PrintOperator(); System.out.println("Depth first traversal:"); sentence.depthFirstSearch(printOp); System.out.println("\nBreadth first traversal:"); sentence.breadthFirstSearch(printOp); } }