From 4c901218a8ac51a9282148a304eef0195efb5f8f Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 14 Oct 2015 08:35:18 +0200 Subject: Finish assignment 7 --- assignment7.tex | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'assignment7.tex') diff --git a/assignment7.tex b/assignment7.tex index b988aa6..6e158c6 100644 --- a/assignment7.tex +++ b/assignment7.tex @@ -1,6 +1,6 @@ \documentclass[10pt,a4paper]{article} -\usepackage[margin=2cm]{geometry} +\usepackage[margin=2cm,top=1cm]{geometry} \usepackage{multicol} \let\assignment7 @@ -119,23 +119,46 @@ \end{minipage} \item \begin{enumerate} - \item %todo + \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 the number of leaves of the children. + \item If $v$ has children, return the sum of their number of leaves. \end{itemize} - - Note that this works on any tree. \end{enumerate} - \item %todo + \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} - \item %todo + This takes $\Theta(n)$, because we visit every element in $S$ only once. - \item %todo + 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} -- cgit v1.2.3