Foundation of Computing Systems

1 / 30

# Foundation of Computing Systems - PowerPoint PPT Presentation

Foundation of Computing Systems. Lecture 11 Trees: Part VI-C. … Continued. Deletion in a Red-Black Tree. Deletion of a node from a red-black tree follows two major steps Delete the node using the usual deletion operation as in a binary search tree .

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Foundation of Computing Systems' - macario-trujillo

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Foundation of Computing Systems

Lecture 11

Trees: Part VI-C

IT 60101: Lecture #11

### … Continued

IT 60101: Lecture #11

Deletion in a Red-Black Tree
• Deletion of a node from a red-black tree follows two major steps
• Delete the node using the usual deletion operation as in a binary search tree.
• This deletion first finds the location of the node to be deleted and then it is replaced by its inorder successor.
• If the inorder successor has its child, then the child is moved up to the place of the inorder successor.

2. The above deletion operation may disturb the properties of a red-black tree. To restore the red-black tree, we require a fix-up operation.

IT 60101: Lecture #11

Deletion in a Red-Black Tree
• Suppose the node that has to be deleted is x and its inorder successor is y.
• The following situation and steps to be followed accordingly as discussed below.

Case 1:y is NULL, that is, node x does not have any inorder successor.

Case 2:  y is RED and y has no children as internal nodes

Case 3:  y is BLACK and it has no children as an internal node

Case 4:y is BLACK and it has an internal RED node

IT 60101: Lecture #11

Deletion Operation: Case 1
• Case 1:y is NULL, that is, node x does not have any inorder successor.

IT 60101: Lecture #11

Deletion Operation: Case 1
• Case 1:y is NULL, that is, node x does not have any inorder successor.

IT 60101: Lecture #11

Deletion Operation: Case 1
• Case 1:y is NULL, that is, node x does not have any inorder successor.

IT 60101: Lecture #11

Deletion Operation: Case 2
• Case 2:  y is RED and y has no children as internal nodes

IT 60101: Lecture #11

Deletion Operation: Case 2
• Case 2:  y is RED and y has no children as internal nodes

IT 60101: Lecture #11

Deletion Operation: Case 3
• Case 3:  y is BLACK and it has no children as an internal node

IT 60101: Lecture #11

Deletion Operation: Case 4
• Case 4:y is BLACK and it has an internal RED node

IT 60101: Lecture #11

Deletion: Fix-up Operation
• Suppose, x is the node to be deleted. y is its inorder successor and z be its another child other than the leaf child.
• The following three problems may arise while x is deleted from the red-black tree.
• y is red and it becomes the new root. This violates the “root property”.
• Both z and the parent of y (now after deletion of y the parent of y becomes the parent of z) are red. This violates the “internal property” and leads to a “double-red problem”.
• y’s removal causes any path that contained one fewer black node than the path before the removal of y. This violates the “black-depth property”.

IT 60101: Lecture #11

Deletion: Fix-up Operation
• Assume that z is the left sub-tree with respect to the parent y. The following four cases can occur while fixing up the above-mentioned problem.
• Case 1:  z’s sibling w is RED
• Case 2:  z’s sibling w is BLACK and both the children of w are BLACK
• Case 3:z’s sibling w is BLACK and w’s left child is RED and w’s right child is BLACK.
• Case 4:  z’s sibling w is BLACK and w’s RIGHT child is RED.

IT 60101: Lecture #11

Fix-up Operation: Case 1
• Case 1:  z’s sibling w is RED
• Steps:
• Change the colour of w to BLACK

2. Switch (flip) the colour of parent of z.

3. Perform a left rotation of w

4. Sibling of z becomes a new sibling

• This may lead to any one of the following problems of fix-up Case 2, Case 3 and Case 4 (when the node w is black).
• These three cases are distinguished by the colours of children of w.

IT 60101: Lecture #11

Fix-up Operation: Case 1

IT 60101: Lecture #11

Fix-up Operation: Case 2
• Case 2:  z’s sibling w is BLACK and both the children of w are BLACK
• Steps:
• Make the colour w of RED
• Add an extra black to the parent of z (which was earlier either red or black).
• Repeat the fix-up procedure but now changing the parent of z becomes a new z.

IT 60101: Lecture #11

Fix-up Operation: Case 2

IT 60101: Lecture #11

Fix-up Operation: Case 3
• Case 3:z’s sibling w is BLACK and w’s left child is RED and w’s right child is BLACK.
• Steps:
• Exchange the colours of w and its left child.
• Perform a right rotation of w’s left child. This rotation results in a new sibling w of z which is a black node with a red child.

IT 60101: Lecture #11

Fix-up Operation: Case 3

IT 60101: Lecture #11

Fix-up Operation: Case 4
• Case 4:  z’s sibling w is BLACK and w’s RIGHT child is RED.
• Steps:
• Exchange the colour of w with the colour of parent of z.
• Perform a left-rotation of parent of z (or w)
• Repeat the fix-up procedure until z reaches the root or z becomes BLACK.

IT 60101: Lecture #11

Fix-up Operation: Case 4

IT 60101: Lecture #11

For detail implementation of deletion operation on red-black binary trees see the book

• Classic Data Structures
• Chapter 7
• PHI, 2nd Edn., 17th Reprint

IT 60101: Lecture #11

HB (AVL) Tree vs. RB Tree
• Height
• A red-black tree with n internal nodes has height at most 2log2(n+1)
• An AVL tree with n internal nodes has height at most 1.44log2n (see Lemma 7.11).
• Specialization:
• An AVL tree is a kind of a red-black tree (in other words, all AVL trees satisfy the properties of a red-black tree but all red-black trees are not AVL trees).
• Generalization:
• A red-black tree is a kind of B-tree. This is why a red-black tree is also alternatively termed Symmetric B-tree. (see Section 7.5.9: How a red-black tree is a kind of B-tree is discussed in the next section).

IT 60101: Lecture #11

Application of RB Tree
• A red-black tree is used to improve the speed of searching in a B-tree
• Restructuring with RB tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11

Restructuring with RB Tree

IT 60101: Lecture #11