Treemap delete node code logic

tags: java

private void deleteEntry(Entry<K,V> p) {
        modCount++;
        size--;

        // If strictly internal, copy successor's element to p and then make p
        // point to successor.
        // There are two sub -nodes.
        if (p.left != null && p.right != null) {
            Entry<K,V> s = successor(p);
            p.key = s.key;
            p.value = s.value;
            p = s;
        } // p has 2 children

        // Start fixup at replacement node, if it exists.
                 // Find whether there is a sub -node to be deleted
        Entry<K,V> replacement = (p.left != null ? p.left : p.right);
		
		 // There is a child node to be deleted, and the sub -node links to the parent node to be deleted
        if (replacement != null) {
            // Link replacement to parent
            replacement.parent = p.parent;
            if (p.parent == null)
                root = replacement;
            else if (p == p.parent.left)
                p.parent.left  = replacement;
            else
                p.parent.right = replacement;

            // Null out links so they are OK to use by fixAfterDeletion.
            p.left = p.right = p.parent = null;

            // Fix replacement
            if (p.color == BLACK)
                fixAfterDeletion(replacement);
        }
		 // Delete the root node directly (no child nodes, no parent node)
		 else if (p.parent == null) { // return if we are the only node.
            root = null;
        } 
		 // Without a child node, it is directly deleted by the leaf node
		else { //  No children. Use self as phantom replacement and unlink.
            if (p.color == BLACK)
                fixAfterDeletion(p);

            if (p.parent != null) {
                if (p == p.parent.left)
                    p.parent.left = null;
                else if (p == p.parent.right)
                    p.parent.right = null;
                p.parent = null;
            }
        }
    }


Intelligent Recommendation

TreeMap source code interpretation

The underlying data structure of TreeMap is the red-black tree. TreeMap takes advantage of the red-black tree with small left nodes and large right nodes, sorting according to the key, so that each el...

Treemap back-end code

Add tree structure createTreeModel Ideas: Key code: Key code: delete: removeById(id) of baseMapper is deleted; modify: Assign the parameter QuestionClassifyModel to the entity Call baseMapper's update...

Java8 source code-TreeMap

Studied a few days agoHashMap、Hashtable、LinkedHashMap、WeakHashMap, Start learning TreeMap today. TreeMap is an implementation of the Map interface based on red-black trees, and the key-value pairs are...

ubuntu16.04 install CUDA10.0+cuDNN7.4+tensorflow1.13

1. Install Nvidia driver, please refer to https://blog.csdn.net/weixin_40512640/article/details/91958123 (note that CUDA10.0 requires that the graphics card driver version is greater than or equal to ...

Multithreaded deadlock

What is a multithreaded deadlock? Answer: Nested synchronization in synchronization, resulting in locks that cannot be released     Deadlock (DeadLock) ①Deadlock overview Thread deadlock mea...

More Recommendation

## @[Data structure] (stack basic function)

@【Data structure】(stack basic function) Define the related functions of the stack, demonstrate the use of the stack, push into the stack, pop the stack, judge the stack empty, and take the top element...

Google's strongest NLP model BERT interpretation

From elmo-->ULMFiT-->gpt-->bert, probably this process   Recently, Google researchers have won the STOA results in 11 NLP tasks through the new BERT model, which has caused a lot of heat...

Web development task list

Why can't 80% of the code farmers can't do architects? >>>   The page here is not only suitable for conventional pages, but also for single-page applications, but it is not applicab...

Come to a React Native study record

2020-7-14 State (status) 1, use the constructor initialization 2. Use setState ({}) to modify and renew the view 3, synchronous asynchronous problem Variable improvement Variable lifting & functio...

iOS algorithm summary-review

According to whether the sorting records are all placed in memory, the sorting is divided into internal sorting and external sorting. The previous ones are all internal sorting. To sum up here, intern...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top