二叉查找树的实现

文章目录


前言

      用Java简单的实现二叉查找树。

//实现二叉查找树

import java.nio.BufferUnderflowException;
import java.util.ArrayDeque;

//T extends Comparable<T>   表示T类必须实现Comparable接口。
//T extends Comparable<? super T>   表示T类或者其父类必须实现Comparable接口。
//如果自定义一个 class NewInteger extends Integer,
// 那么Integer extends Comparable<Integer> 时, BinarySearchTree<NewInteger>=null;   fail!
// 而Integer extends Comparable<? super Integer>时,BinarySearchTree<NewInteger>=null;   pass! (因为Integer已经实现了Comparable接口。
public class BinarySearchTree<T extends Comparable<? super T>>{
    private static class BinaryNode<T> { //节点
        T element;
        BinaryNode<T> left;
        BinaryNode<T> right;
        public BinaryNode(T a){
            element=a;
            left=right=null;
        }
        public BinaryNode(T a, BinaryNode<T> le, BinaryNode<T> ri){
            element=a;
            left=le;
            right=ri;
        }
    }


    private BinaryNode<T> root;//root节点。
    public BinarySearchTree(){
        root=null;
    }
    public boolean isEmpty(){
        return root==null;
    }
    public void makeEmpty(){
        root=null;
    }

    public boolean contains(T element){
        return contains(element,root);
    }
    private boolean contains(T element,BinaryNode<T> root){
        if(root==null)
            return false;
        if(element.compareTo(root.element)==0)
            return true;
        if(element.compareTo(root.element)<0)
            return contains(element,root.left);
        else
            return contains(element,root.right);
    }

    public T findMin(){
        if(isEmpty())
            throw new BufferUnderflowException();
        return findMin(root);
    }
    private T findMin(BinaryNode<T> root){
        while(root.left!=null)
            root=root.left;
        return root.element;
    }

    public T findMax(){
        if(isEmpty())
            throw new BufferUnderflowException();
        return findMax(root);
    }
    private T findMax(BinaryNode<T> root){
        if(root.right==null)
            return root.element;
        return findMax(root.right);
    }

    public void insert(T element){
        root=insert(element,root);
    }
    private BinaryNode<T> insert(T element,BinaryNode<T> root){ //插入
        if(root==null)
            return new BinaryNode<T>(element);
        int compareRe=element.compareTo(root.element);
        if(compareRe<0)
            root.left=insert(element,root.left);
        else if(compareRe>0)
            root.right=insert(element,root.right);
        else
            ;  //遇到重复元素,什么也不做。 当然也可加上一个频率域表示某个元素出现的次数。
        return root;
    }

    public void remove(T element){
        root=remove(element,root);
    }
    private BinaryNode<T> remove(T element,BinaryNode<T> root){
        if(root==null)
            return root;
        int compareRe=element.compareTo(root.element);
        if(compareRe<0)
            root.left=remove(element,root.left);
        else if(compareRe>0)
            root.right=remove(element,root.right);
        else if(root.left!=null&&root.right!=null){//两个孩子
            root.element=findMax(root.left);//左侧最大或右侧最小
            root.left=remove(root.element,root.left);
        }
        else//一个孩子或没有孩子。
            root=(root.right==null)?root.left:root.right;
        return root;
    }
    public void printTree(){//分层打印树节点。
        if(root==null)
            return;
        ArrayDeque<BinaryNode<T>> queue=new ArrayDeque<>();
        queue.offerLast(root);

        int remainingNode=1,nextLayerN=0;
        while(!queue.isEmpty()){
            BinaryNode<T> tmp=queue.pollFirst();
            System.out.print(tmp.element+" ");
            remainingNode--;

            if(tmp.left!=null){
                queue.offerLast(tmp.left);
                nextLayerN++;
            }
            if(tmp.right!=null){
                queue.offerLast(tmp.right);
                nextLayerN++;
            }
            if(remainingNode==0){
                System.out.println();
                remainingNode=nextLayerN;
                nextLayerN=0;
            }
        }
    }
    public static void main(String[] args){
		//测试...
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值