Title: Red Black Trees
1Red Black Trees
- Jason Goodman
- CS 566
- Spring 2007
2History
- The concept of a balancing tree was first
invented by Adelson-Velskii and Landis in
1962. They came up with the AVL tree. - In 1970, J. E. Hopcroft invented the 2-3 trees
(another balancing Search Tree. - The Red Black Tree was first invented by Rudolf
Bayer (1972), who called them symmetric binary
B-trees. - In 1978, Leo J Guibas and Robert Sedgewick came
up with the term, Red Black Binary Tree.
3Rudolf Bayer
- He has been a Professor of informatics at the
Technical University of Munich since 1972. - Informatics is a science concerned with
collection, classification, manipulation,
storage, retrieval and dissemination of
information. - He if famous for inventing 2 sorting structures,
B-trees (now called red black trees), with Edward
M. McCreight. And UB-tress (with Volker Markl). - UB-Trees is another type of balanced tree used
for storing and for efficiently retrieving
multidimensional data.
4Introduction
- Binary Tree is a tree structure in which each
node has at most 2 children. - Binary Search Tree Each node has a value, where
the left sub-tree node contains only values
lesser or equal to its parent, and the right
sub-tree contains nodes of greater or equal value
to its parents. - In the case of a Binary Search Tree search,
insert and delete all have a running time in the
order of O(h) where h height. - This is because, a Binary Search tree is not self
balancing, so in worst case, it can be a list.
If each node has 1 child.
5Introduction
- A red black tree is a type of binary search tree
that is self balancing. - Each node holds one extra piece of data, Color
(red or black). - Typically, in computer science, associative
arrays are used to implement them. - Besides following the rules of Binary Search
Trees, Red Black Trees have 5 addition
rules/properties that keep them balanced. - Balanced Means the longest path is no more then
twice as long as the shortest path. - Note leaves are nil (no value) and act just as
place holder. - Note A Red Black Tree has a height of 2log(n
1) at most
6Red Black Properties
- All rules pertaining to a Binary Search Tree.
- A node is either red or black.
- The root is ALWAYS black.
- All leaves are black.
- Both Children of a node that is red, are black.
(no red node can have a red child). - Every (simple) path from a node to a descendant
leaf contains the same number of black Nodes
(leaves not included). This is called black
height.
7OperationsRed black trees
- Find (or Search) O(log n).
- Minimum O(log n)
- Maximum O(log n)
- Successor O(log n) Node with the smallest value
greater than the node in question. - Predecessor O(log n) Node with the largest value
less than the node in question. - Insert and Delete both run in the order of O(log
n), however, they can destroy the properties of a
Red Black Tree, causing the tree to need to be
restored. - Restoring procedures run in O(log n)
- These operations perform color changes, pointer
moving. - I am going to focus more on Insert and Delete
because these are the operations that effect the
balancing of a tree.
8Read Operations
Search, Minimum, Maximum, Successor and
predecessor work in the order of O(log n). Same
methods as used in a binary search tree. In a
Binary Search Tree theses methods take O(h) where
h height. This is because, these trees are not
guaranteed to be balanced.
9Rotation
Rotation needs to be talked about before Insert
and Delete. Operations (Tree-Insert and
Tree-Delete) can destroy the properties of a red
black tree. To restore sometimes pointers in the
tree need to be change. This is called
Rotation. There are 2 types of rotations, left
and right. These are mirror images of each
other. Note Sometimes to restore the red black
properties, some nodes have to have their colors
change from red to black or black to red.
10Left Rotation
- Left rotation rotates around the link between the
given node, and its right child. - In this image the left rotation is rotating
around the link between nodes P ? Q. (It assumes
that node Q is not nil). - Q (the right child) becomes the parent.
(Bringing its right child if it has one). - P, the former parent becomes the left child.
(Keeping its left child if it has one). - The left child (if it exists) of Q, now becomes
the right child of P. This is done because Q can
have only 2 children. - The right rotation is a mirror image
Q ? higher value than P (indicated by it being a
right child).
Left rotation rotates around given node and its
right child. Right rotation rotates around given
node and its left child
11Left Rotation Pseudo-Code
12Insert
- Insertion can be done in the order of O(log n).
It follows closely the insertion into a binary
search tree, except - The Node inserted is red
- A restore function must be called to fix
red-black properties that might be violated. - Two possible violation types can occur. (Only one
violation can occur at a time) - Violation of property 2 will occur if the new
node added is the root. In this case change the
color to black. - Violation to property 4 will occur if the new
node, has a red parent. - Violations to property 4 can take on 1 of 6
cases. 3 of these cases occur when the new
nodes parent is a left child, the other 3 are
mirror images, and occur if the new nodes parent
is a right child. - I will focus on the 3 cases where the nodes
parent is a left child.
13Insert Fix-up Pseudo-code
14Case 1
- Parent and uncle of newly added node, both red
- To Fix
- In this case we can fix the violation of rule 4
by coloring both the parent and - the uncle of the newly inserted node black, and
changing its grandparent to red. - We then point to the grandparent, and re-enter
the while loop.
15Case 2 and 3
- Case 2 the newly added nodes uncle is black, and
the node is a right child - Set z to point to parent of new node.
- Left-Rotate(T, z). Newly added nodes parent.
- Case 2 becomes Case 3
- Case 3 the newly added nodes uncle is black, and
the node is a left child. - Set zs parent to black.
- Set zs grandparent to red.
- Right-Rotate(T, Grandparent of z).
- Once case 2 or 2 and 3 have been executed, the
tree is balanced.
Note The importance of left vs. right child is
in the number of rotations needed. z initially
refers to the new node, but this will change
during the fixup procedures
16Example of cases 2 and 3
- Case 2 the newly added nodes uncle is black, and
the node is a right child. - Case 3 the newly added nodes uncle is black, and
the node is a left child. - The importance of left vs. right child is the
number of rotations needed. - Case 2 requires 2 rotations. It first executes a
Left-Rotation, then goes into case 3 which
performs some color changes before performing a
Right-Rotation.
17Deletion
- Deletion from a Binary Search tree
- IF the node to delete has 2 children, we find the
successor node, move its information into the
node we want to delete, and remove the successor
node. - ELSE we remove the node with the given value.
- In either case, the node that is actually removed
will have 1 or 0 children. - If it has a child, we set the childs parent to
the parent of the removed node. Else, if no
children, we set the nil (leaf) to be the child
of the parent of the removed node. - We pass this child (or the nil (leaf) into the
fix-up procedure), if this procedure is needed.
18Deletion
- Deletion from a red black tree, is similar to
deletion for a binary search tree, with a few
exception - Always set the parent of a deleted node, to be
the parent of one of the deleted nodes children. - Red black fix-up method called if removed node is
black. - After a deletion of a red node (no violations
occur) - No black-heights have been affected.
- The simple path from any node to a leaf contain
the same of black nodes. - No red nodes have been made adjacent (parent and
child both red). - Deleted node is not the root since the root is
black.
19Delete Cont
- After Deletion of a Black node a restore function
must be called to fix red-black properties that
might be violated. There are 3 possible initial
violations. - If deleted node was the root, a red child might
now be the root, Violation of property 2. - If both the parent of removed node, and a child
of removed node are red, we have a violation of
property 4. - The removal of a black node can cause the
black-height of one path to be shorter (by 1),
violating property 5. - We correct the problem of rule 5 by adding an
extra black to the node passed into the fix-up
procedure. This leads to violations in rules 1
since this node is now neither red or black. - The goal now is to move the extra black to the
root of the tree, and then get rid of the extra
black.
20Delete Fix-up Cases
- There are 8 possible cases. Since four of these
are mirror images of the other 4 (depending on if
the node passed is a left or right child), I will
show the four cases for the node being a left
child. - Note x is initially points to the node passed in
to the fix up procedure (a child of the deleted
node). But, x will change during the fix up. - x points to either a doubly black or a red/black
node. - In this case, the color attribute is considered
red or black, but in terms of counting the black
path, it counts as one or 2 depending on if it is
red/black or doubly black. - At the end of the of the fix-up procedure x is
set to the root, and set Black. This removes the
extra black from the tree.
21Delete Fix-up Pseudo-Code
22Case 1
- Occurs when the sibling of the passed in node is
red. - This mean
- Siblings parent and children must be black
otherwise a red-black property violation would
already have occurred. - Steps
- Color the sibling (node D) black.
- Color the parent (node B) red.
- Left-rotate around parent and sibling (B-D)
- Reset the pointer to point to the new right child
of the node. - Note
- Case 1 will turn into one of the other 3 cases.
23Case 2
- Occurs
- If sibling node is black, and both of the
siblings children are black. - Steps
- Set the sibling to red (node D)
- Set the pointer x to point to the parent (node B)
- this moves the extra black up the tree 1
position. Node A is now carries the extra black
and is pointed to by pointer x. - Re-enter the while loop with x pointing to the
parent. -
24Case 3
- Occurs
- If the sibling is black, the right child of the
sibling is black, (and the left child of the
sibling is red). - Steps
- Set the left child of the sibling (node C) to
black. - Set the sibling (node D) to red.
- Right-Rotate on the sibling (D-C)
- Case 3 turns into case 4.
25Case 4
- Occurs
- If the sibling is black, the right child of the
sibling is red. The color of the siblings left
child can be red or black. - Steps
- Set the sibling equal to the color of the parent.
- Set the parent (node B) to black.
- Set the right child (node E) of sibling to black.
- Left-Rotate around the parent (B-D)
- Set the pointer x Tree Root.
- In the end, set x to Black. This removes the
extra black
264 cases of Delete Fix-up
Keys Dark black Shaded red White red/black
(does not matter)
27Search Analysis
- Search
- Since the red black tree is a balanced binary
search tree, the height is about log n. To find
any node, at most takes log n steps, with an
elementary compare at each step.
28Insert Analysis
- Insert
- Red Black Insert takes O(log n) since the height
of the tree is log n. - But what about the Red Black Insert Fix-up?
- The while loop repeats if case 1 is executed,
(but terminates if case 2 or 3 are executed).
When case 1 is executed, the pointer move up the
tree two positions for each iteration of the
loop. So, the while loop can be performed log n
time at most. - Red Black Insert Fix-up runs in O(log n)
- Only 2 rotations (at most) will be executed.
29Delete Analysis
- Delete
- Deletion of a node (without the fix-up) takes,
O(log n) since the height of the tree is log n. - But, what about Red Black Delete Fix-up?
- If case 1 is performed, it becomes case 2, 3 or
4. - The while loop will only be repeated if case 2 is
executed. In this case the pointer is moved up
the tree log n times at most. - The other cases require at most 3 rotations to
restore the properties. - Red Black Delete Fix-up runs in O(log n)
- At most, 3 rotations will be performed.
30Applications where used
- Because Red Black Trees offer the best possible
worst case running time for insertion, deletions
and search, they are useful in - real-time applications
- Systems in which a real-time constraint i.e.
operational deadline from event to system
response is important - As building blocks in other data structures.
- Red-black trees in use in the kernel.
- The anticipatory, deadline, and CFQ I/O
schedulers all use red black tree to track
requests - The packet CD/DVD driver does the same.
- The high-resolution timer code uses an red black
trees to organize outstanding timer requests. - Virtual memory areas (VMAs) are tracked with
red-black trees
31Comparison to other Algorithms
- Binary Search Trees (on which Red Black Trees are
based on) use the same search method, and similar
insert and delete methods. - In a Binary Search Tree, these methods run in
O(h) where h height. Because the tree is not
self balancing - Red Black trees perform these operations in O(log
n) since they do self balance. Although, the red
black trees might need to perform a restore
procedure that also runs in O(log n). - Balanced Binary trees (such as red black trees)
have 2 advantages over Hash tables - The contents of a balanced binary tree can be
converted into a list sorted by key in time
proportional to the number of associations within
the tree - Two binary trees can be compared for equality in
linear time. - Arrays, insert to end in the order of theta(1) a
constant, but, to insert into a specific sorted
order it runs in O(n). Find also runs in O(n).
32Bibliography
- Introduction to Algorithms (second Edition)
- Thomas H. Cormen, Charles E. Leiserson, Rondald
L. Rivest, Clifford Stein. - Wikipedia (website)
- http//en.wikipedia.org/wiki/Red-black_tree
- http//highered.mcgraw-hill.com/sites/dl/free/0070
131511/25328/chapter13.ppt269 - http//gauss.ececs.uc.edu/Users/Franco/RedBlackTes
ter/redblack.html