Exercise 12.1-1

For the set of {\(1, 4, 5, 10, 16, 17, 21\) } of keys, draw binary search trees of heights 2, 3, 4, 5 and 6.

Height of 2:

12.1-1 Binary Search Tree Height of 2

Height of 3:

12.1-1 Binary Search Tree Height of 3

Height of 4:

12.1-1 Binary Search Tree Height of 4

Height of 5:

12.1-1 Binary Search Tree Height of 5

Height of 6:

12.1-1 Binary Search Tree Height of 6

The following blocks of LaTeX code were used to generate the above binary search trees:

Height of 2:

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {10}
  child {node {4} 
        child {node {1}}
        child {node {5}}
        }
  child {node {17}
        child {node {16}}
        child {node {21}}
        };
\end{tikzpicture}
\end{document}

Height of 3:

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {10}
  child {node {5} 
        child {node {4}
              child {node {1}}
              child {edge from parent[draw = none]}
              }
        child {edge from parent[draw = none]}
        }
  child {node {17}
         child {node {16}}
         child {node {21}}
        };
\end{tikzpicture}
\end{document}

Height of 4:

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {5}
  child {node {4} 
        child {node {1}}
        child {edge from parent[draw = none]}
        }
  child {node {10}
        child {edge from parent[draw = none]}
        child {node {16}
              child {edge from parent[draw = none]}
              child {node {17}
                    child {edge from parent[draw = none]}
                    child {node {21}}
                    }
              }
        };
\end{tikzpicture}
\end{document}

Height of 5:

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {17}
  child {node {16} 
        child {node {10}
              child {node {5}
                    child {node {4}
                          child {node {1}}
                          child {edge from parent[draw = none]}
                          }
                    child {edge from parent[draw = none]}
                    }
              child {edge from parent[draw = none]}
              }
        child {edge from parent[draw = none]}
        }
  child {node {21}};
\end{tikzpicture}
\end{document}

Height of 6:

\documentclass[border = 5pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}[
  every node/.style = {minimum width = 2em, draw, circle},
  level/.style = {sibling distance = 30mm/#1}
  ]
  \node {1}
  child {edge from parent[draw = none]}
  child {node {4}
        child {edge from parent[draw = none]}
        child {node {5}
              child {edge from parent[draw = none]}
              child {node {10}
                    child {edge from parent[draw = none]}
                    child {node {16}
                          child {edge from parent[draw = none]}
                          child {node {17}
                                child {edge from parent[draw = none]}
                                child {node {21}}
                                }
                          }
                    }
              }
        };
\end{tikzpicture}
\end{document}