Verifying the Red-Black tree properties after insertion

To verify whether the Red-Black tree is still balanced and still follows all of its requirements, we will use two concepts: recoloring and rotation.

After inserting a new node into the tree, this new node will be red. This does not affect the rule of the count of black nodes (rule 6), but it can affect rule 5: two adjacent red nodes cannot coexist. If the parent of the inserted node is black, then there is no problem. However, if the parent of the inserted node is red, then we have a violation of rule 5. To solve this violation, we simply need to change the color of the node's parent, the node's grandparentand the node's uncle (because we are changing the parent color as well).

The ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.