Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Red-Black Trees: Properties, Dynamic Set Operations, and Deletion, Study notes of Algorithms and Programming

This document from wellesley college discusses the properties and dynamic set operations of red-black trees, a self-balancing binary search tree. It covers the red-black properties, balance property, rotation techniques, and insertion and deletion processes. The document also mentions the importance of preserving the tree's properties during these operations.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-b4y
koofers-user-b4y 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Wellesley College CS231 Algorithms October 30, 1996
Handout #23
RED-BLACK TREES
Reading: CLR Chapters 14 and 15
---------------------------------------------------------------------------------------------
------------------------
Definition
A red-black tree (RBT) is a binary search tree that satisfies the following red-black
properties:
RBT1. Every node (i.e. non-leaf) has a color that is either red or black.
RBT2. Every leaf (i.e. nil) is black.
RBT3. If a node is red, both children are black.
RBT4. Every path from a given node down to any descendant leaf contains the same number
of black nodes. The number of black nodes on such a path (not including the initial
node) is the black-height (bh) of the node.
RBT5. The root of the tree is black (not a CLR property, but should be).
---------------------------------------------------------------------------------------------
------------------------
Balance Property of Red-Black Trees
Consider a subtree with n nodes (non-leaves) rooted at any node x within a red-black tree.
Then the following relationships hold:
height(x) bh(x) height(x)/2
2height(x) > n 2bh(x) - 1
2 lg(n+1) height(x) > lg(n)
The last relationship is the sense in which a red-black tree is balanced.
---------------------------------------------------------------------------------------------
------------------------
pf3
pf4
pf5

Partial preview of the text

Download Red-Black Trees: Properties, Dynamic Set Operations, and Deletion and more Study notes Algorithms and Programming in PDF only on Docsity!

Wellesley CollegeCS231 AlgorithmsOctober 30, 1996 Handout #

RED-BLACK TREES

Reading: CLR Chapters 14 and 15

**---------------------------------------------------------------------------------------------

Definition**

A red-black tree (RBT) is a binary search tree that satisfies the following red-black properties:

RBT1. Every node (i.e. non-leaf) has a color that is either red or black.

RBT2. Every leaf (i.e. nil) is black.

RBT3. If a node is red, both children are black.

RBT4. Every path from a given node down to any descendant leaf contains the same number of black nodes. The number of black nodes on such a path (not including the initial node) is the black-height (bh) of the node.

RBT5. The root of the tree is black (not a CLR property, but should be). **---------------------------------------------------------------------------------------------


Balance Property of Red-Black Trees**

Consider a subtree with n nodes (non-leaves) rooted at any node x within a red-black tree. Then the following relationships hold:

height(x) ≥ bh(x) ≥ height(x)/

2 height(x)^ > n ≥ 2 bh(x)^ - 1

2 lg(n+1) ≥ height(x) > lg(n)

The last relationship is the sense in which a red-black tree is balanced. --------------------------------------------------------------------------------------------- ------------------------

------------------------Dynamic Set Operations on Red-Black Trees

Since a red-black tree is a binary search tree, the following operations work as on BSTs with no modifications:

Search, Minimum, Maximum, Predecessor, and Successor

Insert and Delete are similar to BST versions, but it may be necessary to update colors and tree structure to preserve RBT properties. It also helps to make some assumptions:

  • every red-black tree T has a distinguished dummy header node header[T] whose left child is the actual tree and whose color is black.
  • every leaf is a data structure with parent and color attributes. (As noted in CLR, can get by with a single distinguished leaf nil[T] per red-black tree.) **---------------------------------------------------------------------------------------------

Rotation**

A binary operator op is said to be associative if for all a, b, and c

(a op b) op c = a op (b op c)

This relationship can be expresssed in tree form as:

a b

op c

op

a

b c

op

op

In a BST, can view a tree node T with left subtree l and right subtree r as an operator that designates the sequence of labels of all nodes in T in an inorder traversal. From this perspective, nodes are associative binary operators:

a b

c a

b c

Y

X

X

Y

Right Rotation

Left Rotation

Changing a BST from one of these forms to the other is safe in the sense that it preserves the binary search tree property. Moving from the left form to the right form is called a right rotation because it changes the thick edge to move down to the right. A left rotation changes the thick edge to move down towards the left. A rotation can be specified either by the root node and a direction (Y right or X left) or by the labels of the two nodes of the thick edge (XY). --------------------------------------------------------------------------------------------- ------------------------

Deletion from a Red-Black Tree

RB-Delete(T, x) has the following steps:

Step 1: Use BST deletion to delete x from T. When both of x's children are non-leaves, x is replaced by Successor(x); in this case, ensure that Successor(x) is colored with x's color. RBT1, RBT2, RBT3, are maintained. Step 2: Let y refer to the fringe node that is spliced out as part of Step 1 (this is either x itself or Successor(x)). If y was red, RBT4 is preserved and we are done. But if y was black, RBT4 is now false. In this case, reassert RBT4 by transferring the blackness of y to the child z of y that replaced y. If z is red, recolor it to black, and we are done. But if z was previously black, it is now "doubly-black". (Note: RBT5 can only be violated if y is a (necessarily black) root with one red child z and one (necessarily black) leaf child. In this case, the root is replaced by z, and transferring the blackness of y to z reasserts RBT5.) Step 3: Propagate double-black node up the tree by a sequence of the four types of moves described below until the extra black is absorbed or the root of the tree is reached (in which case the extra black can be removed without affecting the tree's black-height).

CLR characterize the moves for reasserting RBT4 in four cases. In each case, assume that z is initially doubly-black. Each move preserves the black-height of the tree (convince yourself that this is true for each case)! We consider the cases in the opposite order from CLR. (Study these descriptions in conjunction with the pictures from CLR. Note: what I call z, CLR calls x.)

Case 4 : z's sibling is black and its "opposite" nephew is red. RBT4 is reasserted by a rotation about parent[z]/sibling[z] and a recoloring.

Case 3 : z's sibling is black, its "opposite" nephew is black, and its "same" nephew is red. A rotation about sibling[z]/same-nephew[z] and a recoloring leads to Case 4. So RBT4 is reasserted in two moves with two rotations.

Case 2 : z's sibling is black, and both nephews are black. Merge the extra black of z and the black of sibling[z] and move this blackness to parent[z] (leaving sibling[z] red). If parent[z] was initially red, it is now black, and the extra black has been reabsorbed in one move with zero rotations. But if parent[z] was originally black, it is now doubly-black and we must repeat the doubly-black removal process with parent[z] as the new z. In the worst case, Case2 is encountered at each node on the way to the root for a total of Θ(lg(n)) moves.

Case 1: z's sibling is red, implying that z' parent and two nephews are necessarily black (by RBT3). Rotate about parent[z]/sibling[z] and exchange the colors of these two nodes. After this rotation, z's sibling is black, and one of Case2, Case3, or Case4 applies.

--------------------------------------------------------------------------------------------- ------------------------

The following state diagram summarizes doubly-black removal. Note that RBT4 can be

reasserted in Θ(lg(n)) moves with at most 3 rotations.

Case Analysis

Case 1 (^) Case 3

Move 3 1 rotation

Move 2 0 rotations

Move 4 1 rotation

sibling[z] red

sibling[z] black (^) sibling[z] black both nephews black

same nephew red; opposite nephew black

opposite nephew red

sibling[z] black

RBT4 asserted

new z = Case 4 parent[z]

Case Analysis

Move 1 1 rotation (^) Case 3 Case 4 sibling[z] black both nephews black

parent[z] black

parent[z] red

Case 2a

Case2b

Case2b