HashMap源码分析 二

红黑树

一、2-3树

  1. 树结构的目的就是为了提升整体的效率:插入、删除、查找。相较于链表,平衡树的索引时间复杂度是O(logn),数组的索引时间复杂度是O(n)。

  2. 二叉搜索树的数据插入过程是,与当前节点做对比,小于在左大于在右,但当数据的插入顺序不同,就会出现完全不同的数据结构,可能是一颗平衡二叉树,也有可能退化成链表(插入数据一直是最大的,会一直向右侧节点增加).

  3. 如果希望在插入数据后又保持树的特点,O(longn)的索引性能,就要在插入时进行节点的调整。

  4. 二三树是可以在保持树结构的基础上,允许在一个节点中可以有两个元素,等元素数量等于3的时候再进行调整,通过这种方式来保证整个二叉搜索树的平衡性。

  5. 二三树所有的子叶节点都在同一层,一个节点可以有1到两个数据,如果有三个就需要调整树结构,一个节点一个数据时则有两个子节点,一个节点两个数据时,则有三个子节点,且中间子节点时介于两个节点间的值。

  6. 二三树Java代码实现

    class Node {
        int key1, key2;
        Node left, middle, right;
    ​
        public Node(int key) {
            this.key1 = key;
            this.key2 = -1;  // Initialize key2 to -1 indicating it is not used initially
            this.left = this.middle = this.right = null;
        }
    ​
        public boolean isLeaf() {
            return (left == null && middle == null && right == null);
        }
    }
    ​
    public class TwoThreeTree {
        private Node root;
    ​
        public TwoThreeTree() {
            root = null;
        }
    ​
        public void insert(int key) {
            if (root == null) {
                root = new Node(key);
            } else {
                root = insert(root, key);
            }
        }
    ​
        private Node insert(Node node, int key) {
            if (node == null) {
                return new Node(key);
            }
    ​
            if (node.isLeaf()) {
                if (node.key2 == -1) {
                    if (key < node.key1) {
                        node.key2 = node.key1;
                        node.key1 = key;
                    } else {
                        node.key2 = key;
                    }
                } else {
                    // Split the node and promote the middle key to the parent
                    Node newRoot = new Node(node.key2);
                    Node newLeft = new Node(node.key1);
                    Node newRight = new Node(key);
    ​
                    newRoot.left = newLeft;
                    newRoot.middle = newRight;
                    return newRoot;
                }
            } else {
                if (key < node.key1) {
                    node.left = insert(node.left, key);
                } else if (key > node.key2) {
                    node.right = insert(node.right, key);
                } else {
                    node.middle = insert(node.middle, key);
                }
            }
    ​
            return node;
        }
    ​
        // Other methods for deletion, searching, and tree traversal can be added here
    ​
        public static void main(String[] args) {
            TwoThreeTree tree = new TwoThreeTree();
            tree.insert(10);
            tree.insert(20);
            tree.insert(5);
    ​
            // Perform operations on the tree as needed
        }
    }
    ​

红黑树

  1. Java中的TreeMap, 1.8版本的HashMap都是基于红黑树结构实现的。

  2. 红黑树的时间复杂度是O(longn)

  3. 红黑树插入新的节点时怎么保持平衡:

    1. 左旋定义:把一个向右倾斜的红节点链接转化为左链接。

    2. 右旋定义: 把一个向左倾斜的红节点连接(2-3 树,3-叉双元素节点),转换为右连接。

  4. 红黑树Java代码实现

    
    enum Color {
        RED, BLACK
    }
    ​
    class Node {
        int data;
        Color color;
        Node parent, left, right;
    ​
        public Node(int data) {
            this.data = data;
            this.color = Color.RED;
            this.parent = this.left = this.right = null;
        }
    }
    ​
    public class RedBlackTree {
        private Node root;
    ​
        public RedBlackTree() {
            this.root = null;
        }
    ​
        // 左旋操作
        private void leftRotate(Node x) {
            Node y = x.right;
            x.right = y.left;
    ​
            if (y.left != null)
                y.left.parent = x;
    ​
            y.parent = x.parent;
    ​
            if (x.parent == null)
                root = y;
            else if (x == x.parent.left)
                x.parent.left = y;
            else
                x.parent.right = y;
    ​
            y.left = x;
            x.parent = y;
        }
    ​
        // 右旋操作
        private void rightRotate(Node y) {
            Node x = y.left;
            y.left = x.right;
    ​
            if (x.right != null)
                x.right.parent = y;
    ​
            x.parent = y.parent;
    ​
            if (y.parent == null)
                root = x;
            else if (y == y.parent.left)
                y.parent.left = x;
            else
                y.parent.right = x;
    ​
            x.right = y;
            y.parent = x;
        }
    ​
        // 插入节点
        public void insert(int data) {
            Node node = new Node(data);
            root = insertRecursive(root, node);
            fixInsert(node);
        }
    ​
        private Node insertRecursive(Node root, Node node) {
            if (root == null)
                return node;
    ​
            if (node.data < root.data) {
                root.left = insertRecursive(root.left, node);
                root.left.parent = root;
            } else if (node.data > root.data) {
                root.right = insertRecursive(root.right, node);
                root.right.parent = root;
            }
    ​
            return root;
        }
    ​
        private void fixInsert(Node node) {
            while (node.parent != null && node.parent.color == Color.RED) {
                if (node.parent == node.parent.parent.left) {
                    Node uncle = node.parent.parent.right;
    ​
                    if (uncle != null && uncle.color == Color.RED) {
                        node.parent.color = Color.BLACK;
                        uncle.color = Color.BLACK;
                        node.parent.parent.color = Color.RED;
                        node = node.parent.parent;
                    } else {
                        if (node == node.parent.right) {
                            node = node.parent;
                            leftRotate(node);
                        }
    ​
                        node.parent.color = Color.BLACK;
                        node.parent.parent.color = Color.RED;
                        rightRotate(node.parent.parent);
                    }
                } else {
                    Node uncle = node.parent.parent.left;
    ​
                    if (uncle != null && uncle.color == Color.RED) {
                        node.parent.color = Color.BLACK;
                        uncle.color = Color.BLACK;
                        node.parent.parent.color = Color.RED;
                        node = node.parent.parent;
                    } else {
                        if (node == node.parent.left) {
                            node = node.parent;
                            rightRotate(node);
                        }
    ​
                        node.parent.color = Color.BLACK;
                        node.parent.parent.color = Color.RED;
                        leftRotate(node.parent.parent);
                    }
                }
            }
    ​
            root.color = Color.BLACK;
        }
    ​
        // 中序遍历打印树的元素
        public void inOrder() {
            inOrderRecursive(root);
        }
    ​
        private void inOrderRecursive(Node root) {
            if (root != null) {
                inOrderRecursive(root.left);
                System.out.print(root.data + "(" + (root.color == Color.RED ? "R" : "B") + ") ");
                inOrderRecursive(root.right);
            }
        }
    ​
        public static void main(String[] args) {
            RedBlackTree redBlackTree = new RedBlackTree();
    ​
            // 插入一些示例数据
            int[] elements = {7, 3, 18, 10, 22, 8, 11, 26, 2, 6, 13};
            for (int element : elements) {
                redBlackTree.insert(element);
            }
    ​
            // 打印中序遍历结果
            System.out.print("中序遍历结果: ");
            redBlackTree.inOrder();
        }
    }
    ​

总结

  1. 总结,HashMap总结就是底层使用了数组桶+链表+红黑树。

  2. 计算Hash位置时会先执行一个扰动函数,用来减少碰撞率

  3. 当发生碰撞时会先作为链表结构进行追加,当容量小于64时会进行扩容,扩容的思路就是2^n+1,

  4. 什么时候会发生扩容

    1. hashmap不会在容量满了之后才进行扩容,当达一定的阈值的时候就会开始扩容操作,默认的阈值是0.75,也就是说当容量达到了0.75就会进行扩容的操作。

  5. 为什么HashMap的默认长度要设置为16

    1. 因为16是2的幂次数,数值不算大也不算小,即不会太占用空间,也不会太频繁的触发扩容机制。

  6. 如果HashMap的初始值写入了17,那它初始化后的实际容量是多少

    1. 32,因为HashMap在初始化容量时会先判断当前长度是不是2的幂次等,如果不是则向后寻找最小的2n的值,17之后的最小2n值就是32,所以它的初始容量应该是32.

  7. 什么时候HashMap里存储的链表会转成红黑树

    1. 当容量超过64且链表长度大于8时,会由链表转换成红黑树。

  8. 除了在HashMap中还有哪里用到了红黑树

    1. TreeMap,Mysql的索引

  9. 为什么使用这种树结构

    1. 增、删、遍历速度足够快,如果是数组结构它的时间复杂度会达到O(n), 但如果是树结构它的时间复杂度会是O(logn),所有的树结构目的最终都是为了达到平衡树,如果仅仅是依靠单纯的二叉树,可能会出现链表的情况,也就是无限大或无限小,导致一直向一个方向插入数据,导致出现链表结构。二三树和红黑树都是为了避免这种情况的发生而演变出来的算法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值