Title: Foundation of Computing Systems
1Foundation of Computing Systems
- Lecture 11
- Trees Part VI-C
2 Continued
3Deletion 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.
4Deletion 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
5Deletion Operation Case 1
- Case 1 y is NULL, that is, node x does not have
any inorder successor. -
6Deletion Operation Case 1
- Case 1 y is NULL, that is, node x does not have
any inorder successor. -
7Deletion Operation Case 1
- Case 1 y is NULL, that is, node x does not have
any inorder successor. -
8Deletion Operation Case 2
- Case 2 y is RED and y has no children as
internal nodes -
9Deletion Operation Case 2
- Case 2 y is RED and y has no children as
internal nodes -
10Deletion Operation Case 3
- Case 3 y is BLACK and it has no children as an
internal node -
11Deletion Operation Case 4
- Case 4 y is BLACK and it has an internal RED
node
12Deletion 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.
13Deletion 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.
14Fix-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.
15Fix-up Operation Case 1
16Fix-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.
17Fix-up Operation Case 2
18Fix-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.
19Fix-up Operation Case 3
20Fix-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.
21Fix-up Operation Case 4
22For detail implementation of deletion operation
on red-black binary trees see the book Classic
Data Structures Chapter 7 PHI, 2nd Edn., 17th
Reprint
23HB (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).
24Application of RB Tree
- A red-black tree is used to improve the speed of
searching in a B-tree - Restructuring with RB tree
25Restructuring with RB Tree
26Restructuring with RB Tree
27Restructuring with RB Tree
28Restructuring with RB Tree
29Restructuring with RB Tree
30Restructuring with RB Tree