Skip to content

Commit

Permalink
[RBT] Editorial work in imperative insertion part
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Liu committed Nov 13, 2014
1 parent e4c7e06 commit a53171c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions datastruct/tree/red-black-tree/rbtree-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -969,23 +969,23 @@ \section{Imperative red-black tree algorithm $\star$}
\Function{Insert}{$T, k$}
\State $root \gets T$
\State $x \gets$ \Call{Create-Leaf}{$k$}
\State \Call{Color}{$x$} $\gets RED$
\State $parent \gets NIL$
\While{$T \neq NIL$}
\State $parent \gets T$
\State \Call{Color}{$x$} $\gets$ RED
\State $p \gets$ NIL
\While{$T \neq$ NIL}
\State $p \gets T$
\If{$k <$ \Call{Key}{$T$}}
\State $T \gets $ \Call{Left}{$T$}
\Else
\State $T \gets $ \Call{Right}{$T$}
\EndIf
\EndWhile
\State \Call{Parent}{$x$} $\gets parent$
\If{$parent = NIL$} \Comment{tree $T$ is empty}
\State \Call{Parent}{$x$} $\gets p$
\If{$p =$ NIL} \Comment{tree $T$ is empty}
\State \Return $x$
\ElsIf{$k <$ \Call{Key}{$parent$}}
\State \Call{Left}{$parent$} $\gets x$
\ElsIf{$k <$ \Call{Key}{$p$}}
\State \Call{Left}{$p$} $\gets x$
\Else
\State \Call{Right}{$parent$} $\gets x$
\State \Call{Right}{$p$} $\gets x$
\EndIf
\State \Return \Call{Insert-Fix}{$root, x$}
\EndFunction
Expand Down Expand Up @@ -1027,13 +1027,13 @@ \section{Imperative red-black tree algorithm $\star$}

\begin{algorithmic}[1]
\Function{Insert-Fix}{$T, x$}
\While{\Call{Parent}{$x$} $\neq NIL$ and \textproc{Color}(\Call{Parent}{$x$}) $= RED$}
\If{\textproc{Color}(\Call{Uncle}{$x$}) $= RED$}
\While{\Call{Parent}{$x$} $\neq$ NIL $\land$ \textproc{Color}(\Call{Parent}{$x$}) = RED}
\If{\textproc{Color}(\Call{Uncle}{$x$}) $=$ RED}
\Comment{Case 1, x's uncle is red}
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets BLACK$
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets RED$
\State \textproc{Color}(\Call{Uncle}{$x$}) $\gets BLACK$
\State $x \gets$ \Call{Grandparent}{$x$}
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets$ BLACK
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets$ RED
\State \textproc{Color}(\Call{Uncle}{$x$}) $\gets$ BLACK
\State $x \gets$ \Call{Grand-Parent}{$x$}
\Else
\Comment{x's uncle is black}
\If{\Call{Parent}{$x$} = \textproc{Left}(\Call{Grand-Parent}{$x$})}
Expand All @@ -1043,8 +1043,8 @@ \section{Imperative red-black tree algorithm $\star$}
\State $T \gets$ \Call{Left-Rotate}{$T, x$}
\EndIf
\Comment{Case 3, x is a left child}
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets BLACK$
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets RED$
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets$ BLACK
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets$ RED
\State $T \gets$ \textproc{Right-Rotate}($T$, \Call{Grand-Parent}{$x$})
\Else
\If{ $x =$ \textproc{Left}(\Call{Parent}{$x$})}
Expand All @@ -1053,18 +1053,18 @@ \section{Imperative red-black tree algorithm $\star$}
\State $T \gets$ \Call{Right-Rotate}{$T, x$}
\EndIf
\Comment{Case 3, Symmetric}
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets BLACK$
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets RED$
\State \textproc{Color}(\Call{Parent}{$x$}) $\gets$ BLACK
\State \textproc{Color}(\Call{Grand-Parent}{$x$}) $\gets$ RED
\State $T \gets$ \textproc{Left-Rotate}($T$, \Call{Grand-Parent}{$x$})
\EndIf
\EndIf
\EndWhile
\State \Call{Color}{$T$} $\gets BLACK$
\State \Call{Color}{$T$} $\gets$ BLACK
\State \Return $T$
\EndFunction
\end{algorithmic}

This program takes $O(\lg N)$ time to insert a new key to the red-black tree.
This program takes $O(\lg n)$ time to insert a new key to the red-black tree.
Compare this pseudo code and the $balance$ function we defined in previous
section, we can see the difference. They differ not only in terms of
simplicity, but also in logic. Even if we feed the same series of keys to
Expand Down

0 comments on commit a53171c

Please sign in to comment.