java的HashMap中TreeNode节点的删除复杂性分析

目录

java8中的removeTreeNode源码

        /**
         * Removes the given node, that must be present before this call.
         * This is messier than typical red-black deletion code because we
         * cannot swap the contents of an interior node with a leaf
         * successor that is pinned by "next" pointers that are accessible
         * independently during traversal. So instead we swap the tree
         * linkages. If the current tree appears to have too few nodes,
         * the bin is converted back to a plain bin. (The test triggers
         * somewhere between 2 and 6 nodes, depending on tree structure).
         */
final void removeTreeNode(HashMap<K, V> map, Node<K, V>[] tab, boolean movable) {
   
            int n;
            if(tab == null || (n = tab.length) == 0) {
   
                return;
            }
            
            int index = (n - 1) & hash;
            
            TreeNode<K, V> first = (TreeNode<K, V>) tab[index], root = first, rl;
            TreeNode<K, V> succ = (TreeNode<K, V>) next, pred = prev;
            // pred==null 表示当前待删除的节点没有前驱节点
            if(pred == null) {
   
                // 连续赋值的执行顺序
                // 这里只是移动了tab[idx]和first指针的位置
                tab[index] = first = succ;
            } else {
   
                pred.next = succ;
            }

            // 修改succ的前驱节点,此时被删除的节点为头结点,头节点从链表中删除,但是旧头结点还有指向当前头结点的指针
            // 如果不是头结点,双向修改指针,旧节点对链表的链接仍然存在
            if(succ != null) {
   
                succ.prev = pred
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值