BST student类 java

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;
	}
}	
	
	
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于二叉搜索树(Binary Search Tree,简称 BST)的排序算法,也称为二叉排序树排序或者BST排序,是一种利用二叉搜索树来进行排序的算法。BST排序算法的基本思路是将待排序的所有元素插入到一棵二叉搜索树中,然后按照中序遍历的顺序访问所有节点,就可以得到有序的序列。 下面是 Java 中基于 BST 排序的实现代码: ```java public class BSTSort { private class Node { private final int value; private Node left; private Node right; public Node(int value) { this.value = value; } public void insert(int value) { if (value < this.value) { if (left == null) { left = new Node(value); } else { left.insert(value); } } else { if (right == null) { right = new Node(value); } else { right.insert(value); } } } public List<Integer> inorderTraversal() { List<Integer> result = new ArrayList<>(); if (left != null) { result.addAll(left.inorderTraversal()); } result.add(value); if (right != null) { result.addAll(right.inorderTraversal()); } return result; } } public void sort(int[] array) { if (array == null || array.length == 0) { return; } Node root = new Node(array[0]); for (int i = 1; i < array.length; i++) { root.insert(array[i]); } List<Integer> sortedList = root.inorderTraversal(); for (int i = 0; i < sortedList.size(); i++) { array[i] = sortedList.get(i); } } } ``` 在上面的代码中,我们定义了一个 BSTSort ,其中定义了一个内部 Node 用来表示二叉搜索树的节点。在 insert() 方法中,我们首先比较要插入的元素和当前节点的值的大小关系,如果要插入的元素小于当前节点的值,就递归调用左子树的 insert() 方法;否则递归调用右子树的 insert() 方法。在 inorderTraversal() 方法中,我们采用中序遍历的方式遍历整棵树,将遍历的结果存储在一个 List 中,并返回这个 List。 在 sort() 方法中,我们首先创建了一个节点值为数组第一个元素的二叉搜索树,然后依次将数组中的其他元素插入到这个树中。最后,我们调用根节点的 inorderTraversal() 方法,得到排序后的结果,并将其赋值给原数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值