\(\) \(%\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Cormen et al}}\) \(\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Introduction to Algorithms, 3rd ed.} (\href{http://libproxy.aalto.fi/login?url=http://site.ebrary.com/lib/aalto/Doc?id=10397652}{online via Aalto lib})}\) \(\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Algorithms, 4th ed.}}\) \(%\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Sedgewick and Wayne}}\) \(\) \(\newcommand{\Java}{\href{http://java.com/en/}{Java}}\) \(\newcommand{\Python}{\href{https://www.python.org/}{Python}}\) \(\newcommand{\CPP}{\href{http://www.cplusplus.com/}{C++}}\) \(\newcommand{\ST}[1]{{\Blue{\textsf{#1}}}}\) \(\newcommand{\PseudoCode}[1]{{\color{blue}\textsf{#1}}}\) \(\) \(%\newcommand{\subheading}[1]{\textbf{\large\color{aaltodgreen}#1}}\) \(\newcommand{\subheading}[1]{\large{\usebeamercolor[fg]{frametitle} #1}}\) \(\) \(\newcommand{\Blue}[1]{{\color{flagblue}#1}}\) \(\newcommand{\Red}[1]{{\color{aaltored}#1}}\) \(\newcommand{\Emph}[1]{\emph{\color{flagblue}#1}}\) \(\) \(\newcommand{\Engl}[1]{({\em engl.}\ #1)}\) \(\) \(\newcommand{\Pointer}{\raisebox{-1ex}{\huge\ding{43}}\ }\) \(\) \(\newcommand{\Set}[1]{\{#1\}}\) \(\newcommand{\Setdef}[2]{\{{#1}\mid{#2}\}}\) \(\newcommand{\PSet}[1]{\mathcal{P}(#1)}\) \(\newcommand{\Card}[1]{{\vert{#1}\vert}}\) \(\newcommand{\Tuple}[1]{(#1)}\) \(\newcommand{\Implies}{\Rightarrow}\) \(\newcommand{\Reals}{\mathbb{R}}\) \(\newcommand{\Seq}[1]{(#1)}\) \(\newcommand{\Arr}[1]{[#1]}\) \(\newcommand{\Floor}[1]{{\lfloor{#1}\rfloor}}\) \(\newcommand{\Ceil}[1]{{\lceil{#1}\rceil}}\) \(\newcommand{\Path}[1]{(#1)}\) \(\) \(%\newcommand{\Lg}{\lg}\) \(\newcommand{\Lg}{\log_2}\) \(\) \(\newcommand{\BigOh}{O}\) \(%\newcommand{\BigOh}{\mathcal{O}}\) \(\newcommand{\Oh}[1]{\BigOh(#1)}\) \(\) \(\newcommand{\todo}[1]{\Red{\textbf{TO DO: #1}}}\) \(\) \(\newcommand{\NULL}{\textsf{null}}\) \(\) \(\newcommand{\Insert}{\ensuremath{\textsc{insert}}}\) \(\newcommand{\Search}{\ensuremath{\textsc{search}}}\) \(\newcommand{\Delete}{\ensuremath{\textsc{delete}}}\) \(\newcommand{\Remove}{\ensuremath{\textsc{remove}}}\) \(\) \(\newcommand{\Parent}[1]{\mathop{parent}(#1)}\) \(\) \(\newcommand{\ALengthOf}[1]{{#1}.\textit{length}}\) \(\) \(\newcommand{\TRootOf}[1]{{#1}.\textit{root}}\) \(\newcommand{\TLChildOf}[1]{{#1}.\textit{leftChild}}\) \(\newcommand{\TRChildOf}[1]{{#1}.\textit{rightChild}}\) \(\newcommand{\TNode}{x}\) \(\newcommand{\TNodeI}{y}\) \(\newcommand{\TKeyOf}[1]{{#1}.\textit{key}}\) \(\) \(\newcommand{\PEnqueue}[2]{{#1}.\textsf{enqueue}(#2)}\) \(\newcommand{\PDequeue}[1]{{#1}.\textsf{dequeue}()}\) \(\) \(\newcommand{\Def}{\mathrel{:=}}\) \(\) \(\newcommand{\Eq}{\mathrel{=}}\) \(\newcommand{\Asgn}{\mathrel{\leftarrow}}\) \(%\newcommand{\Asgn}{\mathrel{:=}}\) \(\) \(%\) \(% Heaps\) \(%\) \(\newcommand{\Downheap}{\textsc{downheap}}\) \(\newcommand{\Upheap}{\textsc{upheap}}\) \(\newcommand{\Makeheap}{\textsc{makeheap}}\) \(\) \(%\) \(% Dynamic sets\) \(%\) \(\newcommand{\SInsert}[1]{\textsc{insert}(#1)}\) \(\newcommand{\SSearch}[1]{\textsc{search}(#1)}\) \(\newcommand{\SDelete}[1]{\textsc{delete}(#1)}\) \(\newcommand{\SMin}{\textsc{min}()}\) \(\newcommand{\SMax}{\textsc{max}()}\) \(\newcommand{\SPredecessor}[1]{\textsc{predecessor}(#1)}\) \(\newcommand{\SSuccessor}[1]{\textsc{successor}(#1)}\) \(\) \(%\) \(% Union-find\) \(%\) \(\newcommand{\UFMS}[1]{\textsc{make-set}(#1)}\) \(\newcommand{\UFFS}[1]{\textsc{find-set}(#1)}\) \(\newcommand{\UFCompress}[1]{\textsc{find-and-compress}(#1)}\) \(\newcommand{\UFUnion}[2]{\textsc{union}(#1,#2)}\) \(\) \(\) \(%\) \(% Graphs\) \(%\) \(\newcommand{\Verts}{V}\) \(\newcommand{\Vtx}{v}\) \(\newcommand{\VtxA}{v_1}\) \(\newcommand{\VtxB}{v_2}\) \(\newcommand{\VertsA}{V_\textup{A}}\) \(\newcommand{\VertsB}{V_\textup{B}}\) \(\newcommand{\Edges}{E}\) \(\newcommand{\Edge}{e}\) \(\newcommand{\NofV}{\Card{V}}\) \(\newcommand{\NofE}{\Card{E}}\) \(\newcommand{\Graph}{G}\) \(\) \(\newcommand{\SCC}{C}\) \(\newcommand{\GraphSCC}{G^\text{SCC}}\) \(\newcommand{\VertsSCC}{V^\text{SCC}}\) \(\newcommand{\EdgesSCC}{E^\text{SCC}}\) \(\) \(\newcommand{\GraphT}{G^\text{T}}\) \(%\newcommand{\VertsT}{V^\textup{T}}\) \(\newcommand{\EdgesT}{E^\text{T}}\) \(\) \(%\) \(% NP-completeness etc\) \(%\) \(\newcommand{\Poly}{\textbf{P}}\) \(\newcommand{\NP}{\textbf{NP}}\) \(\newcommand{\PSPACE}{\textbf{PSPACE}}\) \(\newcommand{\EXPTIME}{\textbf{EXPTIME}}\)

Red-Black Trees

Red-black trees are another form of balanced binary search trees. In them, only one “color” bit of additional memory is required for each node. They are used in many libraries to implement ordered sets and maps. For instance:

  • ​ In Scala, both the mutable TreeMap as well as the immutable TreeMap classes provide ordered maps implemented with red-black trees.

  • ​ In Java, java.util.TreeMap implements mutable ordered maps with red-black trees. Similarly, java.util.TreeSet uses TreeMap to implement ordered sets.

  • ​ In the C++ standard library implementations, map and set classes are usually implemented with red-black trees.

See, for instance, this external visualization tool for red-black trees to see how red-black trees work.

Definition: Red-black trees

A binary search tree is a red-black tree if

  1. each node in it is colored either red or black,

  2. the root node is black,

  3. if a node is red, the both its children are black, and

  4. for each node, all simple paths from the node to the descendant leaves contain the same number of black nodes.

Example

  • ​ The tree below is a red-black tree.

    _images/rb-ex-1.png
  • ​ The BST below is not a red-black tree as it violates the requirement 3.

    _images/rb-ex-2.png
  • ​ The BST below is not a red-black tree as it violates the requirement 4.

    _images/rb-ex-3.png

The red nodes allow a certain degree of inbalance but not too much as a path cannot have two successive red nodes.

Lemma: 13.1 in Cormen et al

A red-black tree with \( n \) nodes has height at most \( 2 \Lg(n+1) \).

As red-black trees are BSTs, searching and listing the keys in a tree is done as with non-balanced basic BSTs. Thus searching for keys and finding predecessor and successor keys in a red-black tree are logarithmic time operations.

As with AVL Trees, we only have to show how we can insert and remove keys in red-black trees. Again, the basic insertion and removal is as with basic BSTs but we then have to “rebalance” the tree to make it red-black again.

Inserting keys

The first phase in inserting a key in a red-black tree works as in basic BSTs. The new leaf node is initially colored red, and then:

  1. If its parent is black, the new tree is still a red-black tree and we are done.

  2. If the parent is red, the third condition of red-black trees is violated and we have to “rebalance”.

To consider the latter case above, we now

  • ​ assume that a node \( z \) has just been made red and its parent \( p \) is also red, and

  • ​ then show how to rebalance the tree upwards until it is a valid red-black tree.

For simplicity, we only present rebalancing for the case when \( z \) is in the left sub-tree of its grandparent; the right sub-tree case is symmetric.

Rebalancing, case I

In this case also the sibling \( u \) of \( p \) (the “uncle” of \( z \)) is red. The sub-trees rooted at the nodes \( c_1 \), \( c_2 \), \( b \), \( u_1 \) and \( u_2 \) are valid red-black trees in which all the paths from the sub-tree roots to leaves contain \( k \) black nodes.

_images/rb-balance1.png

To rebalance in this case, color \( g \) red and \( p,u \) black. The sub-tree rooted at \( g \)

  • ​ is now an otherwise valid red-black tree except that its root is red, and

  • ​ all the paths from \( g \) to leaves contain \( k+1 \) black nodes as was the case before the recoloring.

Now, if \( g \) is the root of the whole tree, color it black and stop. Otherwise, continue rebalancing upwards if the parent of \( g \) is red.

Rebalancing, case II

In this case, the sibling \( u \) of \( p \) (the “uncle” of \( z \)) is black, and \( z \) is the left child of \( p \). The subtrees rooted at the nodes \( c_1 \), \( c_2 \), \( b \), and \( u \) are valid red-black trees in which all paths from the sub-tree root to leaves contain the same number of black nodes.

_images/rb-balance2.png

To rebalance, rotate the grandparent \( g \) right and color \( p \) black and \( g \) red. The new sub-tree rooted at \( p \)

  • ​ is now a valid red-black tree, and

  • ​ all the paths from \( p \) to leaves contain the same equal number of black nodes as from \( g \) before the rotation and recoloring.

We are now done with rebalancing.

Rebalancing, case III

In this case, te sibling \( u \) of \( p \) (the “uncle” of \( z \)) is black, and \( z \) is the right child of \( p \). The subtrees rooted at the nodes \( c_1 \), \( c_2 \), \( b \), and \( u \) are valid red-black trees in which all paths from the sub-tree root to leaves contain the same number \( k \) of black nodes.

_images/rb-balance3.png

To rebalance,

  • ​ first rotate the parent \( p \) left, and

  • ​ then rotate the grandparent \( g \) right and color \( z \) black and \( g \) red.

The new sub-tree rooted at \( z \)

  • ​ is now a valid red-black tree, and

  • ​ all the paths from \( z \) to leaves contain \( k+1 \) black nodes, as was the case from \( g \) before the rotations and recoloring.

We are done with rebalancing.

Removing Keys from Red-Black Trees

Again, as with basic BSTs and AVL trees, removing keys is a bit more complicated. The basic process is as with basic BSTs. If the removed node was red, there is no need for rebalancing. But when we remove a black node, the “black height” of a sub-tree rooted in the substituting node of the tree is decreased by one and we must rebalance. The details on how to do this can be found in Section 13.4 of Introduction to Algorithms, 3rd ed. (online via Aalto lib), they are not included in the course.