When removing one element from a red-black tree, O (log n) time is required, where n is the number of elements that are currently in the tree.
If you delete only a few items, it is best to delete them one by one, resulting in O (k log n) operations (k = deleted items, n = items in the tree before deletion).
But if you know that you are going to remove a large number of nodes (for example, 50% or more from the tree), then it is better to iterate the elements you want to save (O (k '), where k' = the elements to be saved), then drop the tree (O (1) or O (n) depending on your memory management scheme) and rebuild the tree (O (k 'log k')). The total complexity is O (k ') + O (k' log k ') = O (k' log k '), which is obviously less than O (k log n) when k' k (you hold less than 50% of the tree )
Well, anyway, when you are going to remove the MOST from the elements, it is better to list in practice those that you want to save, and then restore the tree.
source share