我有一个程序,允许用户在二叉查找树之间选择一棵树和一棵红黑树。我为二叉搜索树编写了类,现在正在处理splay树,但是我意识到我的与用户交互的方法只能用于二叉搜索树。我设置它以便它将创建一个用户选择的树的实例,但在我的代码中,只使用用户选择二叉查找树时将创建的变量。我的问题是我如何使它,以便我只会创建一个用户选择的树的实例,我如何只能使用一个变量,以便当我插入项目或在树上工作时,我不必添加更多的条件语句为不同的树?Java通过对象到方法
这就是我现在
import java.util.Scanner;
import java.lang.Math.*;
public class Driver1
{
public static void main(String[] args)
{
//local variables
String treeChoice = null;
String choice = null;
String choice2 = null;
String command = null;
int insertAmount = -1;
String pattern;
int height = -1;
int i = -1;
//BST<Integer> myTree = null;
//ST<Integer> mySTTree = null;
int num = 0;
//Scanners to take user input
Scanner input = new Scanner(System.in);
Scanner inputt = new Scanner(System.in);
System.out.println("Which tree would you like to test (BST, ST, RBT)? ");
treeChoice = input.nextLine();
//Based on user input either a BST, Splay Tree, or RBT will be initialized.
if("BST".equalsIgnoreCase(treeChoice))
{
BST<Integer> myTree = new BST<Integer>();
}
else if("ST".equalsIgnoreCase(treeChoice))
{
//System.out.println("Splay Tree not ready yet");
ST<Integer> mySTTree = new ST<Integer>();
}
else if("RBT".equalsIgnoreCase(treeChoice))
{
System.out.println("RBT not ready yet");
//RBT<Integer> myTree = new RBT<Integer>();
}
else
{
System.out.println("Invalid Entry");
}
//Ask user how many items to input
System.out.println("How many items would you like to insert? ");
insertAmount = input.nextInt();
//ask user if the items will be random or sorted
System.out.println("Pattern (random or sorted): ");
choice2 = inputt.nextLine();
//If random, create random numbers
if("random".equalsIgnoreCase(choice2))
{
for(i = 1; i <= insertAmount; i++)
{
myTree.insert((int)(Math.random()*1000000)+i);
}
}
//else fill the tree with numbers in order from 1 to the user limit
else if("sorted".equalsIgnoreCase(choice2))
{
for(i = 1; i <= insertAmount; i++)
{
myTree.insert(i);
}
}
//Keep asking users input on what to do to the tree until user says quit
while(command != "quit")
{
System.out.println(
"Next command (insert X, delete X, find X, height, quit)?");
command = inputt.nextLine();
if (command.startsWith("insert"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.insert(num);
if(result == false)
{
System.out.println("Item already present.");
}
}
else if(command.startsWith("delete"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.delete(num);
}
else if(command.startsWith("find"))
{
num = Integer.parseInt(command.replaceAll("\\D", ""));
boolean result = myTree.find(num);
if(result == true)
{
System.out.println("Item present.");
}
else
{
System.out.println("Item not present.");
}
}
else if(command.startsWith("height"))
{
System.out.println("Current height of tree " + myTree.height());
}
else if(command.startsWith("quit"))
{
break;
}
System.out.println();
}
}//Close main method
有,你可以看到我只填写myTree这将是,如果用户选择BST创建的树。而在while循环中,我只能在myTree上工作。
我怎样才能使这更通用或我的其他想法是采取用户输入,然后创建该树的实例,然后将该实例传递到一个单独的方法,以便我仍然可以使用只有myTree,因为它会参考到传入该方法的实例,但我不知道如何将实例传递到另一个方法。这种方式似乎是最好的,但林不知道
任何帮助表示赞赏
行树树会是什么? –
user214577
2013-03-07 08:05:08
@ user214577定义您的树类共同使用的方法的接口(如第一句中所述)。例如,请参阅[什么是接口](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html)。 – 2013-03-07 08:09:26