// Copyright (c) 1996, 1997, 1998 Bruno R. Preiss, P.Eng. // $Author: brpreiss $ // $Date: 1998/12/14 02:57:41 $ // $RCSfile: Application2.java,v $ // $Revision: 3.4 $ package Opus5; import java.io.*; /** * Application program that illustrates the use of a queue * to implement breadth-first traversal. * @see Algorithms#breadthFirstTraversal * @author Bruno R. Preiss, P.Eng. * @version $Id: Application2.java,v 3.4 1998/12/14 02:57:41 brpreiss Exp $ **/ public class Application2 { //[ final String copyright = "@(#) Copyright (c) 1998 by Bruno R. Preiss, P.Eng."; //] /** * Builds an N-ary tree that contains the specified range of keys. * @param lo The first key in the range. * @param hi The last key in the range. * @return An N-ary tree that contains the specified range of keys. * @see NaryTree **/ private static NaryTree BuildTree (char lo, char hi) { char mid = (char) ((lo + hi) / 2); NaryTree result = new NaryTree (2, new Chr (mid)); if (lo < mid) result.attachSubtree (0, BuildTree (lo, (char) (mid - 1))); if (hi > mid) result.attachSubtree (1, BuildTree ((char) (mid + 1), hi)); return result; } /** * Creates a tree containing the keys 'a' to 'g' and then * does a breadth-first traversal of that tree. * @param args The command-line arguments. Ignored. **/ public static void main (String[] args) { Terminal.out.println ("should be: dbfaceg"); Tree tree = BuildTree ('a', 'g'); Algorithms.breadthFirstTraversal (tree); } }