사용자가 이진 검색 트리와 분홍색 트리 중 하나를 선택할 수있는 프로그램이 있습니다. 나는 바이너리 검색 트리에 대한 클래스를 작성했고 현재는 스플레이 트리에서 작업하고 있지만 필자는 사용자와 상호 작용하는 내 메소드가 바이너리 검색 트리에서만 작동한다는 것을 깨달았다. 사용자가 선택한 트리 중 하나의 인스턴스를 만들지 만 내 코드에서는 사용자가 이진 검색 트리를 선택한 경우 만들어 질 변수 만 사용하도록 설정했습니다. 내 질문은 내가 어떻게 사용자가 선택한 트리의 인스턴스를 만들 수 있도록 내가 할 수있는 유일한 방법은 하나의 변수를 사용할 수 있도록 내가 항목을 삽입하거나 나무에 더 많은 조건문을 추가 할 필요가 없다는 것입니다 다른 나무들?Java Pass 개체를 메서드
이는 내가 채울 볼 수 있듯이 지금
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
가지고있는 경우에만 사용자가 BST를 선택한 경우 생성 된 트리 될 것이라고으로 myTree. while 루프에서는 myTree에서만 작동합니다.
어떻게하면 더 일반화 될 수 있습니까? 아니면 다른 생각은 사용자 입력을 가져온 다음 해당 트리의 인스턴스를 만든 다음 인스턴스를 별도의 메서드로 전달하여 참조 할 것이므로 myTree 만 사용할 수 있도록하는 것입니다. 해당 메서드에 전달 된 인스턴스로 전달하지만 다른 메서드로 인스턴스를 전달하는 방법을 모르겠습니다. 이 방법은 가장하지만 것 같다 메신저하지 않도록
어떤 도움에 사용되는 방법을 지정,Tree
말, 공통 기본 클래스를 확장, 또는 더 나은, 공통 인터페이스를 구현해야
라인 트리 트리는 무엇입니까? –
user214577
@ user214577 트리 클래스가 공통으로 가지고있는 메소드를 정의하는 인터페이스입니다 (첫 번째 문장에서 언급했듯이). 예를 들어 [인터페이스 란 무엇입니까?] (http://docs.oracle.com/javase/tutorial/java/concepts/interface.html)를 참조하십시오. –