这是主要课程:
import javax.swing.*;
class BinarySearchTree {
private Node root;
public void main()
{
int Value = 0;
while(Value!= -1)
{
Value = Integer.parseInt(JOptionPane.showInputDialog("Enter a value"));
insert(root, Value);
}
print();
}
public void insert(Comparable x)
{
root = insert(root, x);
}
public boolean find(Comparable x)
{
return find(root,x);
}
public void print()
{
print(root);
}
@SuppressWarnings("unchecked")
boolean find(Node tree, Comparable x)
{
if (tree == null)
return false;
if (x.compareTo(tree.data) == 0)
return true;
if (x.compareTo(tree.data) < 0)
return find(tree.left, x);
else
return find(tree.right, x);
}
void print(Node tree)
{
if (tree != null)
{
print(tree.left);
System.out.println(tree.data);
print(tree.right);
}
}
@SuppressWarnings("unchecked")
Node insert(Node tree, Comparable x)
{
if (tree == null)
{
return new Node(x);
}
if (x.compareTo(tree.data) < 0)
{
tree.left = insert(tree.left, x);
return tree;
}
else
{
tree.right = insert(tree.right, x);
return tree;
}
}
}
节点类:
public class Node {
public Comparable data;
public Node left;
public Node right;
Node(Comparable newdata) {
data = newdata;
}
}
当我调用“print();”时,我尝试打印出结果在void主类中,在树中插入所有值之后,它不会打印任何内容。当我单独调用每个方法时,它们可以工作,但是当我尝试从主类调用它们时却不行。出现这种情况的原因是什么?非常感谢
答案 0 :(得分:4)
这是因为你没有主要的方法。将主方法的签名更改为:
public static void main(String[] args)
然后重新运行它。