【问题标题】:Binary search tree in java, main is not reading from BSTjava中的二叉搜索树,主要不是从BST读取
【发布时间】:2015-12-12 03:03:04
【问题描述】:

二叉搜索树无法正常工作,我被卡住了,不知道如何解决这个问题。知道我在做什么错。该对象确实存在于 BST 中,所以为什么 error.any 帮助将不胜感激。 这是错误:

Exception in thread "main" java.lang.Error: Unresolved compilation 
problems: 
    The method iprint(Node1) in the type BST is not applicable for the arguments ()
    The method preprint(Node1) in the type BST is not applicable for the arguments ()
at MainBST.main(MainBST.java:38)

我的主要代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MainBST {
    public static void main(String[] args) throws FileNotFoundException 
    {
        BST mytree = new BST();
        String file2 = "infile2.txt";
        String file3 = "inFile3.txt";
        addData(file2, "[,\n\r]+", mytree);
        addData(file3, "[\t\n\r]+", mytree);
        System.out.println("-------------Pre-order------------------");
        mytree.iprint(); //This is where i have the problem
        System.out.println("---------------In-Order-----------------");
        mytree.preprint(); //Object is there so why the error
    }
    private static void addData(String filepath, String delimiter, BST tree) throws FileNotFoundException
    {
        File file = new File(filepath);
        Scanner sc = new Scanner(file).useDelimiter(delimiter);
        while(sc.hasNext())
        {
            tree.add(sc.next(), sc.next(), sc.nextInt());
        } 
    }
}

我的 BST 代码

public class BST {
    Node1 root;
    public BST() {
        root = null;
    }
    public void add(String fname, String lname, int age) {
        Node1 NewNode = new Node1(lname, fname, age);
        Node1 compare = root;
        if (root == null)
            root = NewNode;
        else {
            while (true) {
                if (NewNode.age < compare.age) {
                    if (compare.lChild == null) {
                        compare.lChild = NewNode;
                        break;
                    }
                    compare = compare.lChild;
                } else {
                    if (compare.rChild == null) {
                        compare.rChild = NewNode;
                        break;
                    }
                    compare = compare.rChild;
                }
            }
        }
    }
    public void iprint(Node1 t) {
        if (t != null) {
            iprint(t.lChild);   // left
            System.out.println(t);   // data 
            iprint(t.rChild);   // right
        }
    }
    public void preprint(Node1 t) {
        if (t != null) {
            System.out.println(t);   // data 
            preprint(t.lChild);   // left
            preprint(t.rChild);   // right
        }
    }
}

我的 Node1 的代码

public class Node1 {
        String lname;
        String fname;
        int age;
        Node1 lChild;
        Node1 rChild;
        public Node1( String l, String f, int a)
        {
            this.lname = l;
            this.fname = f;
            this.age = a;
            lChild = null;
            rChild = null;
        }
        public String toString()
        {
            return(" the age for "+fname+" "+ lname +" is "+ age);
        }
    }

【问题讨论】:

    标签: java binary-tree binary-search-tree


    【解决方案1】:

    mytree.iprint()mytree.preprint() 期待一个参数。您需要在主函数中将mytree.root 传递给这两个函数。

    【讨论】:

    • 我有一个对象(Node1 t)。应该将其传递给 main 并且如果我添加 root 就可以了,那么我必须发送 lchild、rchild 和其他东西。
    • 所以我做了 mytree.root.iprint();和 mytree.iprint.root();没有一个会起作用
    • @JavaCoder 应该是 mytree.iprint(mytree.root);mytree.preprint(mytree.root); 并且,您需要使 root public 能够从另一个类访问它。
    • 现在它显示这个错误:[code]Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown源) 在 java.util.Scanner.nextInt(Unknown Source) 在 java.util.Scanner.nextInt(Unknown Source) 在 MyMainIsMissing.addData(MyMainIsMissing.java:49) 在 MyMainIsMissing.main(MyMainIsMissing.java:32) [\代码]
    • @JavaCoder 你可以找到更多关于这种类型的异常here
    【解决方案2】:

    也许您应该单步执行 iprint 函数并找到引发异常的确切节点以确定导致问题的条件。

    很难给出一个合适的答案,因为(我)不知道内容。

    【讨论】:

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