整个二叉排序树最难的点也就是删除操作,你看懂我的删除,二叉排序树你就完全了解了。具体有难度的地方已在文中注释
Binary Search Tree:
/**
* @author Jay
* @date 2020/7/10 10:22
* @Description:
*/
public class BST <E extends Comparable<E>> {
private class Node{
private E e;
private Node left,right;
public Node(E e){
this.e=e;
left=null;
right=null;
}
}
private Node root;
private int size;
public BST(){
root=null;
size=0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public void add(E e){
add(root ,e);
// ++size;
}
private Node add(Node root,E e) {
if (root == null) {
++size;
return new Node(e);
}
//通过compareTo方法,e的值比根节点小会返回-1
if (e.compareTo(root.e)<0)
root.left=add(root.left, e);
else if (e.compareTo(root.e)>0)
root.right=add(root.right,e);
return root;
}
public boolean contains(E e){
return contains(root,e);
}
public boolean contains(Node node,E e){
if (root==null)return false;
if (e.equals(root.e))return true;
else if (e.compareTo(root.e)<0) return contains(root.left,e);
else return contains(root.right,e);
}
//前序遍历
public void preOrder(){
preOrder(root);
}
private void preOrder(Node root){
if (root==null)return ;
System.out.println(root.e);
preOrder(root.left);
preOrder(root.right);
}
//后序遍历
public void postOrder(){
postOrder(root);
}
private void postOrder(Node root){
if (root==null)return ;
preOrder(root.left);
preOrder(root.right);
System.out.println(root.e);
}
//二叉树的中序遍历
public void inOrder() {
inOrder(root);
}
private void inOrder(Node root) {
if (root == null) return;
inOrder(root.left);
System.out.println(root.e);
inOrder(root.right);
}
//非递归形式前序遍历
public void preOrderNR(){
LinkStack<Node> stack=new LinkStack<>();
stack.push(root);
while(!stack.isEmpty()){
Node cur=stack.pop();
System.out.println(cur.e);
if (cur.right!=null)stack.push(root.right);
if (cur.left!=null)stack.push(root.left);
}
}
public void BFS(){
LoopQueue<Node> queue=new LoopQueue<>();
queue.enqueue(root);
while (!queue.isEmpty()){
Node cur=queue.dequeue();
System.out.println(cur);
if (cur.left!=null)queue.enqueue(cur.left);
if (cur.right!=null)queue.enqueue(cur.right);
}
}
//返回最小值,递归
public E minimum(){
if (size==0)throw new IllegalArgumentException("BST is empty!");
return minimum(root).e;
}
public Node minimum(Node node){
if (node.left==null)return node;
return minimum(node);
}
//返回最小值,非递归
public E minimumNR(){
if (size == 0)
throw new IllegalArgumentException("BST is empty!");
Node ans=root;
while (ans.left!=null)ans=ans.left;
return ans.e;
}
//返回最大值
public E maximum(){
if (size==0)throw new IllegalArgumentException("BST is empty!");
return maximum(root).e;
}
public Node maximum(Node node){
if (node==null)return node;
return maximum(node);
}
//返回最大值,非递归
public E maximumNR(){
if (size==0)throw new IllegalArgumentException("BST is empty!");
Node ans=root;
while (ans!=null)ans=ans.right;
return ans.e;
}
//删除二分搜索树的最小结点并返回其值
public E removeMin(){
E ret=minimum();
root=removeMin(root);
return ret;
}
public Node removeMin(Node node) {
if (node.left==null){
Node rightNode=node.right;
node.right=null;
--size;
return rightNode;//递归后这里的值赋回下面那句node.left
}
node.left=removeMin(node.left);
return node;//返回删除后的新子树
}
public E removeMax(){
E ret=maximum();
root=removeMax(root);
return ret;
}
public Node removeMax(Node node) {
if (node.right==null){
Node leftNode=node.left;
node.left=null;
--size;
return leftNode;//递归后这里的值赋回下面那句node.right
}
node.right=removeMax(node.right);
return node;//返回删除后的新子树
}
public void remove(E e){
root=remove(root,e);
}
//删除以node为根的二分搜索树中值为e的结点,递归算法
//返回删除结点后新的二分搜索树的根
private Node remove(Node node, E e) {
if (node == null) return null;
if (e.compareTo(node.e) < 0) {
node.left = remove(node.left, e);
return node;
}
else if (e.compareTo(node.e) < 0) {
node.right = remove(node.right, e);
return node;
}
else { //e == node.e
if (node.left == null) { //待删除结点左子树为根的情况
Node rightNode = node.right;
node.right = null;
--size;
return rightNode;
}
if (node.right == null) {
Node leftNode = node.left;
node.left = null;
--size;
return leftNode;
}
//找到右子树最小的节点,也可以使用左子树最大节点
Node successor = minimum(node.right);
//为其接上删除右子树最大节点后的新子树
successor.right = removeMin(node.right);
//为其接上左子树
successor.left = node.left;
node.left = node.right = null;
//这里不用写--size;因为已经在removeMin中减少了size
return successor;
}
}
}