In the style of Figure 13.1(a), draw the complete binary search tree of height 4 on the keys \(\{1, 2, ...., 15\}\). Add the NIL leaves and color the nodes in three different ways such that the black-heights of the resulting red-black trees are 2, 3, and 4.
Black-height = 2:
Black-height = 3:
Black-height = 4:
LaTeX code for black-height = 2:
\documentclass { article}
\usepackage { tikz}
\usetikzlibrary { arrows}
\tikzset {
treenode/.style = { align=center, inner sep=0pt, text centered,
font=\sffamily } ,
arn_ n/.style = { treenode, circle, white, font=\sffamily\bfseries , draw=black,
fill=black, text width=1.5em} ,
arn_ r/.style = { treenode, circle, red, draw=red,
text width=1.5em, very thick} ,
arn_ x/.style = { treenode, rectangle, draw=black,
minimum width=0.5em, minimum height=0.5em}
}
\begin{document}
\begin{tikzpicture} [level/.style={ sibling distance = 6cm/#1,
level distance = 1cm} ]
\node [arn_ n] { 8}
child{ node [arn_ r] { 4}
child{ node [arn_ n] { 2}
child{ node[attribute,xshift=2mm] [arn_ r] { 1}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 3}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 6}
child{ node[attribute,xshift=2mm] [arn_ r] { 5}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 7}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
child{ node [arn_ r] { 12}
child{ node [arn_ n] { 10}
child{ node[attribute,xshift=2mm] [arn_ r] { 9}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 11}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 14}
child{ node[attribute,xshift=2mm] [arn_ r] { 13}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 15}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
;
\end{tikzpicture}
\end{document}
LaTeX code for black-height = 3:
\documentclass { article}
\usepackage { tikz}
\usetikzlibrary { arrows}
\tikzset {
treenode/.style = { align=center, inner sep=0pt, text centered,
font=\sffamily } ,
arn_ n/.style = { treenode, circle, white, font=\sffamily\bfseries , draw=black,
fill=black, text width=1.5em} ,
arn_ r/.style = { treenode, circle, red, draw=red,
text width=1.5em, very thick} ,
arn_ x/.style = { treenode, rectangle, draw=black,
minimum width=0.5em, minimum height=0.5em}
}
\begin{document}
\begin{tikzpicture} [level/.style={ sibling distance = 6cm/#1,
level distance = 1cm} ]
\node [arn_ n] { 8}
child{ node [arn_ n] { 4}
child{ node [arn_ n] { 2}
child{ node[attribute,xshift=2mm] [arn_ r] { 1}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 3}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 6}
child{ node[attribute,xshift=2mm] [arn_ r] { 5}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 7}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
child{ node [arn_ n] { 12}
child{ node [arn_ n] { 10}
child{ node[attribute,xshift=2mm] [arn_ r] { 9}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 11}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 14}
child{ node[attribute,xshift=2mm] [arn_ r] { 13}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ r] { 15}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
;
\end{tikzpicture}
\end{document}
LaTeX code for black-height = 4:
\documentclass { article}
\usepackage { tikz}
\usetikzlibrary { arrows}
\tikzset {
treenode/.style = { align=center, inner sep=0pt, text centered,
font=\sffamily } ,
arn_ n/.style = { treenode, circle, white, font=\sffamily\bfseries , draw=black,
fill=black, text width=1.5em} ,
arn_ r/.style = { treenode, circle, red, draw=red,
text width=1.5em, very thick} ,
arn_ x/.style = { treenode, rectangle, draw=black,
minimum width=0.5em, minimum height=0.5em}
}
\begin{document}
\begin{tikzpicture} [level/.style={ sibling distance = 6cm/#1,
level distance = 1cm} ]
\node [arn_ n] { 8}
child{ node [arn_ n] { 4}
child{ node [arn_ n] { 2}
child{ node[attribute,xshift=2mm] [arn_ n] { 1}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ n] { 3}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 6}
child{ node[attribute,xshift=2mm] [arn_ n] { 5}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ n] { 7}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
child{ node [arn_ n] { 12}
child{ node [arn_ n] { 10}
child{ node[attribute,xshift=2mm] [arn_ n] { 9}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ n] { 11}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
child{ node [arn_ n] { 14}
child{ node[attribute,xshift=2mm] [arn_ n] { 13}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
child{ node[attribute,xshift=-2mm] [arn_ n] { 15}
child{ node[attribute,xshift=4mm] [arn_ n] { nil}}
child{ node[attribute,xshift=-4mm] [arn_ n] { nil}}}
}
}
;
\end{tikzpicture}
\end{document}