Foundation of Computing Systems - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Foundation of Computing Systems

Description:

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 ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 31
Provided by: SchoolofI79
Category:

less

Transcript and Presenter's Notes

Title: Foundation of Computing Systems


1
Foundation of Computing Systems
  • Lecture 11
  • Trees Part VI-C

2
Continued
3
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.

4
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

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

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

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

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

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

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

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

12
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.
  • ys removal causes any path that contained one
    fewer black node than the path before the removal
    of y. This violates the black-depth property.

13
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  zs sibling w is RED
  • Case 2  zs sibling w is BLACK and both the
    children of w are BLACK
  • Case 3  zs sibling w is BLACK and ws left
    child is RED and ws right child is BLACK.
  • Case 4  zs sibling w is BLACK and ws RIGHT
    child is RED.

14
Fix-up Operation Case 1
  • Case 1  zs 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.

15
Fix-up Operation Case 1
16
Fix-up Operation Case 2
  • Case 2  zs 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.

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

19
Fix-up Operation Case 3
20
Fix-up Operation Case 4
  • Case 4  zs sibling w is BLACK and ws 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.

21
Fix-up Operation Case 4
22
For detail implementation of deletion operation
on red-black binary trees see the book Classic
Data Structures Chapter 7 PHI, 2nd Edn., 17th
Reprint
23
HB (AVL) Tree vs. RB Tree
  • Height
  • A red-black tree with n internal nodes has height
    at most 2log2(n1)
  • 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).

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

25
Restructuring with RB Tree
26
Restructuring with RB Tree
27
Restructuring with RB Tree
28
Restructuring with RB Tree
29
Restructuring with RB Tree
30
Restructuring with RB Tree
Write a Comment
User Comments (0)
About PowerShow.com