我建立了一个BST,其元素包括(国家代码(字符串),指标代码(字符串),指标名称(字符串)以及年份的ArrayList(整数)和值(字符串))。
我试图弄清楚如何通过输入指标代码然后输入输出值的年份来提示用户进行搜索。
如果您能说明如何编码搜索方法,将不胜感激,因为我已经尝试了所有方法。
我已经在BST类中尝试过此操作。但是感觉不对(?)
public void search(Indicator indicator, String searchTerm){
String str = (String)indicator.getICode();
int n1 = str.compareTo(searchTerm);
int n2 = searchTerm.compareTo(str);
if (str == null || str.equalsIgnoreCase(searchTerm)){
return str;
}
if (n1 > n2){
return search(indicator, searchTerm);
}
else if (n1 < n2){
return search(indicator, searchTerm);
}
}
这是我的应用程序类:
public class BigDataBST{
public static void main (String [] Args) throws IOException {
try{
BST bigdata = new BST();
MyData d1;
File inFile = new File ("Indicator.txt");
FileReader fr = new FileReader (inFile);
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
while(str != null ){
StringTokenizer st = new StringTokenizer(str,";");
ArrayList <MyData> data = new ArrayList();
String cCode = st.nextToken();
String iName = st.nextToken();
String iCode = st.nextToken();
for (int j = 0; j < 59; j++){
String v = st.nextToken();
int year = 1960 + j;
d1 = new MyData (year,v);
data.add(d1);
}
Indicator idct = new Indicator (cCode,iName,iCode,data);
bigdata.insertNode(idct);
str = br.readLine();
}
TreeNode类:
TreeNode left;
TreeNode right;
Indicator idct;
public TreeNode(Indicator id){
left = right = null;
idct = id;
}
指标类别:
private String cCode; //country code
private String iName; //indicator Name;
private String iCode; //indicator code;
public ArrayList <MyData> DataList;
public Indicator(){
cCode = null;
iName = null;
iCode = null;
DataList = null;
}
public Indicator(String cCode, String iName, String iCode,ArrayList <MyData> DataList){
this.cCode = cCode;
this.iName = iName;
this.iCode = iCode;
this.DataList = DataList;
}
//setter & getter method for attributes iCode,iName and cCode
//toString method
MyData类:
private int year;
private String value;
public MyData(){
year = 0;
value = null;
}
public MyData(int year, String value){
this.year = year;
this.value = value;
}
//setter & getter method for attributes year and value
//toString method
indicator.txt的示例:
(从左至右:cCode; iName; iCode;值)
MYS;工业就业(占总就业的百分比)(国际劳工组织的估算模型); SL.IND.EMPL.ZS;
29,08600044; 28,56900024; 28,36300087; 28,02300072; 27,51600075; 27,48699951; 27,39800072; 27,30500031
答案 0 :(得分:1)
我认为您不知道如何在Binary Search Tree
中进行搜索。您可以通过转到每个节点来做到这一点,并可以使用递归来做到这一点。
但是在您的search()
中,您将Indicator
作为参数,但是实际上您需要使用TreeNode
,因为每个节点都具有Indicator
类型的数据,您可以访问。
在您的search()
中,您使用相同的参数一次又一次地调用search()
,这将永远不会给您带来结果。此外,您没有基本情况。递归不是这样的。您将得到stackoverflowException
(哈哈哈,很有趣,我们在StackOverFlow上)。请改用以下代码:
public void search(string key)
{
searchHelper(key, root); // root node will be in Tree.java
}
public void searchHelper(string key, TreeNode current)
{
if(current == null)
{
System.out.println("\nCant find !");
return;
}
if(key.compareTo(current.idct.getICode()) < 0 )
searchHelper(key, current.left);
else if(key.compareTo(current.idct.getICode()) > 0)
searchHelper(key,current.right);
else
System.out.println("\n"+current.idct + "Found \n");
}