1. Implement a BST of students using Insert function. The information of a student includes id, name, and major.
2. Search the BST given the name of a student.
3. Find the successor/predecessor of a given student node.
public class BTS {
public static void main(String[] args) {
//implement a BST of student using Insert function
//The information of a student include id,name,major
Student Li = new Student(03,"LiLei","computer");
Student Han = new Student(01,"HanMei","chemistry");
Student Jerry = new Student(02,"Jerry","history");
Student Mickey = new Student(04,"Mickey","biochemistry");
Student Minnie = new Student(05,"Minnie","physics");
Student Tom = new Student(06,"Tom","biology");
BTreeNode node1 = new BTreeNode(Han);
BTreeNode node2 = new BTreeNode(Jerry);
BTreeNode node3 = new BTreeNode(Li);
BTreeNode node4 = new BTreeNode(Mickey);
BTreeNode node5 = new BTreeNode(Minnie);
BTreeNode node6 = new BTreeNode(Tom);
BST tree = new BST(node3);
tree.Tree_Insert(node2);
tree.Tree_Insert(node1);
tree.Tree_Insert(node4);
tree.Tree_Insert(node5);
tree.Tree_Insert(node6);
//Search the BST given the name of a student
tree.inOrder(node3);
System.out.println(tree.search("Tom").key.name);
//Find the successor/predecessor of a given student node
System.out.println(tree.Tree_Successor(node3).key);
}
}
class Student implements Comparable<Student>{
int id;
String name;
String major;
public Student(int id,String name,String major) {
this.id = id;
this.major = major;
this.name = name;
}
String getName() {
return name;
}
public String toString() {
return name +" "+ id +" "+ major;
}
public int compareTo(Student s) {
return this.id - s.id;
}
}
class BTreeNode{
Student key;
BTreeNode left;
BTreeNode right;
BTreeNode parent;
public BTreeNode(Student key){
this.key = key;
left = null;
right = null;
parent = null;
}
}
class BST{
BTreeNode root;
public BST(BTreeNode root) {
this.root = root;
}
void Tree_Insert(BTreeNode z) {
BTreeNode y = null;
BTreeNode x = root;
while(root == null)
root = z; //while the tree is empty, z is the root
while(x != null) {
y = x;
if(z.key.compareTo(x.key) < 0) {
x = x.left;
}else x = x.right;
}
z.parent = y;
if(z.key.compareTo(y.key) < 0)
y.left = z;
else y.right = z;
}
void inOrder(BTreeNode r) {
if(r == null)
return ;
inOrder(r.left);
System.out.println(r.key);
inOrder(r.right);
}
Student getKey(BTreeNode a) {
return a.key;
}
BTreeNode search(String name) {
BTreeNode cur = root;
while(cur != null && !cur.key.getName().equals(name)){
if(cur.key.getName().compareTo(name) < 0) {
cur = cur.right;
}else {
cur = cur.left;
}
}
return cur;
}
BTreeNode Tree_Mininum(BTreeNode r) {
if(r != null || r.left == null)
return r;
return Tree_Mininum(r.left);
}
BTreeNode Tree_Successor(Student a) {
BTreeNode b = new BTreeNode(a);
return Tree_Successor(b);
}
BTreeNode Tree_Successor(BTreeNode x){
if(x.right != null)
return Tree_Mininum(x.left);
BTreeNode y = x.parent;
while(y != null && x == y.right) {
x = y;
y = y.parent;
}
return y;
}
BTreeNode Tree_Predecessor(BTreeNode x) {
if(x.left != null)
return Tree_Mininum(x.right);
BTreeNode y = x.parent;
while(y != null && x == y.left) {
x = y;
y = y.parent;
}
return y;
}
}