我遇到了二进制搜索树的问题。
我已经将文件文件读入arrayList并尝试将其放入二进制搜索树中,但是当我尝试调用bst.insert函数然后通过调用printTree进行检查时,它显示为空。任何帮助将不胜感激。
这是BST文件
import java.util.Comparator;
public class BST<AnyType extends Comparable<AnyType>> {
private BinaryNode<AnyType> root;
public BST()
{
root = null;
}
public void insert (AnyType x)
{
root = insert(x, root);
}
public void remove(AnyType x)
{
root = remove(x, root);
}
public AnyType findMin()
{
if(isEmpty())
throw new RuntimeException();
return findMin(root).element;
}
public AnyType findMax()
{
if(isEmpty())
throw new RuntimeException();
return findMax(root).element;
}
public boolean contains(AnyType x)
{
return contains(x, root);
}
public boolean isEmpty()
{
return root == null;
}
public void printTree()
{
if (isEmpty())
System.out.println("Tree is Empty");
else
printTree(root);
}
private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t)
{
if(t==null)
return new BinaryNode<AnyType>(x,null,null);
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
t.left = insert(x,t.left);
else if (compareResult > 0)
t.right = insert(x, t.right);
else
;
return t;
}
private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t)
{
if(t==null)
return t;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
t.left = remove(x, t.left);
else if(compareResult > 0)
t.right = remove(x, t.right);
else if (t.left != null && t.right != null)
{
t.element = findMin(t.right).element;
t.right = remove(t.element, t.right);
}
else
t = (t.left != null) ? t.left : t.right;
return t;
}
private BinaryNode<AnyType> findMin (BinaryNode<AnyType> t)
{
if (t == null)
return null;
else if (t.left == null)
return t;
return findMin(t.left);
}
private BinaryNode<AnyType> findMax (BinaryNode<AnyType> t)
{
if (t != null)
while(t.right != null)
t = t.right;
return t;
}
private boolean contains(AnyType x, BinaryNode<AnyType> t)
{
if (t == null)
return false;
int compareResult = x.compareTo(t.element);
if(compareResult < 0)
return contains(x, t.left);
else if (compareResult > 0)
return contains (x, t.right);
else
return true;
}
private void printTree (BinaryNode<AnyType> t)
{
if (t != null)
{
printTree(t.left);
System.out.println(t.element);
printTree(t.right);
}
}
private int height(BinaryNode<AnyType> t)
{
if (t == null)
return -1;
else
return 1 + Math.max(height(t.left), height(t.right));
}
private static class BinaryNode<AnyType>
{
BinaryNode(AnyType theElement)
{
this(theElement, null, null);
}
BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt)
{
element = theElement;
left = lt;
right = rt;
}
AnyType element;
BinaryNode<AnyType> left;
BinaryNode<AnyType> right;
}
}
主文件:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Main {
public static void main(String[] args) {
Clinic c1 = new Clinic();
BST<Patient> bst = new BST<Patient>();
try{
File f = new File("patients.txt");
Scanner x = new Scanner(f);
while(x.hasNextLine()){
String line = x.nextLine();
String[] detail = line.split("\t",4);
String name = detail[0];
String doctor = detail[1];
String currApp = detail[2];
String nextApp = detail[3];
Patient p = new Patient(name, doctor, currApp, nextApp);
c1.addPatient(p);
c1.assignDoctor();
}
} catch (FileNotFoundException e){
e.printStackTrace();
}
//sort ArrayList of patients
Collections.sort(c1.patientList, new Comparator<Patient>(){
@Override
public int compare(Patient o1, Patient o2){
return o1.getName().compareTo(o2.getName());
}
});
List<Patient> list = new ArrayList<Patient>();
for (Patient p: list){
bst.insert(p);
}
bst.printTree();
}
}
患者档案:
public class Patient implements Comparable<Patient>{
private String patientName;
private String doctorName;
private String currAppt;
private String nextAppt;
Patient (String patientName,String doctorName ,String currAppt, String nextAppt)
{
this.patientName= patientName;
this.doctorName = doctorName;
this.currAppt = currAppt;
this.nextAppt= nextAppt;
}
public String getDoctor()
{return doctorName;}
public String getName()
{
return patientName;
}
public String toString()
{
return (patientName+" "+doctorName+" "+currAppt +" "+ nextAppt);
}
@Override
public int compareTo(Patient o) {
if (patientName.equals(o))
return 0;
return patientName.compareTo(o.patientName);
}
}
答案 0 :(得分:0)
问题在于这一部分:
List<Patient> list = new ArrayList<Patient>();
for (Patient p: list){
bst.insert(p);
}
将列表初始化为空数组列表,然后遍历空列表并添加任何患者。由于它是空的,因此该循环从不向BST添加任何内容。我猜你实际上是想在那里使用c1.patientList。所以:
List<Patient> list = new ArrayList<Patient>();
for (Patient p: c1.patientList){
bst.insert(p);
}