java实现二分搜索树

2018-11-24  本文已影响24人  Swen_9826
每隔段时间都要记得给自己充电!

二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),具有如下性质:

操作代码:

public class BST<E extends Comparable<E>> {
    private class Node{
        public E e;
        public Node left , right;
        
        public Node(E e){
            this.e = e;
            this.left = null;
            this.right = null;
        }
        
    } 
    
    private Node root;
    private int size;
    // 获取长度
    public int getSize(){
        return size;
    }
    // 判断是否为空
    public boolean isEmpty(){
        return size == 0;
    }   
    // 向二分搜索树中添加新的元素e
    public void add(E e){
        root = add(root , e);
    }   
    // 向以node为根的二分搜索树中插入元素e,递归算法
    // 返回插入新节点后二分搜索树的根
    private Node add(Node node, E e) {  
        
        if(node == null){
            size ++ ;
            node = new Node(e); 
            return node;
        }
        
        if(node.e.compareTo(e) < 0){            
            node.right = add(node.right , e);
        }else if(node.e.compareTo(e) > 0){
            node.left = add(node.left , e);
        }
        
        return node;
    }
    // 看二分搜索树中是否包含元素e
    public boolean contains(E e){
        return contains(root, e);
    }
    // 看二分搜索树中是否包含元素e    
    private boolean contains(Node node, E e){
        if(node == null){
            return false;
        }
        if(node.e.equals(e)){
            return true;
        }else if(node.e.compareTo(e)<0){            
            return contains(node.right , e);
        }else{
            return contains(node.left , 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 preOrderNR(){
        Stack<Node> stack = new Stack<Node>();
        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 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);
        System.out.println(node.e);
        postOrder(node.right);
    }    
    // 二分搜索树的层序遍历
    public void levelOrder(){
        Queue<Node> queue = new LinkedList<Node>();  
        queue.add(root);
        while(!queue.isEmpty()){
            Node cur = queue.remove();
            System.out.println(cur.e);
            if(cur.left != null){
                queue.add(cur.left);
            }
            if(cur.right != null){
                queue.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;
        }else{
            node = minimum(node.left);
        }
        return node;
    }    
    // 寻找二分搜索树的最大元素
    public E maximum(){
        if(size == 0)
            throw new IllegalArgumentException("BST is empty");

        return maximum(root).e;
    }
    // 返回以node为根的二分搜索树的最大值所在的节点
    private Node maximum(Node node){
        if(node.right == null){
            return node;
        }else{
            node = maximum(node.right);
        }            
        return node;
    }    
    // 从二分搜索树中删除最小值所在节点, 返回最小值
    public E removeMin(){
        E ret = minimum();
        root = removeMin(root);
        return ret;
    }    
    // 删除掉以node为根的二分搜索树中的最小节点
    // 返回删除节点后新的二分搜索树的根
    private Node removeMin(Node node){
        if(node.left == null){
            Node newNode = node.right;
            node.right = null;
            size -- ;
            return newNode;
        }
        
        node.left = removeMin(node.left);
        return node;
    }    
    // 从二分搜索树中删除最大值所在节点
    public E removeMax(){
        E ret = maximum();
        root = removeMax(root);
        return ret;
    }       
    // 删除掉以node为根的二分搜索树中的最大节点
    // 返回删除节点后新的二分搜索树的根
    private Node removeMax(Node node){
        if(node.right == null){
            Node newNode = node.left;
            node.left = null;
            size -- ;
            return newNode;
        }
        node.right = removeMax(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(node.e.compareTo(e)<0){
            node.right = remove(node.right,e);
            return node;
        }else if(node.e.compareTo(e)>0){
            node.left = remove(node.left,e);
            return node;
        }else{//node.e.equals(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 successorNode = minimum(node.right);           
            successorNode.right = removeMin(node.right);
            successorNode.left = node.left;
                                    
            node.left = node.right = null;
            
            return successorNode;
        }
    }
    
    // 重写toString方法
    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        
        generateBSTString(root , 0 , res);
        
        return res.toString();
    }
    
    private void generateBSTString(Node node,int depth,StringBuilder res) {
        
        if(node == null){
            res.append(generateDepthString(depth) + "null\n");
            return;
        }
        
        res.append(generateDepthString(depth) + node.e + "\n");
        generateBSTString(node.left , depth+1 , res);
        generateBSTString(node.right , depth+1 , res);
    }
    
    private String generateDepthString(int depth){
        StringBuilder res = new StringBuilder();
        
        for(int i=0;i<depth;i++){
            res.append("-");
        }
        return res.toString();
    }
}
上一篇下一篇

猜你喜欢

热点阅读