HashMap1.8 delete the node analysis

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

HashMap delete the node

  I have been concerned about is how to add a node HashMap, when the number of nodes greater than 8 into a red-black tree, otherwise use linked lists, and so on, but whether we have seen the deleted node processing logic of it? Today, we take a look at HashMap delete nodes flash in the pan

 

Source of the problem

  When viewing HashMap source, there is the following fields, when deleted, determine the number of nodes, up to the time of less than 6, it will untreeifying (tree into a linked list), found that when you click on this field, only the split () method used but split () method using a resize () method, irrespective of the deleted node, then look through the deletion node code logic

 

  

Delete Node

  By the remove method, all the way in, find a place to delete nodes. As shown below:

   

We enter removeTreeNode method, the code is as follows:

  

 

View comments method, which introduced to: If the node is too small, the current bin will be converted into ordinary bin. Notes do not understand it does not matter, we entered the place in this method the only transformation. Enter untreeify () method to view the next. code show as below:

  

Oh, look at the first line, if, else look at, ah, this is to get returns a list of (these methods are TreeNode class inside the method, so please pay attention, this is the current TreeNode node to operate). Understand this, this method is to be converted into red-black tree list, then I have to analyze how to do the analysis, but not to use global variables defined.

 

Delete judgment logical analysis

  

  Let's analyze the above analysis, this logic into this untreeify () requirement is, root == null, root.right == null, root.left == null, null == root.left.left four cases, we red-black tree to analyze seven nodes, a is the root node.

 

  

  So into this method are the following cases us a number of one to analyze when to meet the requirements of the node. (The default here think they know the characteristics of five red-black trees, mainly black balance)

 

  When these four conditions are met, we can see a maximum number of nodes like shown above, it may be seven. (Greater than 8 are not considered, because more than 8 will become a red-black tree).

    1. Up to node circumstances: when we delete a node D, only to meet root.left.left == null this condition, the tree can still maintain the characteristics of red-black tree, then the maximum number of nodes 6.

    2. Minimum node case: when the EFG is not present, deleting any node, will satisfy one of the above four kinds of rules A, B, C, D in. Where there is a minimum of nodes, there are three nodes.

  All the above list will be converted into the tree, when the node is a 3 <= nodes <= 6, can be seen, when the number of nodes is less than 6, may be transformed into a linked list, but not absolutely, the so use defined variables (fixed number 6) is not correct. Dynamic number of nodes had to get through to the judge.

The reason the number of nodal analysis  

  Why it may be converted into the list at the time of less than 6, and when more than 8 converted into red-black tree?

    Mainly through the analysis time of the query node, the average time to red-black tree for query log (n), and the list is O (n), the average is O (n) / 2.

    When the number of nodes is 8, the red-black tree query time 2, time query list is: 4, can be seen when the query efficiency is greater than the red-black tree list. (Two function curve problem, when the node is more, more than the time required for red-black tree)

    When the number of nodes is 6, why is converted into a linked list, I think mainly because the number of nodes when too few, if still with the red-black tree, in order to maintain the characteristics of red-black tree, you need to flip, L, D, etc., more consumption performance. 

  Why not 7 is transformed?

    Primarily to give a transition, to prevent frequent transformation. Also above, node 7 may be just a full binary tree.

Guess you like

Origin blog.csdn.net/shiyan719902675/article/details/92432373