# What happens internally when you remove an object from a treemap

I am wondering what happens if you remove an object in the middle of a treemap. If the treemap would look something like this:

So what would happen if you remove the number 30? Because 27 and 34 will disconnect from the tree.

• Are you talking about the specific implementation in Java (TreeMap) or just in general? The short answer is you have to reattach all the disconnected nodes. – jonrsharpe Sep 27 '16 at 7:17
• Stack Overflow isn't the place to come to for answers from the textbook. If you ask specifically about java.util.TreeMap, read about Red-Black trees and study the source code. – Marko Topolnik Sep 27 '16 at 7:19
• According to the docs I just linked, it uses the algorithm from CLR; if you want more info, that's where to look. – jonrsharpe Sep 27 '16 at 7:20
• – Spotted Sep 27 '16 at 7:22
• Red-black trees are a very well-known, classical technique. The definitive explanation on how they work, including adding, removing, balancing etc. is in the book Introduction To Algorithms by Cormen, Leiserson and Rivest (shortly referred to as CLR). If you want to understand the code, you should familiarize yourself with the technique, the "coloring" of the nodes etc. Most people here don't wish to re-introduce this well-known explanation, so you should go to the source (or try Wikipedia). – RealSkeptic Sep 27 '16 at 7:31