0% found this document useful (0 votes)
11 views15 pages

13 Red Black Trees

Uploaded by

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

13 Red Black Trees

Uploaded by

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

Red-Black Trees

Data Structures and Algorithms


Andrei Bulatov
Algorithms – Red-Black Trees 13-2

Red-Black Trees
All binary search tree operations take (ℎ) time, where h is the
height of the tree
Therefore, it is important to `balance’ the tree so that its height is as
small as possible
There are many ways to achieve this
One of them: Red-Black trees

Every node of such a tree contains one extra bit, its color
Another agreement: the Nil pointer is treated as a leaf, an extra node
The rest of the nodes are called internal
Algorithms – Red-Black Trees 13-3

Red-Black Properties
A binary search tree is a red-black tree if it satisfies the following
red-black properties:
 Every node is either red or black
 The root is black
 Every leaf (Nil) is black
 If a node is red, then both its children are black
 For each node, all paths from the node to descendant leaves
contain the same number of black nodes
Algorithms – Red-Black Trees 13-4

Example

Nil Nil

Nil Nil Nil Nil Nil Nil

Nil Nil Nil Nil


Nil Nil Nil Nil
Nil Nil

Nil Nil
Algorithms – Red-Black Trees 13-5

Example

nil[ ]
Algorithms – Red-Black Trees 13-6

Black Height
The number of black nodes on paths from node to its descendant
leaves in a red-black tree is called its black height, denoted bh( )

Lemma
A red-black tree with internal nodes has height at most 2 log( + 1)
Proof
We show first that the subtree rooted at contains at least 2 ( ) − 1
nodes
Induction on bh( )
Base Case: If bh( ) = 0, then is a leaf, nil[ ]
In this case, 2 ( ) − 1 = 2 − 1 = 0 internal nodes
Algorithms – Red-Black Trees 13-7

Black Height (cntd)


Inductive hypothesis: the claim is true for any with height less than
that of
Inductive Case: Let bh( ) > 0 and has two children
The black height of the children is either bh( ) or bh( )– 1,
depending on its color
Since the children have smaller height, we can apply the induction
hypothesis
Thus the subtree rooted at contains at least
2 −1 + 2 −1 +1=2 ( )−1
nodes
Algorithms – Red-Black Trees 13-8

Black Height (cntd)


Suppose ℎ is the height of the tree
By the red-black property at least half of nodes on every root-to-leaf
path are black (not including the root)
Therefore the black height of the root is at least ℎ/2
Thus
≥ 2!/" − 1
log + 1 ≥ ℎ/2

QED
Algorithms – Red-Black Trees 13-9

Rotations
Sometimes we will need to rearrange pointers inside an RB-tree

Left-Rotate( , )

# %
Right-Rotate( , )
$ % # $
Algorithms – Red-Black Trees 13-10

Rotations: Pseudocode
Left-Rotate( , )
set : =right[ ]
set right[ ]: =left[ ]
set parent[left[ ]]: =
set parent[ ]: =parent[ ]
if parent[ ] =nil[ ] then
set root[ ]: =
else if =left[parent[ ]] then
set left[parent[ ]]: =
else
right[parent[ ]]: =
set left[ ]: =
set parent[ ]: =
Algorithms – Red-Black Trees 13-11

Rotations: Example
Algorithms – Red-Black Trees 13-12

Insertion
Insertion for RB-trees is done in the same way as for ordinary binary
search trees.
Except:
we should be careful about Nil links
the new node is colored red
the resulting tree may not be an RB-tree, we need to fix it
Algorithms – Red-Black Trees 13-13

Insertion: Pseudocode
RB-Insert( , ()
set : =Nil[ ], : =root[ ]
while ≠ Nil[ ] do
set : =
if *+ [(] < *+ [ ] then set : =left[ ]
else set : =right[ ]
endwhile
set parent[(]: =
if =Nil[ ] then set root[ ]: = (
else if *+ [(] < *+ [ ] then set left[ ]: = (
else set right[ ]: = (
set left[(]: =Nil[ ] right[(]: =Nil[ ] color[(]: =RED
RB-Insert-FixUp( , ()
Algorithms – Red-Black Trees 13-14

Insertion: FixUp

Step 1

Step 2

Step 3
Algorithms – Red-Black Trees 13-15

FixUp: Pseudocode
RB-Insert-FixUp( , ()
while color[parent[(]] =RED do
if parent[(] =left[parent[parent[(]]] then do
set : =right[parent[parent[(]]]
if color[ ] =RED then do
set color[parent[(]]: =BLACK color[ ]: =BLACK
set color[parent[parent[(]]]: =RED
set (: =parent[parent[(]]
else do
if ( =right[parent[(]] then do
set (: =parent[(] Left-Rotate( , ()
set color[parent[(]]: =BLACK
set color[parent[parent[(]]]: =RED
Right-Rotate( ,parent[parent[(]])
else (same as then with left and right swopped)
color[root[ ]]: =BLACK

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