8.树的进阶=平衡树2-3树红黑树B树B+树

目录

一、平衡树与2-3树。 

二、红黑树。

三、B树、B+树。


 

一、平衡树与2-3树。 

0b1c98ab4185468a93ede769d91b07ae.png

二、红黑树。

f38ed91777604195ba99a554bd07b3ff.png

package 树的进阶.红黑树;

public class RedBlackTree <Key extends Comparable<Key>,Value>{
    //根结点
    private Node root;
    //记录树中元素的个数
    private int N;
    //红色链接
    private static final boolean RED = true;
    //黑色链接
    private static final boolean BLACK = false;

    //结点类
    private class Node{
        //存储键
        public Key key;
        //存储值
        public Value value;
        //记录左子结点
        public Node left;
        //记录右子结点
        public Node right;
        //由其父结点指向它的链接的颜色
        public boolean color;

        public Node(Key key, Value value, Node left, Node right, boolean color) {
            this.key = key;
            this.value = value;
            this.left = left;
            this.right = right;
            this.color = color;
        }
    }

    //获取树中元素的个数
    public int size(){
        return N;
    }

    /**
     * 判断当前结点的父结点指向链接是否为红色
     */
    private boolean isRed(Node x){
        if (x == null){
            return false;
        }
        return x.color == RED;
    }

    /**
     * 左旋转
     */
    private Node rotateLeft(Node h){
        //1.获取h结点的右子结点,表示为x
        Node x = h.right;
        //2.让x结点的左子结点称为h结点的右子结点
        h.right = x.left;
        //3.让h成为x结点的左子结点
        x.left = h;
        //4.让x结点的color属性等于h结点的color属性
        x.color = h.color;
        //5.让h结点的color属性变为红色
        h.color = RED;
        return x;//x已经替代了h这个结点
    }

    /**
     * 右旋
     */
    private Node rotateRight(Node h){
        //1.获取h结点的左子结点,标识为x
        Node x = h.left;
        //2.让x结点的右子结点成为h结点的左子结点
        h.left = x.right;
        //3.让h结点成为x结点的右子结点
        x.right = h;
        //4.让x结点的color属性等于h结点的color属性
        // (如果父节点指向的子结点color是红色,那么就是红链接,这里导致父子结点交换,所以要把原来的父节点颜色赋值给现在的父节点,原本的子结点是红色,所以现在的子结点也要是红色)
        x.color = h.color;
        //5.让h结点的color属性为红色
        h.color = RED;
        return x;
    }

    /**
     * 颜色反转,相当于完成拆分4-结点
     */
    private void flipColors(Node h){
        //当前结点变为红色
        h.color = RED;
        //左子结点和右子结点变为黑色
        h.left.color = BLACK;
        h.right.color = BLACK;
    }

    /**
     * 在整个树上完成插入操作
     */
    public void put(Key key,Value value){
        root = put(root,key,value);
        //根结点的颜色总是黑色
        root.color = BLACK;
    }

    /**
     * 在指定树中,完成插入操作,并返回添加元素后新的树
     */
    private Node put(Node h, Key key, Value val) {
        //判断是否为空,如果为空则直接返回一个红色的结点就可以了
        if (h == null){
            //数量+1
            N++;
            return new Node(key,val,null,null,RED);
        }
        //比较h结点的键和key的大小
        int cmp = key.compareTo(h.key);
        if (cmp < 0){
            //继续往左
            h.left = put(h.left,key,val);
        }else if (cmp > 0){
            //继续往右
            h.right = put(h.right,key,val);
        }else {
            //发生值的替换
            h.value = val;
        }
        //进行左旋:当当前结点h的左子结点为黑色,右子结点为红色,需要左旋
        if (isRed(h.right) && !isRed(h.left)){
            //左右子结点为null则为false
            h = rotateLeft(h);
        }
        //进行右旋:当当前结点h的左子结点和左子结点的左子结点都为红色,需要右旋
        if (isRed(h.left) && isRed(h.left.left)){
            h = rotateRight(h);
        }
        //进行颜色反转:当前结点的左子结点和右子结点都为红色时,需要颜色反转
        if (isRed(h.left) && isRed(h.right)){
            flipColors(h);
        }
        return h;
    }

    //根据key,从树中找出对应的值
    public Value get(Key key) {
        return get(root,key);
    }
    //从指定的树x中,查找key对应的值
    public Value get(Node x, Key key) {
        if (x == null){
            return null;
        }
        //比较x结点的键和key的大小
        int cmp = key.compareTo(x.key);
        if (cmp < 0){
            return get(x.left,key);
        }else if (cmp > 0){
            return get(x.right,key);
        }else {
            return x.value;
        }
    }
}

代码测试:

package 树的进阶.红黑树;

public class RedBlackTreeTest {
    public static void main(String[] args) {
        //创建红黑树
        RedBlackTree<String, String> tree = new RedBlackTree<>();
        //往树中插入元素
        tree.put("1","张三");
        tree.put("2","李四");
        tree.put("3","王五");
        //从树中获取元素
        String r1 = tree.get("1");
        System.out.println(r1);

        String r2 = tree.get("2");
        System.out.println(r2);

        String r3 = tree.get("3");
        System.out.println(r3);
    }
}

三、B树、B+树。

45a1f855ab07497bbc3cead03ae78a9b.png

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值