Reputation: 73
i've built a BST that have elements of (country code(string), indicator code(string), indicator name(string) and ArrayList of Years(int) and values(string)).
I'm trying to figure out how to prompt the user to search by entering Indicator code and then the year which the output will be the value.
it would be much appreciated if you could show how to code the search method cause I've tried everything.
I've tried this in the BST class. but it doesn't feel right(?)
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);
}
}
this is my application class:
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 class:
TreeNode left;
TreeNode right;
Indicator idct;
public TreeNode(Indicator id){
left = right = null;
idct = id;
}
indicator class:
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 class:
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
example of indicator.txt:
(from left: cCode; iName; iCode; values)
MYS; Employment in industry (% of total employment) (modeled ILO estimate); SL.IND.EMPL.ZS;
29,08600044;28,56900024;28,36300087;28,02300072;27,51600075;27,48699951;27,39800072;27,30500031
Upvotes: 1
Views: 1107
Reputation: 1907
I think you don't know how to search in a Binary Search Tree
. You can do this by going to each node and can use recursion to do so.
In your search()
you are taking Indicator
as a parameter, but actually you need to take TreeNode
because every node has a data of type Indicator
which you can access.
In your search()
you are calling the search()
again and again with same parameters which will not give you results ever. Moreover, you don't have a base case. That's not how recursion work. You will be getting a stackoverflowException
(Hahaa, its funny because we are on StackOverFlow). Use this code instead:
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");
}
Upvotes: 1