OK!!! we have seen that Binary Search Trees (BST) have a nice property the, insertion, deletions, minimum and maximum depends on the height of the tree. However, BST do not have the ability to keep a well balanced tree unless you are willing to randomize the initial input and have only insertions (Cormen’s Chapter 12.4). This is clearly not the case for the average application that uses Binary Trees (Look at “Linux Kernel Development”). Therefore, we require an auto-balancing data structure. This is accomplished by extending the BST into Red-Black Trees (RBT).
A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black.
A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
A red-black tree is a type of self balancing binary search tree, with one extra attribute for each node: the colour, which is either red or black.
A red–black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
red black tree definition,operation,searching,insertion into red black tree,left child,right child,parent,color,key,red,black,nodes,correction,sibling,binary search tree,deletion for red black tree
Short overview of balance tree: Data Structures, Binary search tree, BST Problem, Self Balancing BST, Usage, Red Black Trees, Red Black Insertion, AVL Tree, Rotations, B-Tree
red black tree definition,operation,searching,insertion into red black tree,left child,right child,parent,color,key,red,black,nodes,correction,sibling,binary search tree,deletion for red black tree
Short overview of balance tree: Data Structures, Binary search tree, BST Problem, Self Balancing BST, Usage, Red Black Trees, Red Black Insertion, AVL Tree, Rotations, B-Tree
Presentation for the project of "INFO-F413 Data structures and algorithms" (ULB).
From the article "Planar Point Location Using Persistent Search Trees" of Neil Sarnak and Robert E. Tarjan.
https://bitbucket.org/OPiMedia/persistent-search-trees
Senior data scientist and founder of the company Intelligentia Data I+D SA de CV. We are offering consultancy services, development of projects and products in Machine Learning, Big Data, Data Sciences and Artificial Intelligence.
My first set of slides (The NN and DL class I am preparing for the fall)... I included the problem of Vanishing Gradient and the need to have ReLu (Mentioning btw the saturation problem inherited from Hebbian Learning)
It has been almost 62 years since the invention of the term Artificial Intelligence by Samuel and Minsky et al. at the Dartmouth workshop College in 1956 (“Dartmouth Summer Research Project on Artificial Intelligence”) where this new area of Computer Science was invented. However, the history of Artificial Intelligence goes back to previous millennia, when the Greeks in their Myths spoke about golden robots at Hephaestus, and the Galatea of Pygmalion. They were the first automatons known at the dawn of history, and although these first attempts were only myths, automatons were invented and built through multiple civilizations in history. Nevertheless, these automatons resembled in quite limited way their final objectives, representing animals and humans. In spite of that, the greatest illusion of an automaton, the Turk by Wolfgang von Kempelen, inspired many people, trough its exhibitions, as Alexander Graham Bell and Charles Babbage to develop inventions that would change forever human history. Thus, the importance of the concept “Artificial Intelligence” as a driver of our technological dreams. And although Artificial Intelligence has never been defined in a precise practical way, the amount of research and methods that have been developed to tackle some of its basics tasks have been and are quite humongous. Thus, the importance of having an introduction to the concepts of Artificial Intelligence, thus the dream can continue.
A review of one of the most popular methods of clustering, a part of what is know as unsupervised learning, K-Means. Here, we go from the basic heuristic used to solve the NP-Hard problem to an approximation algorithm K-Centers. Additionally, we look at variations coming from the Fuzzy Set ideas. In the future, we will add more about On-Line algorithms in the line of Stochastic Gradient Ideas...
Here a Review of the Combination of Machine Learning models from Bayesian Averaging, Committees to Boosting... Specifically An statistical analysis of Boosting is done
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
CFD analysis is incredibly effective at solving mysteries and improving the performance of complex systems!
Here's a great example: At a large natural gas-fired power plant, where they use waste heat to generate steam and energy, they were puzzled that their boiler wasn't producing as much steam as expected.
R&R and Tetra Engineering Group Inc. were asked to solve the issue with reduced steam production.
An inspection had shown that a significant amount of hot flue gas was bypassing the boiler tubes, where the heat was supposed to be transferred.
R&R Consult conducted a CFD analysis, which revealed that 6.3% of the flue gas was bypassing the boiler tubes without transferring heat. The analysis also showed that the flue gas was instead being directed along the sides of the boiler and between the modules that were supposed to capture the heat. This was the cause of the reduced performance.
Based on our results, Tetra Engineering installed covering plates to reduce the bypass flow. This improved the boiler's performance and increased electricity production.
It is always satisfying when we can help solve complex challenges like this. Do your systems also need a check-up or optimization? Give us a call!
Work done in cooperation with James Malloy and David Moelling from Tetra Engineering.
More examples of our work https://www.r-r-consult.dk/en/cases-en/
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Welcome to WIPAC Monthly the magazine brought to you by the LinkedIn Group Water Industry Process Automation & Control.
In this month's edition, along with this month's industry news to celebrate the 13 years since the group was created we have articles including
A case study of the used of Advanced Process Control at the Wastewater Treatment works at Lleida in Spain
A look back on an article on smart wastewater networks in order to see how the industry has measured up in the interim around the adoption of Digital Transformation in the Water Industry.
2. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
2 / 149
3. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
3 / 149
4. Images/ITESM.p
Well Balanced Trees
Do you remember AVL (Adelson-Velskii and Landis) Trees?
Quite nice recursive methods for balancing the tree!!!
It is based on an height Invariant
At any node in the tree, the heights of the left and right subtrees
differs by at most 1.
4 / 149
5. Images/ITESM.p
Well Balanced Trees
Do you remember AVL (Adelson-Velskii and Landis) Trees?
Quite nice recursive methods for balancing the tree!!!
It is based on an height Invariant
At any node in the tree, the heights of the left and right subtrees
differs by at most 1.
4 / 149
6. Images/ITESM.p
Thus, it is necessary to add an extra field to the Node
Structure
Structure of a Node
Structure 1: Struct Node
1 Key key
2 int height
3 Value val
4 Node Left, Right
5 / 149
7. Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
8. Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
9. Images/ITESM.p
Observations
Due to the balancing methods
AVL Trees requiere to have a O (log2 n) rotations.
AVL Trees operations cost O (log2 n).
There is a problem about the height
How much memory it is necessary to allocate for the height field in
massive binary search trees?
6 / 149
10. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
11. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
12. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
13. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
14. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
15. Images/ITESM.p
Furthermore
Arguments in favor of AVL Trees
Search is O (log N) since AVL trees are always balanced.
Insertion and deletions are also O (log N).
Arguments against using AVL trees
Difficult to program and debug.
The dynamic space nature of the balancing (Height) factor.
Asymptotically faster but re-balancing costs time.
Most large searches are done in database systems on disk and use
other structures (e.g. B-trees).
7 / 149
16. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
8 / 149
17. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
18. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
19. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
20. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
21. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
22. Images/ITESM.p
Definitions
Definition
A Red Black Tree is a Binary Search Tree where each node has an extra
field, its color.
Properties
1 Every node is either red or black.
2 The root is black.
3 Every leaf (NIL) is black.
4 If a node is red, then both its children are black.
5 For each node, all paths from the node to descendant leaves contain
the same number of black nodes.
9 / 149
24. Images/ITESM.p
Height on a Red Black Tree
Black Height bh(x)
We call the number of black nodes on any path from, but not including, a
node x down to a leaf the black height of the node.
11 / 149
25. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
12 / 149
26. Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
27. Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
28. Images/ITESM.p
Lemma for the height of Red-Black Trees
Theorem
A Red-Black Trees with n internal nodes has height at most 2 log(n + 1).
Proof: Step 1
Prove that any subtree rooted at x contains at least 2bh(x) − 1
internal nodes.
If bh(x) = 0, then
13 / 149
29. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
14 / 149
34. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
18 / 149
36. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
37. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
38. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
39. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
40. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
41. Images/ITESM.p
Proof
Case I
Height of child is bh(x).
Then, the child is red, if not subtree rooted at x will have height
bh(x) + 1!
Now, we have two subtrees from the child with red root...(The
children are black)
Thus, we have
Thus, each of this subtrees has height bh(x) − 1 ⇒ each tree
contains at least 2bh(x)−1 − 1 nodes by inductive hypothesis.
Then the child contains at least 2bh(x)−1 − 1 + 2bh(x)−1 − 1 internal
nodes.
Finally, the tree rooted contains 2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 (One
for the node rooted at the child node)
20 / 149
42. Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
43. Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
44. Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
45. Images/ITESM.p
Proof
Finally
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Case II
Height of the child is bh(x) − 1.
Now, we have two subtrees with at least 2bh(x)−1 − 1 nodes by
inductive hypothesis.
Thus, we have that the tree with root x contains at least
2bh(x)−1 − 1 + 2bh(x)−1 − 1 + 1 internal nodes.
21 / 149
46. Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
47. Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
48. Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
49. Images/ITESM.p
Proof
Again
Then, the tree with root x contains at least
2 × 2bh(x)−1 − 1 = 2bh(x) − 1
Finally
Now given that h is the height of the tree, we have by property 4 that
bh(T) ≥ h
2 .
Then n ≥ 2bh(root) − 1 ≥ 2
h
2 − 1.
Then, h ≤ 2 log(n + 1) .
22 / 149
50. Images/ITESM.p
Lemma for the height of Red-Black Trees
Corollary
From the previous theorem we conclude that SEARCH, MINIMUM,
etcetera, can be implemented in O(log n).
23 / 149
51. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
24 / 149
52. Images/ITESM.p
Rotations in Red-Black Trees
Purpose
Rotations are used to maintain the structure of the Red-Black Trees.
Types of rotations
There are left and right rotations, they are inverse to each other.
25 / 149
53. Images/ITESM.p
Rotations in Red-Black Trees
Purpose
Rotations are used to maintain the structure of the Red-Black Trees.
Types of rotations
There are left and right rotations, they are inverse to each other.
y
x
y
x
Left-Rotate(T,x)
Right-Rotate(T,y)
25 / 149
54. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
55. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
56. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
57. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
58. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
59. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
60. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
61. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
62. Images/ITESM.p
Example of Rotations in Red-Black Trees
LEFT-ROTATE(T,x)
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
3 if y.left = T.nil
4 y.left.p = x
5 y.p = x.p Link x’s parent to y
6 if x.p == T.nil
7 T.root = y
8 elseif x == x.p.left
9 x.p.left = y
10 else x.p.right = y
11 y.left = x Put x on y’s left
12 x.p = y
26 / 149
63. Images/ITESM.p
Example Left-Rotate
Step 1 - the correct right child is changed
1 y = x.right Set y
2 x.right = y.left Turn y’s left subtree into x’s right subtree
y
x
27 / 149
65. Images/ITESM.p
Example Left-Rotate
Step 3
6. if x.p == T.nil Set y to be the root if x was it
7. T.root = y
8. elseif x == x.p.left
9. x.p.left = y
10. else x.p.right = y
y
x
29 / 149
67. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
31 / 149
70. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
33 / 149
71. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
First
Search Variables being Initialized.
34 / 149
72. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x =
x.left
7. else x =
x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Second
Binary search for insertion.
35 / 149
73. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Third
Change parent of the node to be inserted.
36 / 149
74. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Fourth
Test to see if the Tree is empty!!!
37 / 149
75. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Fifth
Insert node z in the correct left or right child:
if z.key < y.key ⇒ y.left = z
if z.key ≥ y.key ⇒ y.right = z
38 / 149
76. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Sixth
Make z’s leafs equal to T.nil.
39 / 149
77. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Seventh
Make z’s color equal to RED.
40 / 149
78. Images/ITESM.p
Insertion Code in Red-Black Trees
RB-INSERT(T, z)
1. y = T.nil
2. x = T.root
3. while x=T.nil
4. y = x
5. if z.key<x.key
6. x = x.left
7. else x = x.right
8. z.p = y
9. if y == T.nil
10. T.root = z
11. elseif z.key < y.key
12. y.left = z
13. else y.right = z
14. z.left = T.nil
15. z.right = T.nil
16. z.color = T.RED
17. RB-Insert-Fixup(T.z)
Eight
Call RB-Insert-Fixup!!!
41 / 149
79. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
42 / 149
80. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
81. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
82. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
83. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
84. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
85. Images/ITESM.p
Tree-Insert Invariance
Initialization:
Prior to the first iteration of the loop, we started with a Red-Black
Trees with no violations.
Then, the algorithm insert the red node z at the bottom of the
Red-Black Trees.
This does not violate properties 1,3 and 5.
The RB-Insert-Fixup is called because the following possible violations
1 If z is the first node to be inserted, you violate the property 2.
2 Then, it is the only violation on the entire Red-Black Trees.
3 If z is not the first node to be inserted than you could be violating
property 4.
43 / 149
86. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
44 / 149
87. Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 1
z’s uncle is RED
Change of parent and uncle’s color to
BLACK
Move problem to z’s grandfather
45 / 149
90. Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
91. Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
92. Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
93. Images/ITESM.p
Thus
Observations
1 Recoloring will fix the z.parent.color == red and keeps the bh
property
1 It will move the problem upwards.
2 Nevertheless, the tree rooted at A, B and D are Red-Black Trees.
3 So the new z will move the problem upward for the next iteration.
48 / 149
96. Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 2
if z is in the right child then
Move the problem to the parent by
making z = z.p
Rotate left using z as the rotation
node
51 / 149
102. Images/ITESM.p
RB-Insert-Fixup
RB-Insert-Fixup(T,z)
1 while z.p.color == RED
2 if z.p == z.p.p.left
3 y=z.p.p.right
4 if y.color ==RED
5 z.p.color = BLACK
6 y.color = BLACK
7 z.p.p.color = RED
8 z = z.p.p
9 else if z == z.p.right
10 z = z.p
11 Left-Rotate(T, z)
12 z.p.color = BLACK
13 z.p.p.color = RED
14 Right-Rotate(T, z.p.p)
15 else (“right” and “left” exchanged)
16 T.root.color = BLACK
Case 3
if z is in the left child then
Recolor z’s parent to BLACK
recolor z’s grandparent to RED
Rotate right using the grandparent
55 / 149
103. Images/ITESM.p
Then
We do the following
Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ
are black nodes)
Finally
Then, you rotate right using z.p.p to fix the black height property!!
56 / 149
104. Images/ITESM.p
Then
We do the following
Then, we recolor B.color = BLACK, C.color = RED (No problem γ and δ
are black nodes)
Finally
Then, you rotate right using z.p.p to fix the black height property!!
56 / 149
105. Images/ITESM.p
From Case 3 to final recoloring
Case 3 - It allows to fix the bh locally
z
y
A
B
CASE 3
B
A C
C
57 / 149
107. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
59 / 149
109. Images/ITESM.p
Example: Insertion in Red-Black Trees
The root becomes red!!!
STEPS
1 y = T.nil
2 x = T.root
3 We never go into
the search loop
4 z.p = y
5 if y == T.nil
6 T.root = z
7 z.left = T.nil
8 z.right = T.nil
9 z.color = T.RED
26 1741 14 21 10 712
30
28
T.nil
Selection Order
z
61 / 149
110. Images/ITESM.p
Example: Insertion in Red-Black Trees
Fix the problem
ONLY STEP
1 T.root.color=BLACK
It happens at the end of the
Fix-up
26 1741 14 21 10 712
30
28
T.nil
Selection Order
z
62 / 149
111. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 7
1 Do a binary
search to find a
place to insert
2 Parent of z is not
RED ⇒ no fix-up
26 1741 14 21 10
7
12
30
28
T.nil
Selection Order
z
63 / 149
112. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 12
1 Do a binary
search to find a
place to insert
2 Parent of z is
RED!!!
26 1741 14 21 10
7
12
30
28
T.nil
Selection Order
z
64 / 149
113. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 2 into Case 3
1 Re-balance first to the
left
if z==z.p.right
z=z.p
Left-Rotate(T, z)
26 1741 14 21 10
712
30
28
T.nil
Selection Order
7z
65 / 149
114. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26 1741 14 21 10
712
30
28
T.nil
Selection Order
7z
66 / 149
115. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 10
1 We insert 10
2 We color it to RED
3 We need to fix the
problem
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
y
67 / 149
116. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if z.p == z.p.p.left
2 y=z.p.p.right
3 if y.color ==RED
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
y
68 / 149
117. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if y.color ==RED
2 z.p.color = BLACK
3 y.color = BLACK
4 z.p.p.color = RED
5 z = z.p.p
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
69 / 149
118. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 You get out of the loop and...
1 T.root.color = BLACK
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
70 / 149
119. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 28
1 We insert 28
2 We color it to RED
3 No problem to fix...
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
71 / 149
120. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 28
1 We insert 28
2 We color it to RED
3 No problem to fix...
26 1741 14 21
10
712
30
28
T.nil
Selection Order
7
z
72 / 149
121. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 21
1 We insert 21
2 We color it to RED
3 We need to fix...
26 1741 14
21
10
712
30
28
T.nil
Selection Order
7
z
73 / 149
122. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26 1741 14
2110
712
30
28
T.nil
Selection Order
7
z
74 / 149
123. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 14
1 We insert 14
2 We color it to RED
3 We need to fix...
26 1741
14
2110
712
30
28
T.nil
Selection Order
7
z
75 / 149
124. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if z.p == z.p.p.left
2 y=z.p.p.right
3 if y.color ==RED
26 1741
14
2110
712
30
28
T.nil
Selection Order
7
z
y
76 / 149
125. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1
1 if y.color ==RED
2 z.p.color = BLACK
3 y.color = BLACK
4 z.p.p.color = RED
5 z = z.p.p
Nothing to do after!!!
26 1741
14
2110
712
30
28
T.nil
Selection Order
7 z
77 / 149
126. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 17
1 Do a binary search to find a
place to insert
2 Parent of z is RED!!!
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7 z
z
78 / 149
127. Images/ITESM.p
Case 2 into 3
1 Re-balance first to the left
if z==z.p.right
z=z.p
Left-Rotate(T, z)
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
79 / 149
128. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
79 / 149
129. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
2110
712
30
28
T.nil
Selection Order
7
z
80 / 149
130. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3
1 Re-color and
re-balance to the right
z.p.color = BLACK
z.p.p.color=RED
Right-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already
BLACK so nothing
happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
81 / 149
131. Images/ITESM.p
Example: Insertion in Red-Black Trees
Insert 41
1 Put in the correct
node by binary
search
2 Because 41’s parent
is BLACK nothing to
fix
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
82 / 149
132. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 Symmetrical
1 if z.p == z.p.p.right
2 y=z.p.p.left
3 if y.color ==RED
4 z.p.color = BLACK
5 y.color = BLACK
6 z.p.p.color = RED
7 z = z.p.p
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
83 / 149
133. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 1 Symmetrical
1 if z.p == z.p.p.right
2 y=z.p.p.left
3 if y.color ==RED
4 z.p.color = BLACK
5 y.color = BLACK
6 z.p.p.color = RED
7 z = z.p.p
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
84 / 149
134. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 2 to case 3 - Symmetrical
1 Re-balance first to the right
if z==z.p.left
z=z.p
Right-Rotate(T, z)
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7 z
85 / 149
135. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3 - Symmetrical
1 Re-color and re-balance to the
right
z.p.color = BLACK
z.p.p.color=RED
Left-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already BLACK so
nothing happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7 z
86 / 149
136. Images/ITESM.p
Example: Insertion in Red-Black Trees
Case 3 - Symmetrical
1 Re-color and re-balance to the
right
z.p.color = BLACK
z.p.p.color=RED
Left-Rotate(T, z.p.p)
2 No problem to fix
3 The root is already BLACK so
nothing happens
26
17
41
14
21
10
712
30
28
T.nil
Selection Order
7
z
87 / 149
138. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
89 / 149
139. Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
140. Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
141. Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
142. Images/ITESM.p
The Main Idea
Here, we use the idea of removing
By pushing in its place its successor.
Therefore we have a problem
If the successor of a node is the node y.
And y is black.
We have removed a black node from a path.
90 / 149
144. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 1
Store the info of the node to be
deleted
92 / 149
145. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 2
If the left child is empty
Store the info of the right child
Move z.right into the position of
z
93 / 149
146. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 3
If the right child is empty
Store the info of the left child
Move z.left into the position of z
94 / 149
147. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 4
Find the successor of z
Store the info of it: Color and
right child
95 / 149
148. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
Case 5
If parent of succesor is z then
set parent of x to y
96 / 149
149. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
1 y = z
2 y-original-color = y.color
3 if z.left == T.nil
4 x = z.right
5 RB-Transplant(T, z, z.right)
6 elseif z.right == T.nil
7 x = z.left
8 RB-Transplant(T, z, z.left)
9 else y = Tree-Minimum(z.right)
10 y-original-color = y.color
11 x = y.right
12 if y.p == z
13 x.p = y
14 else RB-Transplant(T,y,y.right)
15 y.right = z.right
16 y.right.p = y
ø
Case 6
Substitute y with y.right
set y.right with z.right
set the parent of y.right to y
97 / 149
150. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
17. RB-Transplant(T, z, y)
18. y.left = z.left
19. y.left.p = y
20. y.color = z.color
21. if y-original-color ==BLACK
22. RB-Delete-Fixup(T,x)
Case 7
Substitute z with y
Make y.left to z.left
Make the parent of y.left to y
Make the color of y to the color
of z
98 / 149
151. Images/ITESM.p
Deletion in Red-Black Trees
RB-DELETE(T,z)
17. RB-Transplant(T, z, y)
18. y.left = z.left
19. y.left.p = y
20. y.color = z.color
21. if y-original-color ==BLACK
22. RB-Delete-Fixup(T,x)
Case 8
If y-original-color == BLACK
then call RB-Delete-Fixup(T,x)
After all x points to the node
that:
It is moved into the position
of y.
Where y was moved into the
position of z.
99 / 149
153. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
101 / 149
154. Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
155. Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
156. Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
157. Images/ITESM.p
A virtual node y to be removed or moved around
Case 1
In line 1, y is removed when it points to z and has less than two
children.
Case 2
In line 9, y is moved around when z has two children
Because y=Tree-Minimum(z.right).
Then
y will move to z’s position.
102 / 149
158. Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
159. Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
160. Images/ITESM.p
A virtual node y to be removed or moved around
Now, the color of y’s can change
Therefore, it gets stored in y-original-color (Lines 2, 10).
Then, when z has two children
Then y moves to z’s position and y gets the same color than z.
This can produce a violation.
103 / 149
161. Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
162. Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
163. Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
164. Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
165. Images/ITESM.p
The node x gets position y
In lines 4, 7, and 11, x is set to point to
to y’s only child or
T.nil
Since x is going to move to y’s original position
The x.p is pointed to y’s parent.
Unless z is y’s original parent
The assignment of x.p takes place in line 6 of RB-Transplant.
Observe that when RB-Transplant is called in lines 5, 8, or 14, the
second parameter passed is the same as x.
104 / 149
166. Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
167. Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
168. Images/ITESM.p
The node x gets position y
if z is not the original y’s parent
We do not want x.p to point to it since we are going to remove it.
Then
In line 13 of RB-Delete, x.p is set to point to y.
Finally
y will take the position of z in line 17.
105 / 149
169. Images/ITESM.p
The node x gets position y
Then
If y was originally black after taking the z.color can produce a violation,
then RB-Delete-Fixup is called.
This can happen
If y was originally red the Red-Black Trees properties still hold.
106 / 149
170. Images/ITESM.p
The node x gets position y
Then
If y was originally black after taking the z.color can produce a violation,
then RB-Delete-Fixup is called.
This can happen
If y was originally red the Red-Black Trees properties still hold.
106 / 149
172. Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
173. Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
174. Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
175. Images/ITESM.p
Explanation
We have three problems
1 If y was a root and a RED child becomes the new root, we have
violated property 2.
2 If both x and x.p are RED, then we have violated property 4.
3 Moving y around decreases the black-height on a section of the Red
Black Tree.
4 Thus, Property 5 is violated.
108 / 149
176. Images/ITESM.p
Fix-up
How?
This could be fixed assuming that x has an “extra black.”
Meaning
This means that the node is “doubly black” or “red-and-black.”
109 / 149
177. Images/ITESM.p
Fix-up
How?
This could be fixed assuming that x has an “extra black.”
Meaning
This means that the node is “doubly black” or “red-and-black.”
109 / 149
179. Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
180. Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
181. Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
182. Images/ITESM.p
How is this done?
Thus
The procedure RB-DELETE -FIXUP restores properties 2, 4, and 5 by
using the while loop to push the extra BLACK node up the tree.
Until
If we have that x is a red-and-black, we simply need to change the
color of the node to BLACK (Line 23).
Then, use Rotations
Use suitable rotations and re-colorings until x stops to be a doubly
black node.
If we have that x is pointing to the root, remove “extra node.”
111 / 149
183. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
112 / 149
184. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
While loop
Because a violation
on the bh, you need
to move x up until
the problem is fixed
up.
113 / 149
185. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Finding who you are
Find which child are
you
Make w the other
child
114 / 149
186. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 1
if x is BLACK and w
is RED
Fix the bh problem
by making w
BLACK, x’s parent
to RED then rotate
left using x’s parent
Make w = x.p.right
moving the problem
down.
115 / 149
187. Images/ITESM.p
Suitable Rotations and Recoloring
Case 1 - x’s sibling w is red. You keep the bh property of the other
subtrees
x wA
B
C
D
E
116 / 149
188. Images/ITESM.p
Suitable Rotations and Recoloring
Case 1 - x’s sibling w is red. You keep the bh property of the other
subtrees
x new wA
B
C
D
E
117 / 149
189. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 2
Now if w.left’s color
and w.right’s color is
BLACK
We do something
smart decrease the
bh height at w by
making w’s color to
RED
Move the problem
fropm to x to x.p
(After all the
subtree have the
same height at x.p)
118 / 149
190. Images/ITESM.p
Suitable Rotations and Recoloring
Case 2 - x’s sibling w is black, and both of w’s children are black.
x wA
C
D
E
B
Note: The Node with half and half colors has the meaning that it
can be red or black.
119 / 149
192. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 3
If w.right’s color is
BLACK
We do something
smart, we re-color
and do a right
rotation at w
This does not
change the
Red-Black Trees
properties of w
but prepare the
situation for fixing
the x height problem
in case 4.
121 / 149
193. Images/ITESM.p
Suitable Rotations and Recoloring
Case 3 - x’s sibling w is black, w’s left child is red, and w’s right child
is black.
x A
C
D
E
B
w
122 / 149
194. Images/ITESM.p
Suitable Rotations and Recoloring
Case 3: x’s sibling w is black, w’s left child is red, and w’s right child
is black.
x A C
D
E
B
new w
123 / 149
195. Images/ITESM.p
RB-DELETE-FIXUP(T,x)
1 while x = T.root and x.color == BLACK
2 if x == x.p.left
3 w = x.p.right
4 if w.color == RED
5 w.color = BLACK
6 x.p.color = RED
7 Left-Rotate(T, x.p)
8 w = x.p.right
9 if w.left.color == BLACK and w.right.color == BLACK
10 w.color = RED
11 x = x.p
12 else if w.right.color == BLACK
13 w.left.color = BLACK
14 w.color = RED
15 Right-Rotate(T, w)
16 w = x.p.right
17 w.color = x.p.color
18 x.p.color = BLACK
19 w.right.color = BLACK
20 Left-Rotate(T, x.p)
21 x = T.root
22 else (same with “right” and “left” exchanged)
23 x.color = BLACK
Case 4
We are ready to fix
our problem!!! with
respect to x (Case 2
and 3 where a
preparation to fix
the problem)
We increase the
height of the bh
with the problem, x.
124 / 149
198. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
127 / 149
200. Images/ITESM.p
Deletion in Red-Black Trees
Store the info about z
y = z
y-original-color = y.color 26
17
41
14 21
10
712
30
28
T.nil
7
z
129 / 149
201. Images/ITESM.p
Deletion in Red-Black Trees
None of the children of z are T.nil, thus
else y = Tree-Minimum(z.right)
y-original-color = y.color
x = y.right 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
130 / 149
202. Images/ITESM.p
Deletion in Red-Black Trees
We have that y.p= z
else RB-Transplant(T,y,y.right)
y.right = z.right
y.right.p = y 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
131 / 149
203. Images/ITESM.p
Deletion in Red-Black Trees
Transplant(T, y, y.right)
elseif y == y.p.left
y.p.left = y.right
y.right.p = y.p
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
y.right.p=y.p
132 / 149
204. Images/ITESM.p
Deletion in Red-Black Trees
Now, we move pointers
y.right = z.right
y.right.p = y 26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
y.right.p=y
133 / 149
205. Images/ITESM.p
Deletion in Red-Black Trees
Transplant(z,y)
if z.p == T.nil
T.root = y
y.p = z.p
26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
y.p = z.p
134 / 149
206. Images/ITESM.p
Deletion in Red-Black Trees
Move pointers
y.left = z.left
y.left.p = y
y.color = z.color 26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
y.left.p = y
135 / 149
207. Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
z.right = NULL
z.left = NULL
z.parent = NULL
26
17
41
14
21
10
712
30
28
T.nil
7
z y
x
136 / 149
208. Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
Never enter into the loop
Simply do x.color =
BLACK
26
41
14
21
10
712
30
28
T.nil
7
y
x
137 / 149
210. Images/ITESM.p
Deletion in Red-Black Trees
None of the children of z are T.nil, thus
else y = Tree-Minimum(z.right)
y-original-color = y.color
(BLACK)
x = y.right (T.NIL)
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
139 / 149
212. Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
141 / 149
213. Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
142 / 149
214. Images/ITESM.p
Deletion in Red-Black Trees
Next
RB-Transplant(T, z, y)
y.left = z.left
y.left.p = y
y.color = z.color
26
17
41
14 21
10
712
30
28
T.nil
7
z
y
x
143 / 149
215. Images/ITESM.p
Deletion in Red-Black Trees
We remove z safely and we go into Delete-Fixup(T,x)
enter into the loop
if x == x.p.right
w = x.p.left
26
17
41
14
21
10
30
28
T.nil
7
y
x
w
144 / 149
216. Images/ITESM.p
Deletion in Red-Black Trees
We enter into case 4
1 w.color = x.p.color
2 x.p.color = BLACK
3 w.right.color = BLACK
26
17
41
14
21
10
30
28
T.nil
7
x
w
145 / 149
217. Images/ITESM.p
Deletion in Red-Black Trees
Rotate and move the x
1 Left-Rotate(T, x.p)
2 x = T.root 26
17
41
14
21
10 30
28
T.nil
7
x
146 / 149
218. Images/ITESM.p
Applications of Red-Black Trees
Completely Fair Scheduler (CFS)
It is a task scheduler which was merged into 2.6.23 release of the
Linux Kernel.
It is a replacement of earlier O(1) scheduler.
CFS algorithm was designed to maintain balance (fairness) in
providing processor time to tasks.
Sorting using Parallel Implementations
Running in O(log log n) time
147 / 149
219. Images/ITESM.p
Applications of Red-Black Trees
Completely Fair Scheduler (CFS)
It is a task scheduler which was merged into 2.6.23 release of the
Linux Kernel.
It is a replacement of earlier O(1) scheduler.
CFS algorithm was designed to maintain balance (fairness) in
providing processor time to tasks.
Sorting using Parallel Implementations
Running in O(log log n) time
147 / 149
220. Images/ITESM.p
Outline
1 Red-Black Trees
The Search for Well Balanced Threes
Red-Black Trees
Lemma for the height of Red-Black Trees
Base Case of Induction
Induction
Rotations in Red-Black Trees
Insertion in Red-Black Trees
Insertion Code
The Insertion Invariance
The Fixup Code
Example
Deletion in Red-Black Trees
A Virtual Node y
The Code To Fix the Violations
Example of Deletion in Red-Black Trees
2 Exercises
Something for you to do
148 / 149