\documentclass[10pt,a4paper]{article}

\usepackage[margin=2cm,top=1cm]{geometry}
\usepackage{multicol}

\let\assignment7

\usepackage{enumitem}
\setenumerate[1]{label=\assignment.\arabic*.}
\setenumerate[2]{label=\textit{\alph*})}

% textcomp package is not available everywhere, and we only need the Copyright symbol
% taken from http://tex.stackexchange.com/a/1677/23992
\DeclareTextCommandDefault{\textregistered}{\textcircled{\check@mathfonts\fontsize\sf@size\z@\math@fontsfalse\selectfont R}}

\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhead{}
\fancyfoot[C]{Copyright {\textcopyright} 2015 Camil Staps}
\pagestyle{fancy}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\DeclareMathAlphabet{\pazocal}{OMS}{zplm}{m}{n}

\usepackage{tikz}
\usepackage{array}
\usepackage{caption}
\usepackage[hidelinks]{hyperref}
\usepackage{url}

\parindent0pt

\title{Algoritmen en Datastructuren - assignment \assignment}
\author{Camil Staps\\\small{s4498062}}

\begin{document}

\maketitle
\thispagestyle{fancy}

\begin{multicols}{2}

\begin{enumerate}
    \item The initial tree would be:
        
        \begin{minipage}{\linewidth}
            \centering
            \vskip10pt
            \begin{tikzpicture}[every node/.style={draw,circle,minimum width=2em},
                                level 1/.style={sibling distance=9em},
                                level 2/.style={sibling distance=5em},
                                level 3/.style={sibling distance=3em},
                                font={\footnotesize}]
                \node {9}
                    child { node {5}
                        child { node {4} }
                        child { node {7}
                            child[missing] { node {} }
                            child { node {8} }
                        }
                    }
                    child { node {12}
                        child { node {10}
                            child[missing] { node {} }
                            child { node {11} }
                        }
                        child { node {18} }
                    };
            \end{tikzpicture}
        \end{minipage}\\

        After removing $9$:

        \begin{minipage}{\linewidth}
            \centering
            \vskip10pt
            \begin{tikzpicture}[every node/.style={draw,circle,minimum width=2em},
                                level 1/.style={sibling distance=9em},
                                level 2/.style={sibling distance=5em},
                                level 3/.style={sibling distance=3em},
                                font={\footnotesize}]
                \node {10}
                    child { node {5}
                        child { node {4} }
                        child { node {7}
                            child[missing] { node {} }
                            child { node {8} }
                        }
                    }
                    child { node {12}
                        child { node {11} }
                        child { node {18} }
                    };
            \end{tikzpicture}
        \end{minipage}\\

        After removing $5$:

        \begin{minipage}{\linewidth}
            \centering
            \vskip10pt
            \begin{tikzpicture}[every node/.style={draw,circle,minimum width=2em},
                                level 1/.style={sibling distance=9em},
                                level 2/.style={sibling distance=5em},
                                font={\footnotesize}]
                \node {10}
                    child { node {7}
                        child { node {4} }
                        child { node {8} }
                    }
                    child { node {12}
                        child { node {11} }
                        child { node {18} }
                    };
            \end{tikzpicture}
        \end{minipage}

    \item \begin{enumerate}
            \item For a node $v$:

                \begin{itemize}
                    \item If $v$ is nil, return $-1$.
                    \item If $v$ is a leaf node, return $0$.
                    \item If $v$ has children, return the maximum of their heights.
                \end{itemize}
            \item For a node $v$:

                \begin{itemize}
                    \item If $v$ is nil, return $0$.
                    \item If $v$ is a leaf node, return $1$.
                    \item If $v$ has children, return the sum of their number of leaves.
                \end{itemize}
        \end{enumerate}

    \item We could give every node an extra field which holds the number of children of that node. It is not easy to see that insertion and deletion still take $\pazocal O(h)$.
        
        Then, to find node $x$ we start at the root of the tree, with children $l$ and $r$. If that node has less than $k+1$ children, we return nil, because there is no such $x$. If $l$ has less than $k+1$ children, say $c$, we recursively execute the same algorithm on $r$ with $k=k-c$. Otherwise, we execute the same algorithm on $l$ with the same $k$.

        The rationale is that the number of children of the left node tells us if going left will give a solution, and the number of children of the right node tells us if going right will give a solution. If neither gives a solution, there are not enough nodes in the tree. Since every level is visited once, this runs in $\pazocal O(h)$.

    \item We find the minimum in the tree and store it in the list. Then, find the successor in the tree of the last node you added to the list and add it to the right of that node in the list. Repeat this until you reach the maximum in the tree, which doesn't have a successor. Then link the two ends of the list by referencing them to each other.

    \item For every element $e$ in $S$ with length $k$:

        \begin{itemize}
            \item Take the root of the tree.
            \item For every $i \le 0 \le k$:

                \begin{itemize}
                    \item If there is no child $e_i$, create it
                    \item If $i=k$, make $e_i$ light grey and return
                    \item If $i\ne k$, go down to $e_i$ and repeat for $i:=i+1$
                \end{itemize}
        \end{itemize}

        This takes $\Theta(n)$, because we visit every element in $S$ only once.

        We could simultaneously build a linked list and a binary tree. Every node in the tree references a node in the list, so that we can access it in $\pazocal O(1)$ if we have the node in the tree. We build the tree as described above, in $\Theta(n)$, and in addition to that we update the list every time we update the tree. Since this takes constant time (simply pointer arithmetic), the whole procedure takes $\Theta(n)$.

\end{enumerate}

\end{multicols}

\end{document}

