Java 二叉搜索树迭代非键
我有一个难题。我需要打印BST中所有不是键的值。因此,由于树不是根据这些值排序的,所以我不能像以前使用BST时那样进行排序。我只需要查看树上的每个节点,将非键值与我输入的值进行比较,然后确定是否打印它 即学生目录,我需要在其中打印2.0以上的所有GPA。由于树是按学生ID排序的,而不是按GPA排序的,我如何遍历每个节点,比较GPA并打印所有高于2.0的节点 如果你需要看一下我的代码,整个东西都在这里,它是巨大的Java 二叉搜索树迭代非键,java,printing,binary-search-tree,Java,Printing,Binary Search Tree,我有一个难题。我需要打印BST中所有不是键的值。因此,由于树不是根据这些值排序的,所以我不能像以前使用BST时那样进行排序。我只需要查看树上的每个节点,将非键值与我输入的值进行比较,然后确定是否打印它 即学生目录,我需要在其中打印2.0以上的所有GPA。由于树是按学生ID排序的,而不是按GPA排序的,我如何遍历每个节点,比较GPA并打印所有高于2.0的节点 如果你需要看一下我的代码,整个东西都在这里,它是巨大的 public class StudentBST { private static N
public class StudentBST
{
private static Node root;
static class Node
{
public int studentID;
public String lastName;
public String firstName;
public String major;
public double gpa;
public Node left, right;
public int minValue()
{
if(left == null)
{
return studentID;
}
else
{
return left.minValue();
}
}
public boolean remove(int i, Node node)
{
if(i < this.studentID)
{
if(left != null)
{
return left.remove(i, this);
}
else
{
return false;
}
}
else if(i > this.studentID)
{
if(right != null)
{
return right.remove(i, this);
}
else
{
return false;
}
}
else
{
if(left != null && right != null)
{
this.studentID = right.minValue();
right.remove(this.studentID, this);
}
else if(node.left == this)
{
node.left = (left != null) ? left : right;
}
else if(node.right == this)
{
node.right = (left != null) ? left : right;
}
return true;
}
}
public Node(int i, String l, String f, String m, double g)
{
studentID = i;
lastName = l;
firstName = f;
major = m;
gpa = g;
left = null;
right = null;
}
}
public StudentBST()
{
root = null;
}
private static void insert(int i, String l, String f, String m, double g)
{
root = insert(root, i, l, f, m , g);
}
private static Node insert(Node node, int i, String l, String f, String m, double g)
{
if(node == null)
{
node = new Node(i, l, f, m, g);
}
else
{
if(i <= node.studentID)
{
node.left = insert(node.left, i, l, f, m, g);
}
else
{
node.right = insert(node.right, i, l, f, m, g);
}
}
return(node);
}
public static void printBST()
{
printBST(root);
System.out.println();
}
private static void printBST(Node node)
{
if(node == null)
{
return;
}
printBST(node.left);
System.out.println(node.studentID + ", " + node.lastName + ", " + node.firstName
+ ", " + node.major + ", " + node.gpa);
printBST(node.right);
}
public static boolean remove(int i)
{
if(root == null)
{
return false;
}
else
{
if(root.studentID == i)
{
Node auxRoot = new Node(0, "", "", "", 0);
auxRoot.left = root;
boolean result = root.remove(i, auxRoot);
root = auxRoot.left;
return result;
}
else
{
return root.remove(i, null);
}
}
}
public static void main(String[] args)
{
StudentBST.insert(8, "Costanza", "George", "Napping", 1.60);
StudentBST.insert(10, "Kramer", "Cosmo", "Chemistry", 3.04);
StudentBST.insert(5, "Seinfeld", "Jerry", "Theater", 2.05);
StudentBST.printBST();
Scanner input = new Scanner(System.in);
int option = 9;
while(option != 0)
{
System.out.println("1 - Add new student 2 - Delete student 3 - Print All" +
" 0 - Exit");
option = input.nextInt();
if(option == 1)
{
System.out.println("Enter student ID");
int i = input.nextInt();
input.nextLine();
System.out.println("Enter Last Name");
String l = input.nextLine();
System.out.println("Enter First Name");
String f = input.nextLine();
System.out.println("Enter major");
String m = input.nextLine();
System.out.println("Enter GPA");
Double g = input.nextDouble();
System.out.println("Inserted student record");
StudentBST.insert(i, l, f, m, g);
}
if(option == 2)
{
System.out.println("Enter Student ID to delete");
int i = input.nextInt();
boolean b = StudentBST.remove(i);
if(b)
{
System.out.println("Deletion completed");
}
else
{
System.out.println("Deletion encountered error");
}
}
if(option == 3)
{
StudentBST.printBST();
}
}
}
公共班级学生BST
{
私有静态节点根;
静态类节点
{
公共int学生;
公共字符串lastName;
公共字符串名;
公共弦专业;
公共双gpa;
公共节点左、右;
公共int minValue()
{
if(left==null)
{
返回学生ID;
}
其他的
{
返回left.minValue();
}
}
公共布尔删除(int i,Node)
{
如果(ithis.studentID)
{
if(右!=null)
{
返回权。删除(i,本);
}
其他的
{
返回false;
}
}
其他的
{
if(左!=null和右!=null)
{
this.studentID=right.minValue();
对。删除(this.studentID,this);
}
else if(node.left==此)
{
node.left=(left!=null)?left:右侧;
}
else if(node.right==此)
{
node.right=(左!=null)?左:右;
}
返回true;
}
}
公共节点(int i、字符串l、字符串f、字符串m、双g)
{
studentID=i;
lastName=l;
firstName=f;
主修=m;
gpa=g;
左=空;
右=空;
}
}
公立学生
{
root=null;
}
私有静态void插入(int i、字符串l、字符串f、字符串m、双g)
{
根=插入(根,i,l,f,m,g);
}
私有静态节点插入(节点节点,int i,字符串l,字符串f,字符串m,双g)
{
if(node==null)
{
节点=新节点(i、l、f、m、g);
}
其他的
{
如果(i我认为您的想法是正确的:只需遍历整个树并打印出高于某个阈值的GPA。粗略实现如下所示:
public void printGPAs(Node node, double gpa_cutoff) {
if (node == null) {
return;
}
if (node.gpa >= gpa_cutoff) {
System.out.println(node.gpa);
}
printGPAs(node.left);
printGPAs(node.right);
}
如果要按特定顺序打印,最简单的方法是在打印过程中将它们放入列表中,插入正确的位置以保持所需的顺序。没问题——听起来你只是被以“非标准”方式处理树的想法弄糊涂了