\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[margin=2cm]{geometry}

\title{Howework 2 -- Programming Fundamentals 3}
\author{Claudio Maggioni}

\begin{document}
\maketitle
\tableofcontents
\section{Exercise 1}
\subsection{Question 2}
Results returned by \texttt{BinTreeSimple} are wrong since the class is not thread safe. In particular, \texttt{BinTreeSimple.insert(int)} has race conditions on the integer field occ of each node, and the fields left and right on each node. To fix this, an AtomicInteger could be used for occ and access on n.left and n.right could be synchronized.

\subsection{Question 3: Note on implementation}
I have implemented the class \texttt{BinTreeFullSync}
as a thread-safe alternative of \texttt{BinTreeSimple} where the only code modification between the two were the addition of \texttt{synchronized} keywords on already existing threads. Since I was not sure how to interpret this specific question, I have implemented \texttt{BinTreeFullSyncEdited}, a thread-safe refactoring of \texttt{BinTreeSimple} where the only synchronization mechanism is method-level \texttt{synchronized} blocks, which are used on new methods created in the subclass \texttt{Node}.

\subsection{Question 5}
\texttt{BinTreeFullSync} has the worst performance of all 3 implementations since it allows only sequential access to the tree structure. \texttt{BinTreeFullSyncEdited} is slightly faster, but \texttt{BinTreeCAS} is the fastest since the first one is a \texttt{synchronized} block imitation of the second one, which is instead based on \texttt{Atomic*} objects and can use the extra speed provided by architecture-based optimization.

\end{document}   
