aboutsummaryrefslogtreecommitdiff
path: root/assignment7.tex
blob: 6e158c641801de1ec3b6678e7eef00e0da16d161 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
\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}