Javatpoint Logo
Javatpoint Logo

Deletion in Red-Black Tree using Python

Red-Black Trees

A red-black tree is a binary search tree with the additional property of being "nearly" adjusted. Every node in a red-black tree has a color, either red or black, and these colors are utilized to keep up with balance during insertions and deletions.

Deletion:

BST Deletion:

Begin by finding the node you need to erase utilizing standard BST deletion. Assuming the node has one or no children, eliminate it and supplant it with its child (if any).

Save the shade of the node to be erased, as it will be required later to decide if to apply the "erase fix."

Fix Violations (Erase Fix):

Like insertion, deletion could disregard red-black tree properties, especially the double-black property. You want to play out a progression of recoloring, rotation, and substitution tasks known as the "erase fix."

  1. Decide the side (left or right) from which the node was erased and whether the kin (sibling) of the node is red or black.
  2. Assuming the kin is red, perform rotations and recoloring to change the issue into one of the different cases.
  3. If the kin is black and both of its children are black or invalid, recolor the kin as red and spread the double black issue upwards.
  4. Assuming the kin is black and has something like one red child, perform rotations and recoloring to change over the issue into the following case.
  5. Rehash these means while climbing the tree until the double black is spread out of the root or the violation is settled.

Code

Output:

The initial starting tree:

Deletion in Red-Black Tree using Python

After deleting element 40:

Deletion in Red-Black Tree using Python

After deleting element 65:

Deletion in Red-Black Tree using Python

Executing these means accurately guarantees that insertion and deletion activities perform well and keep up with the helpful properties of the tree.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA