簡體   English   中英

Java中的二進制搜索樹,主要不是從BST讀取

[英]Binary search tree in java, main is not reading from BST

二進制搜索樹無法正常工作,我被困住了,不知道如何解決此問題。 不知道我在做什么錯。 該對象確實存在於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);
        }
    }

mytree.iprint()mytree.preprint()一個參數。 您需要將mytree.root傳遞給主函數中的這兩個函數。

也許您應該逐步執行iprint函數,並找到引發異常的確切節點,以確定導致問題的條件。

由於(I)不知道內容,因此很難給出適當的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM