二分搜索树
用Java代码实现二分搜索树
public class BST<E extends Comparable<E>>{
}
1.创建内部类Node节点
private class Node{
public E e;
public Node left,right;
public Node(E e){
this.e = e;
left = null;
right = null;
}
}
2.基础属性,方法
private Node root;
private int size;
public BST(){
root = null;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
3.添加操作,递归写法
public void add(E e){
if(root == null){
root = new Node(e);
size++;
}else{
add(e,root);
}
}
private void add(E e,Node node){
if(e.compareTo(node.e)<0){
if(node.left==null){
node.left = new Node(e);
size ++;
return;
}else {
add(e,node.left);
}
} else if(e.compareTo(node.e)>0){
if(node.right==null){
node.right = new Node(e);
size ++;
return;
}else {
add(e,node.right);
}
} else{
return;
}
}
优化
public void add(E e){
root = add(e,root);
}
private Node add(E e,Node node){
if(node==null){
size++;
return new Node(e);
}
if(e.compareTo(node.e)<0)
node.left = add(e,node.left);
else if(e.compareTo(node.e)>0)
node.right = add(e,node.right);
return node;
}
查看是否包含E e
public boolean contains(E e){
return contains(e,root);
}
private boolean contains(E e,Node node){
if(node == null)
return false;
if(e.compareTo(node.e)==0)
return true;
else if(e.compareTo(node.e)<0)
return contains(e,node.left);
else
return contains(e,node.right);
}
前序遍历(先访问根节点,再访问左,后访问右)
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node){
if(node == null)
return;
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
中序遍历
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node){
if(node == null)
return;
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
后序遍历
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node){
if(node == null)
return;
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
先序遍历的非递归方法,采用栈
public void preOrderNR(){
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
Node cur= stack.pop();
System.out.println(cur.e);
if(cur.right!=null){
stack.push(cur.right);
}
if(cur.left!=null){
stack.push(cur.left);
}
}
}
层序遍历
public void levelOrder(){
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()){
Node cur = q.remove();
if(cur.left!=null){
q.add(cur.left);
}
if(cur.right!=null){
q.add(cur.right);
}
}
}
查找删除最大最小值
public E minmum(){
if(size==0)
throw new IllegalArgumentException("");
return minimum(root).e;
}
private Node minimum(Node node){
if(node.left==null)
return node;
return minimum(node.left);
}
public E maxmum(){
if(size==0)
throw new IllegalArgumentException("");
return maxmum(root).e;
}
private Node maxmum(Node node){
if(node.right==null)
return node;
return maxmum(node.right);
}
public E removeMin(){
E ret = minmum();
root = removeMin(root);
return ret;
}
private Node removeMin(Node node){
if(node.left==null){
Node r = node.right;
node.right = null;
size--;
return r;
}
node.left = removeMin(node.left);
return node;
}
public E removeMax(){
E ret = maxmum();
root = removeMax(root);
return ret;
}
private Node removeMax(Node node){
if(node.right==null){
Node r = node.left;
node.left = null;
size--;
return r;
}
node.right = removeMax(node.right);
return node;
}
删除指定节点(三种情况,左右子树都不为空的重点)
public void remove(E e){
remove(e,root);
}
private Node remove(E e,Node node){
if(node==null)
return null;
if(e.compareTo(node.e)==0){
if(node.left==null && node.right==null){
Node r = node.right;
node.right = null;
size--;
return r;
}
if(node.right==null){
Node l = node.left;
node.left = null;
size--;
return l;
}
Node successor = minimum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
node.left=node.left=null;
return successor;
}else if(e.compareTo(node.e)<0){
node.left = remove(e,node.left);
return node;
}else {
node.right = remove(e,node.right);
return node;
}
}