数据结构与算法--Size balance树实现有序表

public class Code_SizeBalancedTreeMap {
    public static class SBTNode<K extends Comparable<K>, V>{
        public K key;
        public V value;
        public SBTNode<K, V> l;
        public SBTNode<K, V> r;
        public int size;

        public SBTNode(K key, V value){
            this.key = key;
            this.value = value;
            size = 1;
        }
    }

    public static class SizeBalanceTreeMap<K extends Comparable<K>, V>{
        private SBTNode<K,V> root;

        // 右旋
        private SBTNode<K,V> rightRotate(SBTNode<K,V> cur){
            SBTNode<K,V> leftNode = cur.l;
            cur.l = leftNode.r;
            leftNode.r = cur;
            leftNode.size = cur.size;
            cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
            return leftNode;
        }

        // 左旋
        private SBTNode<K,V> leftRotate(SBTNode<K,V> cur){
            SBTNode<K,V> rightNode = cur.r;
            cur.r = rightNode.l;
            rightNode.l = cur;
            rightNode.size = cur.size;
            cur.size = (cur.l != null ? cur.l.size : 0) + (cur.r != null ? cur.r.size : 0) + 1;
            return rightNode;
        }

        private SBTNode<K, V> maintain(SBTNode<K, V> cur) {
            if (cur == null){
                return null;
            }

            int leftSize = cur.l != null ? cur.l.size : 0;
            int leftLeftSize = cur.l != null && cur.l.l != null ? cur.l.l.size : 0;
            int leftRightSize = cur.l != null && cur.l.r != null ? cur.l.r.size : 0;
            int rightSize = cur.r != null ? cur.r.size : 0;
            int rightLeftSize = cur.r != null && cur.r.l != null ? cur.r.l.size : 0;
            int rightRightSize = cur.r != null && cur.r.r != null ? cur.r.r.size : 0;

            if (leftLeftSize > rightSize){
                cur = rightRotate(cur);
                cur.r = maintain(cur.r);
                cur = maintain(cur);
            }else if(leftRightSize > rightSize){
                cur.l = leftRotate(cur.l);
                cur = rightRotate(cur);
                cur.l = maintain(cur.l);
                cur.r = maintain(cur.r);
                cur = maintain(cur);
            }else if(rightRightSize > leftSize){
                cur = leftRotate(cur);
                cur.l = maintain(cur.l);
                cur = maintain(cur);
            }else if(rightLeftSize > leftSize){
                cur.r = rightRotate(cur.r);
                cur = leftRotate(cur);
                cur.l = maintain(cur.l);
                cur.r = maintain(cur.r);
                cur = maintain(cur);
            }

            return cur;
        }

        // 现在,以cur为头的树上,新增,加(key, value)这样的记录
        // 加完之后,会对cur做检查,该调整调整
        // 返回,调整完之后,整棵树的新头部
        private SBTNode<K, V> add(SBTNode<K, V> cur, K key, V value) {
            if (cur == null){
                return new SBTNode<>(key,value);
            }else{
                cur.size++;
                if(key.compareTo(cur.key) < 0){
                    cur.l = add(cur.l,key,value);
                }else{
                    cur.r = add(cur.r,key,value);
                }
                return maintain(cur);
            }
        }

        // 在cur这棵树上,删掉key所代表的节点
        // 返回cur这棵树的新头部
        private SBTNode<K, V> delete(SBTNode<K, V> cur, K key) {
            cur.size --;
            if (key.compareTo(cur.key) > 0) {
                cur.r = delete(cur.r, key);
            } else if (key.compareTo(cur.key) < 0) {
                cur.l = delete(cur.l, key);
            } else { // 当前要删掉cur
               if(cur.l == null && cur.r == null){
                   cur = null;
               }else if(cur.l == null && cur.r != null){
                   cur = cur.r;
               }else if(cur.l != null && cur.r == null){
                   cur = cur.l;
               }else{
                   SBTNode<K,V> pre = null;
                   SBTNode<K,V> des = cur.r;
                   des.size--;
                   while (des.l != null){
                       pre = des;
                       des = des.l;
                       des.size--;
                   }

                   if(pre != null){
                       pre.l = des.r;
                       des.r = cur.r;
                   }
                   des.l = cur.l;
                   des.size = des.l.size + (des.r == null ? 0 : des.r.size) + 1;
                   cur = des;
               }
            }

            return cur;
        }

        private SBTNode<K, V> findLastIndex(K key) {
            SBTNode<K,V> pre = root;
            SBTNode<K,V> cur = root;
            while (cur != null){
                pre = cur;
                if(key.compareTo(cur.key) == 0){
                    break;
                }else if(key.compareTo(cur.key) < 0){
                    cur = cur.l;
                }else{
                    cur = cur.r;
                }
            }
            return pre;
        }

        // (key,value) put -> 有序表 新增、改value
        public void put(K key, V value) {
            if (key == null) {
                throw new RuntimeException("invalid parameter.");
            }
            SBTNode<K,V> lastNode = findLastIndex(key);
            if(lastNode != null && lastNode.key.compareTo(key) == 0){
                lastNode.value = value;
            }else{
                root = add(root,key,value);
            }
        }
    }






}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值