二分搜索树的java实现

Posted lwb444

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分搜索树的java实现相关的知识,希望对你有一定的参考价值。

 递归理解起来还是有点难,弄清楚搞了不短的时间

package com.puple.atto.datastructure;

import java.util.LinkedList;
import java.util.Queue;

public class BST<E extends Comparable<E>> {
private class Node{
public E e;
public Node left,right;

public Node(E e){
this.e=e;
left=null;
right=null;
}

public Node(E e,Node left,Node right){
this.e=e;
this.left=left;
this.right=right;
}

@Override
public String toString() {
return ""+this.e;
// return "value:"+this.e+",left:"+this.left.e+",rigth:"+this.right.e;
}
}

private Node root;
private int size;

public BST(){
root=null;
size=0;
}

public int size(){
return size;
}

public boolean isEmpty(){
return size==0;
}

private Node add(Node node,E e){
if (node==null){
size++;
return new Node(e);
}
if (e.compareTo(node.e)<0){
node.left=add(node.left,e);
}
else if(e.compareTo(node.e)>0){
node.right=add(node.right,e);
}
return node;
}

public void add(E e){
root=add(root,e);
}


private boolean contains(Node node,E e){
if (node==null){
return false;
}
if(e.compareTo(node.e)==0){
return true;
}
else if(e.compareTo(node.e)<0){
return contains(node.left,e);
}
else{
return contains(node.right,e);
}
}

public boolean contains(E e){
return contains(root,e);
}


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.right);
System.out.println(node.e);
postOrder(node.left);
}

public void levelOrder(){
Queue<Node> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
Node cur=q.remove();
System.out.println(cur.e);
if (cur.left!=null){
q.add(cur.left);
}
if (cur.right!=null){
q.add(cur.right);
}
}
}

public E minimum(){
if(size==0){
throw new IllegalArgumentException("BST is empty");
}
return minimum(root).e;
}

private Node minimum(Node node){
if(node.left==null){
return node;
}
return minimum(node.left);
}

public E maximum(){
if(size==0){
throw new IllegalArgumentException("BST is empty.");
}
return maximum(root).e;
}

private Node maximum(Node node){
if (node.right==null){
return node;
}
return maximum(node.right);
}


public E removeMin(){
E ret=minimum();
root=removeMin(root);
return ret;
}

private Node removeMin(Node node){

if(node.left == null){
Node rightNode = node.right;
node.right = null;
size --;
return rightNode;
}
node.left=removeMin(node.left);
return node;
}


public E removeMax(){
E ret=maximum();
root=removeMax(root);
return ret;
}

private Node removeMax(Node node){

if(node.right == null){
Node leftNode = node.left;
node.left = null;
size --;
return leftNode;
}
node.right=removeMin(node.right);
return node;
}


// 从二分搜索树中删除元素为e的节点
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.compareTo(node.e) == 0

// 待删除节点左子树为空的情况
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;

return successor;
}
}






public static void main(String[] args) {
BST bst=new BST();
bst.add(1);
bst.add(8);

bst.add(-1);
bst.add(9);
bst.add(-5);
bst.add(-10);
bst.add(-3);
bst.add(17);
bst.add(100);
bst.add(-75);
bst.add(-90);
bst.add(-80);
bst.add(120);
// System.out.println(bst.root.e);
// System.out.println(bst.contains(9));
// bst.preOrder();
// bst.inOrder();
// bst.postOrder();

}
}

以上是关于二分搜索树的java实现的主要内容,如果未能解决你的问题,请参考以下文章

学习数据结构笔记(11) --- [二分搜索树(BinarySearchtTree)]

实现二分搜索树

数据结构 --- [二分搜索树 (Java)]

数据结构-PHP 二分搜索树的层序遍历(队列实现)

十九 二分搜索树的广度优先遍历

13-自平衡二分搜索树 AVLTree