【问题标题】:Binary Search Tree (BST) search method for string字符串的二叉搜索树 (BST) 搜索方法
【发布时间】:2019-06-17 13:51:31
【问题描述】:

我已经构建了一个 BST,其中包含(国家代码(字符串)、指标代码(字符串)、指标名称(字符串)和年份数组列表(整数)和值(字符串))。

我试图弄清楚如何通过输入指标代码和输出值的年份来提示用户进行搜索。

如果您能说明如何对搜索方法进行编码,将不胜感激,因为我已经尝试了所有方法。

我在 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;值)
我的;工业就业(占总就业的百分比)(模拟国际劳工组织估计); SL.IND.EMPL.ZS;

29,08600044;28,56900024;28,36300087;28,02300072;27,51600075;27,48699951;27,39800072;27,30500031

【问题讨论】:

  • 提供更多代码,即 indicator.java、BigData.java 并请对这些 icode、ccode 进行一些注释,所有这些都有些混乱...还提供输入文件的示例...在您的search() 你为什么要使用 Indicator 使用 TreeNode 并称之为递归。
  • @ZainArshad 我已经更新了你想看的东西。至于搜索方法,我不太确定我在做什么,因为我所做的每一项研究都是基于整数的。

标签: java binary-search-tree


【解决方案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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-06
    • 2019-04-09
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多