简单的AVL 实现

在上篇 排序二叉树实现的基础上,增加自动平衡功能。

package TreeDemos;

import java.util.ArrayList;

public class BalancedTree<K extends Comparable<K>,V> {
//    在排序二叉树的基础上实现自动平衡
    public static class Node<K extends Comparable<K>, V> {
        public K key;
        public V value;
        public Node<K, V> lchlid;
        public Node<K, V> rchlid;
        public Integer BI=0;
        public Integer height=0;
    
        public Node(K key, V value, Node<K, V> lchlid, Node<K, V> rchlid) {
            this.key = key;
            this.value = value;
            this.lchlid = lchlid;
            this.rchlid = rchlid;
        }

        public Node() {
        }

        public void rightBalance(Node<K,V> node,Node<K,V> newNode){
            if(node.BI<-1){
                if (newNode.key.compareTo(node.rchlid.key) > 0){
                    leftRotate(node);
                }
                else if(newNode.key.compareTo(node.rchlid.key) < 0){
                    rightRotate(node.rchlid);
                    leftRotate(node);
                }
            }
        }
        public void leftBalance(Node<K,V> node,Node<K,V> newNode){
            if(node.BI>1){
                if (newNode.key.compareTo(node.lchlid.key) < 0){
                    rightRotate(node);
                }
                else if(newNode.key.compareTo(node.lchlid.key) > 0){
                    leftRotate(node.lchlid);
                    rightRotate(node);
                }
            }
        }

        public void put(Node<K, V> newNode) {
            if (newNode.key.compareTo(this.key) < 0) {
                if (lchlid != null) {
                    lchlid.put(newNode);
                    this.getHeight();
                    this.getBI();
                    System.out.println("r"+this.key+":"+this.height+":"+this.BI);
                    leftBalance(this,newNode);
                } else {
                    lchlid = newNode;
                    lchlid.height+=1;
                    this.getHeight();
                    this.getBI();
//                    leftBalance(this,newNode);
                }

            } else if (newNode.key.compareTo(this.key) > 0) {
                if (rchlid != null) {
                    rchlid.put(newNode);
                    this.getHeight();
                    this.getBI();;
                    System.out.println("l"+this.key+":"+this.height+":"+this.BI);
                    rightBalance(this,newNode);
                } else {
                    rchlid = newNode;
                    rchlid.height+=1;
                    this.getHeight();
                    this.getBI();
//                    rightBalance(this,newNode);
                }
            }
        }

        public void getHeight() {
            if(this.lchlid!=null && this.rchlid!=null){
                this.height=(Math.max(this.lchlid.height,this.rchlid.height))+1;
            }
            else if(this.lchlid==null && this.rchlid==null){
                this.height=1;
            }
            else {
                this.height= this.lchlid!=null?this.lchlid.height+1:this.rchlid.height+1;
            }
        }

        public void getBI(){
            if(this.lchlid!=null && this.rchlid!=null){
                this.BI=-this.rchlid.height+this.lchlid.height;
            }
            else if(this.lchlid==null && this.rchlid==null){
                this.BI=0;
            }
            else {
                this.BI= this.lchlid!=null?this.lchlid.height:-this.rchlid.height;
            }
        }



    public Node<K, V> find(K key) {
            if (key.compareTo(this.key) < 0 && lchlid != null) {
                return lchlid.find(key);
            } else if (key.compareTo(this.key) > 0 && rchlid != null) {
                return rchlid.find(key);
            } else if (key.compareTo(this.key) == 0) {
                return this;
            } else {
                return null;
            }
        }
    
        public void delete(K key, Node<K, V> parent, Node<K, V> root) {
            if (key.compareTo(this.key) < 0 && lchlid != null) {
                lchlid.delete(key, this ,root);
            } else if (key.compareTo(this.key) > 0 && rchlid != null) {
                rchlid.delete(key, this,root);
            } else if (key.compareTo(this.key) == 0) {
                delete1(parent,root);
            } else {
                System.out.println("此节点不存在");
            }
        }
    
        public Node<K,V> getPre(){
            Node<K,V> l=lchlid;
            while (l.rchlid!=null){
                l=l.rchlid;
            }
            return l;
        }
    
        public void delete1(Node<K, V> parent, Node<K, V> root){
            //        分为三种情况  第一种:叶子节点,直接删除; 第二种:只含有一个子节点,继承 ,第三种:含有两个节点
            if (lchlid == null && rchlid == null) {
                if (parent.lchlid == this) {
                    parent.lchlid = null;
                }
                if (parent.rchlid == this) {
                    parent.rchlid = null;
                }
            } else if (lchlid != null && rchlid == null) {
                value = lchlid.value;
                this.key=lchlid.key;
                lchlid = null;
            } else if (lchlid == null) {
                value = rchlid.value;
                this.key=rchlid.key;
                rchlid = null;
            } else {
                Node<K,V> pre=getPre();
                root.delete(pre.key,null,root);
                this.value=pre.value;
                this.key=pre.key;
            }
        }
        
        public void leftRotate(Node<K,V> node){
            Node<K,V> temp=new Node<K,V>();
            temp.value=node.value;
            temp.key=node.key;
            temp.rchlid=node.rchlid.lchlid;
            temp.lchlid=node.lchlid;

            temp.getHeight();
            temp.getBI();

            node.value=node.rchlid.value;
            node.key=node.rchlid.key;
            node.rchlid=node.rchlid.rchlid;
            node.lchlid=temp;
            node.getHeight();
            node.getBI();
            System.out.println("lt");
            
        }
        
        public void rightRotate(Node<K,V> node){
            Node<K,V> temp=new Node<K,V>();
            temp.value=node.value;
            temp.key=node.key;
            temp.lchlid=node.lchlid.rchlid;
            temp.rchlid=node.rchlid;
            temp.getHeight();
            temp.getBI();

            node.value=node.lchlid.value;
            node.key=node.lchlid.key;
            node.lchlid=node.lchlid.lchlid;
            node.rchlid=temp;
            node.getHeight();
            node.getBI();
            System.out.println("rt");
        }

        public String toString() {
            return "[" + key + ":" + value + ":"+BI+":"+height+"]";
        }
        
        public String toString2() {
            StringBuilder stringBuilder = new StringBuilder();
            if (lchlid != null) {
                stringBuilder.append(lchlid.toString2());
            }
            stringBuilder.append(this);
            if (rchlid != null) {
                stringBuilder.append(rchlid.toString2());
            }
            return stringBuilder.toString();
        }
    }
    public Node<K,V> root;


    public BalancedTree(ArrayList<Node<K,V>> nodes){
        for(Node<K,V> node :nodes){
            put(node);
        }
    }

    public void put(Node<K,V> node){
        if(root==null){
            root=node;
            root.height+=1;
        }
        else{
            root.put(node);
        }
    }

    public Node<K,V> find(K key){
        return root.find(key);
    }


    public void delete(K key){
        root.delete(key,null,root);

    }

    public String toString(){
//        中序遍历
        return root.toString2();
    }


    public static void main(String[] args) {
        ArrayList<Node<Integer,String>> nodes=new ArrayList<Node<Integer,String>>();
        nodes.add(new Node<Integer,String>(3,"A",null,null));
        nodes.add(new Node<Integer,String>(2,"B",null,null));
        nodes.add(new Node<Integer,String>(1,"J",null,null));
        nodes.add(new Node<Integer,String>(4,"F",null,null));
        nodes.add(new Node<Integer,String>(5,"G",null,null));
        nodes.add(new Node<Integer,String>(6,"K",null,null));
        nodes.add(new Node<Integer,String>(7,"K",null,null));
        nodes.add(new Node<Integer,String>(10,"K",null,null));
        nodes.add(new Node<Integer,String>(9,"K",null,null));
        nodes.add(new Node<Integer,String>(8,"K",null,null));

        BalancedTree<Integer,String> myTree=new BalancedTree<>(nodes);
        System.out.println(myTree);

        Node<Integer,String> res= myTree.find(56);
//        System.out.println(res.getString());

        myTree.delete(56);
        System.out.println(myTree);
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值