// Copyright (c) 1996, 1997, 1998 Bruno R. Preiss, P.Eng.
// $Author: brpreiss $
// $Date: 1998/12/14 02:52:01 $
// $RCSfile: Demo5.java,v $
// $Revision: 3.5 $
package Opus5;
import java.io.*;
/**
* Demonstration program that tests the following classes:
* GeneralTree,
* BinaryTree,
* NaryTree,
* BinarySearchTree,
* AVLTree,
* MWayTree, and
* BTree.
* @see GeneralTree
* @see BinaryTree
* @see NaryTree
* @see BinarySearchTree
* @see AVLTree
* @see MWayTree
* @see BTree
* @author Bruno R. Preiss, P.Eng.
* @version $Id: Demo5.java,v 3.5 1998/12/14 02:52:01 brpreiss Exp $
**/
public class Demo5
{
//[
final String copyright =
"@(#) Copyright (c) 1998 by Bruno R. Preiss, P.Eng.";
//]
/**
* Performs various tests on the specified tree.
* @param tree The tree to be tested.
**/
private static void testTree (Tree tree)
{
PrintingVisitor visitor = new PrintingVisitor ();
Terminal.out.println (tree);
Terminal.out.println ("Breadth-First traversal");
tree.breadthFirstTraversal (visitor);
visitor.finish ();
Terminal.out.println ("Preorder traversal");
tree.depthFirstTraversal (new PreOrder (visitor));
visitor.finish ();
Terminal.out.println ("Inorder traversal");
tree.depthFirstTraversal (new InOrder (visitor));
visitor.finish ();
Terminal.out.println ("Postorder traversal");
tree.depthFirstTraversal (new PostOrder (visitor));
visitor.finish ();
Terminal.out.println ("Using Enumeration");
Enumeration i = tree.getEnumeration ();
while (i.hasMoreElements ())
Terminal.out.println (i.nextElement ());
}
/**
* Performs various tests on the specified search tree.
* @param tree The search tree to be tested.
**/
private static void testSearchTree (SearchTree tree)
{
for (int j = 1; j <=7; ++j)
tree.insert (new Int (j));
PrintingVisitor visitor = new PrintingVisitor ();
Terminal.out.println (tree);
Terminal.out.println ("Breadth-first traversal");
tree.breadthFirstTraversal (visitor);
visitor.finish ();
Terminal.out.println ("Preorder traversal");
tree.depthFirstTraversal (new PreOrder (visitor));
visitor.finish ();
Terminal.out.println ("Inorder traversal");
tree.depthFirstTraversal (new InOrder (visitor));
visitor.finish ();
Terminal.out.println ("Postorder traversal");
tree.depthFirstTraversal (new PostOrder (visitor));
visitor.finish ();
Terminal.out.println ("Using Enumeration");
Enumeration i = tree.getEnumeration ();
while (i.hasMoreElements ())
Terminal.out.println (i.nextElement ());
Terminal.out.println ("Withdrawing 4");
Comparable obj = tree.find (new Int (4));
try
{
tree.withdraw (obj);
}
catch (MethodNotImplemented exception)
{ Terminal.out.println (exception); }
Terminal.out.println (tree);
}
/**
* The (static) main program.
* Invokes various test routines.
* @param args The command-line arguments. Ignored.
**/
public static void main (String[] args)
{
GeneralTree gt = new GeneralTree (new Chr ('A'));
gt.attachSubtree (new GeneralTree (new Chr ('B')));
gt.attachSubtree (new GeneralTree (new Chr ('C')));
gt.attachSubtree (new GeneralTree (new Chr ('D')));
gt.attachSubtree (new GeneralTree (new Chr ('E')));
testTree (gt);
BinaryTree bt = new BinaryTree (new Int (4));
bt.attachLeft (new BinaryTree (new Int (2)));
bt.attachRight (new BinaryTree (new Int (6)));
testTree (bt);
NaryTree nt = new NaryTree (3, new Int (1));
nt.attachSubtree (0, new NaryTree (3, new Int (2)));
nt.attachSubtree (1, new NaryTree (3, new Int (3)));
nt.attachSubtree (2, new NaryTree (3, new Int (4)));
testTree (nt);
SearchTree tree = new BinarySearchTree ();
testSearchTree (tree);
tree = new AVLTree ();
testSearchTree (tree);
tree = new MWayTree (3);
testSearchTree (tree);
tree = new BTree (3);
testSearchTree (tree);
}
/**
* A visitor that prints each object it visits.
* @author Bruno R. Preiss, P.Eng.
* @version $Id: Demo5.java,v 3.5 1998/12/14 02:52:01 brpreiss Exp $
**/
private static class PrintingVisitor
extends AbstractVisitor
{
/**
* Indicates whether a comma should be printed.
**/
private boolean comma;
/**
* Prints the specified object on Terminal.out.
**/
public void visit (Object object)
{
if (comma)
Terminal.out.print (", ");
Terminal.out.print (object);
comma = true;
}
/**
* Finishes a line of output on Terminal.out and
* resets the comma flag.
**/
public void finish ()
{
Terminal.out.println ();
comma = false;
}
}
}