📄 demosimpletreeuos.java
字号:
/* DemoSimpleTreeUos.java
* ---------------------------------------------
* Copyright (c) 2002 University of Saskatchewan
* All Rights Reserved
* --------------------------------------------- */
package dslib.demo;
import dslib.tree.*;
import java.io.*;
import java.lang.reflect.*;
/** A class that demonstrates the use of any SimpleTreeUos */
public class DemoSimpleTreeUos
{
/** A demo for testing SimpleTreeUos.
@param t The tree that will be demo-ed.*/
public DemoSimpleTreeUos(LinkedSimpleTreeUos t)
{
// place the output in a text file that reflects the class being demonstrated.
String className = t.getClass().getName();
className = className.substring(className.lastIndexOf(".")+1);
PrintWriter out = null;
try
{
out = new PrintWriter(new FileOutputStream("OutputDemo" + className + ".txt", true));
} catch (Exception e)
{
System.err.println("Error creating output file in test routine for " + className);
e.printStackTrace();
}
out.println("Testing routines of SimpleTreeUos");
out.println("inserted 3, 6, 10");
out.println("inserted 2, 4, 9, 11");
t = createInstance(t, new Integer(6));
LinkedSimpleTreeUos t1 = createInstance(t, new Integer(2));
LinkedSimpleTreeUos t2 = createInstance(t, new Integer(3));
LinkedSimpleTreeUos t3 = createInstance(t, new Integer(4));
LinkedSimpleTreeUos t4 = createInstance(t, new Integer(7));
LinkedSimpleTreeUos t5 = createInstance(t, new Integer(10));
LinkedSimpleTreeUos t6 = createInstance(t, new Integer(11));
t.setRootLeftSubtree(t2);
t2.setRootLeftSubtree(t1);
t2.setRootRightSubtree(t3);
t.setRootRightSubtree(t5);
t5.setRootLeftSubtree(t4);
t5.setRootRightSubtree(t6);
out.println("Root value? " + t.rootItem());
out.println("Left value? " + t.rootLeftSubtree().rootItem());
out.println("Right value? " + t.rootRightSubtree().rootItem());
out.println("Left Left value? " + t.rootLeftSubtree().rootLeftSubtree().rootItem());
out.println("Right Left value? " + t.rootLeftSubtree().rootRightSubtree().rootItem());
out.println("Left Right value? " + t.rootRightSubtree().rootLeftSubtree().rootItem());
out.println("Right Right value? " + t.rootRightSubtree().rootRightSubtree().rootItem());
out.println("That's it.");
t.wipeOut();
out.close();
}
/** creates a new instance of type l with initial value o
@param l an instance of a tree
@param o an initial value*/
protected LinkedSimpleTreeUos createInstance(LinkedSimpleTreeUos l, Object o)
{
/* this method uses a few aspects of the Java Language that have not been discussed
in the text. In a nutshell, this class creates a new instance of whatever
type of tree it is passed with a inital value of o. It does this by
finding all of the constructors of the class and selecting the one that
it requires.*/
try
{
Object[] args = {null, o, null};
Constructor[] con = l.getClass().getConstructors();
for(int i =0 ; i <= con.length -1; i++)
if(con[i].getParameterTypes().length == 3)
return (LinkedSimpleTreeUos) con[i].newInstance(args );
} catch (Exception e)
{
System.out.println("Error in instance creation!!");
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -