java 中的二叉搜索树,main 未从 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);
}
}
也许您应该单步执行 iprint 函数并找到引发异常的确切节点以确定导致问题的条件。
不知道内容,很难给出合适的答案。
mytree.iprint()
和 mytree.preprint()
期待争论。您需要将 mytree.root
传递给主函数中的这两个函数。
二叉搜索树无法正常工作,我被卡住了,不知道如何解决这个问题。知道我做错了什么。该对象确实存在于 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);
}
}
也许您应该单步执行 iprint 函数并找到引发异常的确切节点以确定导致问题的条件。
不知道内容,很难给出合适的答案。
mytree.iprint()
和 mytree.preprint()
期待争论。您需要将 mytree.root
传递给主函数中的这两个函数。