0% found this document useful (0 votes)
42 views

RB Tree

Here are the steps to insert those nodes into a red-black tree: 1. Insert 2 as the root node (black) 2. Insert 1 as left child of 2 (red) 3. Insert 4 as right child of 2 (red) 4. Insert 5 as right child of 4 (red triggering left rotation of 4 and recoloring) 5. Insert 9 as right child of 2 (red triggering right rotation of 2 and recoloring) 6. Insert 3 as left child of 1 (red triggering left rotation of 1 and recoloring) 7. Insert 6 as right child of 4 (red triggering right rotation of 4 and recoloring) 8. Insert 7

Uploaded by

Okabe Rintarou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

RB Tree

Here are the steps to insert those nodes into a red-black tree: 1. Insert 2 as the root node (black) 2. Insert 1 as left child of 2 (red) 3. Insert 4 as right child of 2 (red) 4. Insert 5 as right child of 4 (red triggering left rotation of 4 and recoloring) 5. Insert 9 as right child of 2 (red triggering right rotation of 2 and recoloring) 6. Insert 3 as left child of 1 (red triggering left rotation of 1 and recoloring) 7. Insert 6 as right child of 4 (red triggering right rotation of 4 and recoloring) 8. Insert 7

Uploaded by

Okabe Rintarou
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Red Black Tree

Definition
• A red-black tree is a binary search tree with one extra attribute for
each node: the color, which is either red or black.
• Red-black tree properties are:
1. Every node is either red or black.
2. Root and leaves (NULL) are black; if a node’s child is missing then we will
assume that it has a nil child in that place and this nil child is always colored
black.
3. If a node is red, then both its children are black. (red property)
4. Every simple path from a node to a descendant leaf contains the same
number of black nodes. (black property)
Example
Operations
1. Rotation
2. Insertion
3. Deletion
Rotations
To ensure that color scheme and properties of red-black trees don’t
get thrown off, they employ a key operation known as rotation.
Rotation is a binary operation, between a parent node and one of its
children, that swaps nodes and modifies their pointers while
preserving the inorder traversal of the tree (so that elements are still
sorted).
There are two types of rotations: left rotation and right rotation.
Left Rotation
Left rotation swaps the parent node with its right child.
Here are the steps involved in for left rotation:
1. Assume node x is the parent and node y is a non-leaf right child.
2. Let y be the parent and x be its left child.
3. Let y’s left child be x’s right child.
Example:
Right Rotation
Right rotation swaps the parent node with its left child.
Here are the steps involved in for right rotation:
1. Assume node x is the parent and node y is a non-leaf left child.
2. Let y be the parent and x be its right child.
3. Let y’s right child be x’s left child.
Insertion
• Let x be the newly inserted node.
1) Perform standard BST insertion and make the color of newly
inserted nodes as RED.
• 2) If x is root, change color of x as BLACK (Black height of complete
tree increases by 1).
• 3) Do following if color of x’s parent is not BLACK or x is not root.
….a) If x’s uncle is RED (Grand parent must have been black
from property 4)
……..(i) Change color of parent and uncle as BLACK.
……..(ii) color of grand parent as RED.
……..(iii) Change x = x’s grandparent, repeat steps 2 and 3 for new x.
b) If x’s uncle is BLACK, then there can be four configurations for x, x’s
parent (p) and x’s grandparent (g) (This is similar to AVL Tree)
……..i) Left Left Case (p is left child of g and x is left child of p)
……..ii) Left Right Case (p is left child of g and x is right child of p)
……..iii) Right Right Case (Mirror of case i)
……..iv) Right Left Case (Mirror of case ii)
Left Left Case (p is left child of g and x is left
child of p)
Left Right Case (p is left child of g and x is
right child of p)
(p is right child of g and x is right child of p)
(p is right child of g and x is left child of p)
Exercise:
Insert 2, 1, 4, 5, 9, 3, 6, 7 in RB Tree

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy