\section{Orthogonal functions and orthogonal matrices}
\label{sec:orthogonal-matrices}

\begin{outcome}
  \begin{enumerate}
  \item Determine whether a linear transformation is an isometry
    and/or orthogonal.
  \item Determine whether a matrix is orthogonal.
  \end{enumerate}
\end{outcome}

\begin{definition}{Isometries and orthogonal maps}{isometry-orthogonal-map}
  Let $V,W$ be real inner product spaces, and let $T:V\to W$ be a
  linear transformation. We say that $T$ is an \textbf{isometry}%
  \index{isometry}%
  \index{linear transformation!isometry} if for all
  $\vect{u},\vect{v}\in V$,
  \begin{equation*}
    \iprod{T(\vect{u}),T(\vect{v})} = \iprod{\vect{u},\vect{v}}.
  \end{equation*}
  Moreover, $T$ is called an \textbf{orthogonal transformation}%
  \index{orthogonal transformation}%
  \index{linear transformation!orthogonal}, or simply
  \textbf{orthogonal}, if it is an isometry and invertible.
\end{definition}

In particular, if $T$ is an isometry, we have
$\norm{T\vect{v}} = \sqrt{\iprod{T\vect{v},T\vect{v}}} =
\sqrt{\iprod{\vect{v},\vect{v}}} = \norm{\vect{v}}$, so isometries
preserve the norm. (This is fact where the name ``isometry'' comes
from: from Greek ``isos'', meaning ``equal'', and ``metron'', meaning
``measure''). Conversely, any norm-preserving linear function is an
isometry, as the following proposition shows.

\begin{proposition}{Norm-preserving linear maps are isometries}{norm-isometry}
  Let $T:V\to W$ be a linear function such that for all
  $\vect{v}\in V$, $\norm{T\vect{v}} = \norm{\vect{v}}$. Then $T$ is
  an isometry.
\end{proposition}

\begin{proof}
  We first note that for all $\vect{u},\vect{v}\in V$,
  \begin{equation*}
    \iprod{\vect{u},\vect{v}}
    ~=~ \frac{1}{2}(\iprod{\vect{u},\vect{v}} + \iprod{\vect{v},\vect{u}})
    ~=~ \frac{1}{2}(\iprod{\vect{u}+\vect{v},\vect{u}+\vect{v}}
    - \iprod{\vect{u},\vect{u}} - \iprod{\vect{v},\vect{v}})
    ~=~ \frac{1}{2}(\norm{\vect{u}+\vect{v}}^2
    - \norm{\vect{u}}^2 - \norm{\vect{v}}^2).
  \end{equation*}
  Now suppose $T$ is norm-preserving and linear. Then for all
  $\vect{u},\vect{v}\in V$,
  \begin{eqnarray*}
    \iprod{T(\vect{u}),T(\vect{v})}
    &=& \frac{1}{2}(\norm{T(\vect{u})+T(\vect{v})}^2 - \norm{T(\vect{u})}^2 -
        \norm{T(\vect{v})}^2) \\
    &=& \frac{1}{2}(\norm{T(\vect{u}+\vect{v})}^2 - \norm{T(\vect{u})}^2 -
        \norm{T(\vect{v})}^2) \\
    &=& \frac{1}{2}(\norm{\vect{u}+\vect{v}}^2 -
        \norm{\vect{u}}^2 - \norm{\vect{v}^2}) \\
    &=& \iprod{\vect{u},\vect{v}}.
  \end{eqnarray*}
  Therefore, $T$ is an isometry.
\end{proof}

We also note that if $T$ is an isometry and $\vect{u}\orth\vect{v}$,
then $T(\vect{u})\orth T(\vect{v})$, because
$\iprod{T(\vect{u}),T(\vect{v})} = \iprod{\vect{u},\vect{v}} = 0$. So
isometries preserve right angles. In fact, isometries also preserve
arbitrary angles, due to the formula
$\cos\theta =
\frac{\iprod{\vect{u},\vect{v}}}{\norm{\vect{u}}\,\norm{\vect{v}}} =
\frac{\iprod{T(\vect{u}),T(\vect{v})}}
{\norm{T(\vect{u})}\,\norm{T(\vect{v})}}$.

\begin{example}{Orthogonal transformations on $R^n$}{orthogonal-transformation-rn}
  Let $T_1,T_2,T_3:\R^2\to\R^2$ be the linear transformations with matrices
  \begin{equation*}
    \coord{T_1} = \begin{mymatrix}{cc} 0 & -1 \\ 1 & 0 \end{mymatrix},
    \quad
    \coord{T_2} = \begin{mymatrix}{cc} 2 & 0 \\ 0 & 2 \end{mymatrix},
    \quad\mbox{and}\quad
    \coord{T_3} = \begin{mymatrix}{cc} 1 & 1 \\ 0 & 1 \end{mymatrix},
  \end{equation*}
  respectively. $T_1$ is a counterclockwise rotation by $90^{\circ}$,
  $T_2$ is a scaling by a factor of $2$, and $T_3$ is a shearing.
  Then $T_1$ is orthogonal, but $T_2$ and $T_3$ are not. Note that
  $T_1$, being a rotation, preserves lengths and angles. $T_2$
  preserves angles but not lengths. $T_3$ preserves neither lengths
  nor angles.
  \begin{center}
    \begin{tikzpicture}[scale=0.9]
      \begin{scope}
        \begin{scope}[scale=0.25]
          \draw[red,thick,fill=red!15]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
        \begin{scope}[xshift=2.25cm]
          \path (0,0) node {$\stackrel{\displaystyle T_1}{\longmapsto}$};
          \path (0,-1.75) node {orthogonal};
        \end{scope}
        \begin{scope}[xshift=4.5cm,scale=0.25]
          \draw[red,thick,fill=red!15,cm={0,1,-1,0,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick,cm={0,1,-1,0,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
      \end{scope}
      \begin{scope}[xshift=10cm]
        \begin{scope}[scale=0.25]
          \draw[red,thick,fill=red!15]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
        \begin{scope}[xshift=2.25cm]
          \path (0,0) node {$\stackrel{\displaystyle T_2}{\longmapsto}$};
          \path (0,-1.75) node {not orthogonal};
        \end{scope}
        \begin{scope}[xshift=4.5cm,scale=0.25]
          \draw[red,thick,fill=red!15,cm={2,0,0,2,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick,cm={2,0,0,2,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
      \end{scope}
      \begin{scope}[xshift=5cm,yshift=-4.5cm]
        \begin{scope}[scale=0.25]
          \draw[red,thick,fill=red!15]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
        \begin{scope}[xshift=2.25cm]
          \path (0,0) node {$\stackrel{\displaystyle T_3}{\longmapsto}$};
          \path (0,-1.75) node {not orthogonal};
        \end{scope}
        \begin{scope}[xshift=4.5cm,scale=0.25]
          \draw[red,thick,fill=red!15,cm={1,0,1,1,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[step=1cm, gray!50, very thin] (-5.8,-5.8) grid (5.8,5.8);
          \draw[red,thick,cm={1,0,1,1,(0,0)}]
          (0,0) -- (0,5) -- (3,5) -- (3,4) -- (1,4) --
          (1,3) -- (2,3) -- (2,2) -- (1,2) -- (1,0) -- cycle;
          \draw[semithick,->] (-6.5,0) -- (6.5,0);
          \draw[semithick,->] (0,-6.5) -- (0,6.5);
        \end{scope}
      \end{scope}
    \end{tikzpicture}
  \end{center}
\end{example}

\begin{proposition}{The matrix of an orthogonal transformation}{matrix-orthogonal}
  Let $V,W$ be finite-dimensional real inner product spaces and let
  $T:V\to W$ be a linear transformation. Let $B$ and $C$ be
  orthonormal bases of $V$ and $W$, respectively, and let
  $P=\coord{T}_{C,B}$ be the matrix of $T$ with respect to the bases
  $B$ and $C$. Then
  \begin{enumialphparenastyle}
    \begin{enumerate}
    \item $T$ is an isometry if and only if $P^TP = I$.
    \item $T$ is orthogonal if and only if $P^TP = I$ and $\dim V=\dim
      W$.
    \end{enumerate}
  \end{enumialphparenastyle}
\end{proposition}

\begin{proof}
  (a) Recall from Proposition~\ref{prop:inner-product-dot-product}
  that for all $\vect{u},\vect{v}\in V$, we have
  $\iprod{\vect{u},\vect{v}} = \coord{\vect{u}}_B\dotprod
  \coord{\vect{v}}_B$. Consider two vectors $\vect{u},\vect{v}\in V$
  and let $\vect{t}=\coord{u}_B$ and $\vect{s}=\coord{v}_B$. We have:
  \begin{eqnarray*}
    \iprod{T(\vect{u}),T(\vect{v})} = \iprod{\vect{u},\vect{v}}
    &\iff& \coord{T(\vect{u})}_C\dotprod \coord{T(\vect{v})}_C
           = \coord{\vect{u}}_B\dotprod \coord{\vect{v}}_B \\
    &\iff& (\coord{T}_{C,B}\coord{\vect{u}}_B)\dotprod (\coord{T}_{C,B}\coord{\vect{v}}_B)
           = \coord{\vect{u}}_B\dotprod \coord{\vect{v}}_B \\
    &\iff& (P\vect{t})\dotprod (P\vect{s})
           = \vect{t}\dotprod \vect{s} \\
    &\iff& \vect{t}^T P^T P\vect{s}
           = \vect{t}^T \vect{s} \\
    &\iff& \vect{t}^T P^T P\vect{s}
           = \vect{t}^T I \vect{s}.
  \end{eqnarray*}
  Therefore, $T$ is an isometry if and only if
  $\vect{t}^T P^T P\vect{s} = \vect{t}^T I \vect{s}$ holds for all
  $\vect{t},\vect{s}\in\R^m$. If $P^TP=I$, then this is clearly
  true. Conversely, assume
  $\vect{t}^T P^T P\vect{s} = \vect{t}^T I \vect{s}$ holds for all
  $\vect{t},\vect{s}\in\R^m$. Then by taking $\vect{t}$ and $\vect{s}$
  to be the $i\th$ and $j\th$ standard basis vector, it follows that
  the $(i,j)$-entry of $P^TP$ is equal to the $(i,j)$-entry of $I$,
  for all $i$ and $j$. Therefore, $P^TP=I$.

  (b) By definition, $T$ is orthogonal if and only if it is an
  isometry and invertible. Therefore, if $T$ is orthogonal, then
  $P^TP=I$ by part (a) and $\dim V=\dim W$ because every invertible
  matrix is square (see
  Theorem~\ref{thm:invertible-square}). Conversely, assume that
  $P^TP=I$ and $\dim V=\dim W$. Then $T$ is an isometry by part
  (a). Also, by Theorem~\ref{thm:right-square-left}, since $P$ is
  square and $P^T$ is a left inverse of $P$, the matrix $P$, and
  therefore the linear transformation $T$, is invertible. Therefore,
  $T$ is orthogonal.
\end{proof}

\begin{example}{Orthogonal transformations}{orthogonal-transformations}
  Which of the following matrices define orthogonal transformations?
  Which ones define isometries?
  \begin{equation*}
    A = \begin{mymatrix}{rr}
      0  & 1 \\
      -1 & 0 \\
    \end{mymatrix},\quad
    B = \begin{mymatrix}{rr}
      1 & 1 \\
      1 & 0 \\
    \end{mymatrix},\quad
    C = \frac{1}{\sqrt{2}}\begin{mymatrix}{rr}
      1 & 1 \\
      1 & -1 \\
    \end{mymatrix},\quad
    D = \frac{1}{3}\begin{mymatrix}{rr}
      1 & -2 \\
      2 & -1 \\
      2 &  2 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  We have
  \begin{equation*}
    A^TA = \begin{mymatrix}{rr}
      0  & 1 \\
      -1 & 0 \\
    \end{mymatrix}\begin{mymatrix}{rr}
      0  & -1 \\
      1 & 0 \\
    \end{mymatrix} = \begin{mymatrix}{rr}
      1 & 0 \\
      0 & 1 \\
    \end{mymatrix} = I,
  \end{equation*}
  and therefore $A$ defines an isometry and (since it is also a square
  matrix) an orthogonal transformation. We have
  \begin{equation*}
    B^TB = \begin{mymatrix}{rr}
      1 & 1 \\
      1 & 0 \\
    \end{mymatrix}\begin{mymatrix}{rr}
      1 & 1 \\
      1 & 0 \\
    \end{mymatrix} = \begin{mymatrix}{rr}
      2 & 1 \\
      1 & 1 \\
    \end{mymatrix} \neq I,
  \end{equation*}
  so the linear transformation defined by $B$ is neither an isometry
  nor orthogonal. A similar pair of calculations shows that $C^TC=I$
  and $D^TD=I$, so both $C$ and $D$ are isometries. Since $C$ is a
  square matrix, it is also orthogonal. $D$ is not a square matrix,
  and therefore not orthogonal.
\end{solution}

In light of Proposition~\ref{prop:matrix-orthogonal}, we say that an
$n\times n$-matrix $P$ is orthogonal if $P^TP=I$.

\begin{definition}{Orthogonal matrix}{orthogonal-matrix}
  An $n\times n$-matrix $P$ is called \textbf{orthogonal}%
  \index{orthogonal matrix}%
  \index{matrix!orthogonal} if $P^TP=I$.
\end{definition}

The following proposition gives several equivalent ways of checking
whether an $n\times n$-matrix $P$ is orthogonal.

\begin{proposition}{Conditions for orthogonal matrices}{conditions-orthogonal-matrix}
  The following are equivalent for an $n\times n$-matrix $P$:
  \begin{enumialphparenastyle}
    \begin{enumerate}
    \item $P$ is orthogonal.
    \item $P^TP=I$.
    \item $P$ is invertible and $P^{-1}=P^T$.
    \item $PP^T=I$.
    \item $P^T$ is orthogonal.
    \item The columns of $P$ form an orthonormal set of vectors.
    \item The rows of $P$ form an orthonormal set of vectors.
    \end{enumerate}
  \end{enumialphparenastyle}
\end{proposition}

\begin{proof}
  The equivalence (a) {\textiff} (b) is just the definition of
  orthogonality, bearing in mind that $P$ is assumed to be a square
  matrix. The equivalences (b) {\textiff} (c) {\textiff} (d) follow
  from Theorem~\ref{thm:right-square-left}, since a square matrix is
  invertible if and only if it is left invertible if and only if it is
  right invertible.  The equivalence (d) {\textiff} (e) is again just
  the definition of orthogonality, this time applied to $P^T$. To show
  (b) {\textiff} (f), let $\vect{a}_1,\ldots,\vect{a}_n$ be the
  columns of $P$, and let $\vect{e}_1,\ldots,\vect{e}_n$ be the
  standard basis vectors of $\R^n$. Then we have $P^TP = I$ if and
  only if for all $i,j$, the $(i,j)$-entry of $P^TP$ is equal to the
  $(i,j)$-entry of $I$.  But the $(i,j)$-entry of $P^TP$ is
  $\vect{e}_i^TP^TP\vect{e}_j = \vect{a}_i^T\vect{a}_j =
  \vect{a}_i\dotprod\vect{a}_j$. On the other hand, the $(i,j)$-entry
  of $I$ is $1$ if $i=j$ and $0$ if $i\neq j$. It follows that
  $P^TP=I$ if and only if for all $i,j$,
  \begin{equation*}
    \vect{a}_i\dotprod\vect{a}_j=\begin{cases}
      1 & \mbox{when $i=j$ and} \\
      0 & \mbox{otherwise}.
    \end{cases}
  \end{equation*}
  But this is exactly what it means for $\vect{a}_1,\ldots,\vect{a}_n$
  to form an orthonormal set.
  The proof of (d) {\textiff} (g) is completely analogous, using rows
  instead of columns.
\end{proof}

\begin{example}{Orthogonal matrices}{conditions-orthogonal-matrix}
  Determine which of the following matrices are orthogonal by checking
  whether the columns form an orthonormal set.
  \begin{equation*}
    A = \begin{mymatrix}{rrr}
      0 & -1 & 0 \\
      1 & 0 & 0 \\
      0 & 0 & 1 \\
    \end{mymatrix},
    \quad
    B = \frac{1}{3}
    \begin{mymatrix}{rrr}
      2  & -1 &  2 \\
      -1 &  2 &  2 \\
      2  &  2 & -1 \\
    \end{mymatrix},
    \quad
    C =
    \begin{mymatrix}{rrr}
      3 &  4 & 0 \\
      4 & -3 & 0 \\
      0 &  0 & 5 \\
    \end{mymatrix},
    \quad
    D = \frac{1}{3}
    \begin{mymatrix}{rrr}
      2  &  1 &  2 \\
      1  &  2 &  2 \\
      2  &  2 &  1 \\
    \end{mymatrix}.
  \end{equation*}
\end{example}

\begin{solution}
  By Proposition~\ref{prop:conditions-orthogonal-matrix}, it suffices to check
  whether the columns of each matrix are orthonormal. This is the case
  for $A$ and $B$. For example, the columns of $B$ are
  \begin{equation*}
    \vect{b}_1 = \frac{1}{3}\begin{mymatrix}{r} 2 \\ -1 \\ 2 \end{mymatrix},
    \quad
    \vect{b}_2 = \frac{1}{3}\begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix},
    \quad\mbox{and}\quad
    \vect{b}_3 = \frac{1}{3}\begin{mymatrix}{r} 2 \\ 2 \\ -1 \end{mymatrix},
  \end{equation*}
  and we have
  \begin{equation*}
    \vect{b}_1\dotprod \vect{b}_1 =
    \frac{1}{9}
    \begin{mymatrix}{r} 2 \\ -1 \\ 2 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} 2 \\ -1 \\ 2 \end{mymatrix}
    = 1,\qquad
    \vect{b}_1\dotprod \vect{b}_2 =
    \frac{1}{9}
    \begin{mymatrix}{r} 2 \\ -1 \\ 2 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
    = 0,
  \end{equation*}
  \begin{equation*}
    \vect{b}_2\dotprod \vect{b}_2 =
    \frac{1}{9}
    \begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
    = 1,\qquad
    \vect{b}_1\dotprod \vect{b}_3 =
    \frac{1}{9}
    \begin{mymatrix}{r} 2 \\ -1 \\ 2 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} 2 \\ 2 \\ -1 \end{mymatrix}
    = 0,
  \end{equation*}
  \begin{equation*}
    \vect{b}_3\dotprod \vect{b}_3 =
    \frac{1}{9}
    \begin{mymatrix}{r} 2 \\ 2 \\ -1 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} 2 \\ 2 \\ -1 \end{mymatrix}
    = 1,\qquad
    \vect{b}_2\dotprod \vect{b}_3 =
    \frac{1}{9}
    \begin{mymatrix}{r} -1 \\ 2 \\ 2 \end{mymatrix}
    \dotprod
    \begin{mymatrix}{r} 2 \\ 2 \\ -1 \end{mymatrix}
    = 0.
  \end{equation*}
  So the columns of $B$ are orthonormal.  The columns of $C$ are
  orthogonal, but not normalized, so $C$ is not an orthogonal
  matrix. The columns of $D$ are normalized, but not orthogonal, so
  $D$ is not an orthogonal matrix either.
\end{solution}

\begin{proposition}{Properties of orthogonal matrices}{properties-orthogonal}
  \begin{enumialphparenastyle}
  \begin{enumerate}
  \item\label{item:properties-orthogonal-a}
    If $P,Q$ are orthogonal $n\times n$-matrices, then $PQ$ is
    orthogonal.
  \item The identity matrix is orthogonal.
  \item If $P$ is orthogonal, then so is $P^{-1}$.
  \item\label{item:properties-orthogonal-d}
    If $P$ is an orthogonal $n\times n$-matrix and $Q$ is an
    orthogonal $m\times m$-matrix, then the $(n+m)\times(n+m)$-matrix
    \begin{equation*}
      \begin{mymatrix}{c|c}
        P & 0 \\\hline
        0 & Q
      \end{mymatrix}
    \end{equation*}
    is orthogonal.
    \end{enumerate}
  \end{enumialphparenastyle}
\end{proposition}


%!TEX root = ../Main.tex
\begin{wrapfigure}[13]{r}{.45\textwidth}
	\vspace{-2em}
	\includegraphics[width=.45\textwidth]{Figures/pointplot.eps}
	\caption{Figure showing the simple system. The atoms 4-11 is the device itself while the black atoms are the semi-infinite chain of atoms around the device.}\label{inlinepointplot}
\end{wrapfigure}
In the following sections, the focus will be to present and explain how to calculate band structures, local density of states and transmission through Python-programming, using the tight-binding approximation for any periodic structure. For simplicity, all initial examples and calculations will be done on a simple system, to make sure the different steps are easy to follow. The system can be seen in \cref{inlinepointplot}. Note how the atoms have indices according to their position. The device consist of a left (red) contact, a right (blue) contact and the atoms in between (yellow). Around the device one finds the semi-infinite chain of atoms.
\subsection{Creating the on-site Hamiltonian and hopping matrices}
The first and most essential parts needed for calculations is the \textit{on-site} Hamiltonian \(\mathbf{h_0}\) and the \textit{hopping} matrices \(\mathbf{V},\ \mathbf{V}^{\dagger}\). The starting point is a matrix, containing a set of coordinates \(x_0,y_0,z_0\), representing atom positions and a set of unit vectors \(\mathbf{u}_x, \ \mathbf{u}_y, \ \mathbf{u}_z\). The unit vectors will be the basis of the unit cell containing all atom coordinates. From now on, only the x and y coordinates will be considered as we will focus only on planar 2D devices.
The on-site Hamiltonian represents the interaction of atoms within the unit cell and the hopping matrices represents the interaction of atoms between periodically repeated unit cells.
\begin{figure}[ht]
	\centering
	\includegraphics[width=.7\textwidth]{Figures/atomreffig.eps}
	\caption{Visual representation of the periodic simple system. The atoms surrounded by the black box in the centre represents the unit cell. The neighbouring boxes are unit cells repeated periodically once in every direction. Blue connections are on-site hops. Yellow are off-site hops.}
	\label{atomrepfig}
\end{figure}
In \cref{atomrepfig} a visual representation of the simple systems with periodically repeated unit cells can be seen. For the rest of the report, the on-site Hamiltonian will allways be the centre cell while the hopping matrices will be the cells surrounding the on-site Hamiltonian (see \cref{repfig} for a generalised visual representation of the concept).\newline
In order to find the hops within the cell, we start off with a set of coordinates for the unit cell atoms and their lattice unit vectors. In order to get the on-site hop matrix we use what is called the vector outer product. This means that every coordinate set (\textit{x,y}) is compared to every other coordinate set. In our case, we want to find the nearest neighbours. Every two atoms within a distance of \SI{1.6}{\angstrom} is considered nearest neighbours. A function called \textit{Onsite} have been developed to do just this. In \cref{npouter} the function can be seen.
\im{Listings/Functions.py}{33}{38}
\vspace{-1\baselineskip}
\captionof{listing}{The outer operator in NumPy is manifested as two nested loops. On lines 34-36 each atomic distance is calculated. Line 37 replaces all nearest neighbour distances with an input potential, leaving the rest as zero. Lastly the diagonal is subtracted from the matrix on line 38.\label{npouter}}\vspace{\baselineskip}
The function produces a matrix which contain all distances between all atoms in the unit cell. The tight-binding model dictates that only atoms with a specific inter-atomic distance interact. Therefore the function has implemented a threshold (\cref{npouter} line 37) to determine whether a given distance is too great for interaction or small enough for interaction. All distances above the threshold will be changed to a 0-element in the on-site Hamiltonian matrix, representing zero interaction and all distances below the threshold will be changed to 1 to represent interaction between atoms. Finally The on-site Hamiltonian is multiplied with a on-site potential (scalar). The on-site potential \(V_{pp\pi}\) differs depending on the system. Lastly the diagonal is subtracted as these elements represent the electrons average energy on each site (the atoms does not interact with themselves). Remember that \(\epsilon_0 = 0\). Now the on-site Hamiltonian is complete and the product is a matrix containing 0's and 1's to represent interaction between atoms in a unit cell. \newline
Moving on to the hopping matrices one first has to realise that the interactions are happening in a 2D plane. This has to be kept in mind when describing interaction between unit cells repeated in all directions in the plane. Effectively this means that six hopping matrices should be created. One in the x-direction, one in the y-direction, one in the xy-direction and their hermitian conjugates. Graphically this corresponds to a structure of this kind:
\begin{figure}[H]
	\centering
	\begin{subfigure}[t]{.7\textwidth}
		\centering
		\includegraphics[width=\textwidth]{Figures/repfig.eps}
		\caption{Representative figure of how the on-site Hamiltonian along with its hopping matrices are structured}
		\label{repfig}
	\end{subfigure}
	\vskip
	\begin{subfigure}[t]{.7\textwidth}
		\centering
		\includegraphics[width=\textwidth]{Figures/stitch.eps}
		\caption{Matrix maps from calculation on an arbitrary graphene system with a unit cell of 12 atoms. The on-site Hamiltonian along with all its hopping matrices are stitched together like in figure \cref{repfig}. All the dark spots represent a hopping of an electron to its nearest neighbour i.e. a 1 element and yellow represents a 0 element}
		\label{matrixmap}
	\end{subfigure}
	\caption{Schematic and practical example of onsite and hop matrices.}\label{repfigmatrixmap}
\end{figure}
In practice this is done by shifting the original \(x_0,y_0\) coordinates by the given unit vectors \(\mathbf{u}_x,\mathbf{u}_y\). By addition of the unit vectors to the original coordinate matrix one can get three new coordinate matrices \(\mathbf{xy}_{shift-x}=x_0,y_0 + \mathbf{u}_x\), \(\mathbf{xy}_{shift-y}=x_0,y_0 + \mathbf{u}_y\) and \(\mathbf{xy}_{shift-xy}=x_0,y_0 + \mathbf{u}_x+\mathbf{u}_y\). With these three matrices what follows is basically the same method used to get the on-site Hamiltonian. The only difference being that it will be distances between atoms in the on-site Hamiltonian and the shifted matrices respectively. That way it is the distance, and thus the interaction, between the on-site Hamiltonian and the repeated unit cells that is calculated. The three resulting hopping matrices are denoted \(\vb{V}_{x}\), \(\vb{V}_{y}\) and \(\vb{V}_{xy}\). They represent interaction (hopping) in the "forward" direction (left-to-right) (see \cref{repfig}). To create the hopping matrices hopping in the "backwards" (right-to-left) direction (see \cref{repfig}) one simply has to complex conjugate the hopping matrices. These matrices are denoted with a dagger: \(\vb{V}_{x}^{\dagger}\), \(\vb{V}_{y}^{\dagger}\) and \(\vb{V}_{xy}^{\dagger}\). To get an idea of how such matrices looks like, see \cref{matrixmap}, where the resulting matrix-maps from a small 12-atom graphene unit cell are shown. It has been stitched together like in \cref{repfig}.
\subsection{Defining the full Hamiltonian and solving the Schr\"{o}dinger equation}\label{FullHam}
Now that the on-site Hamiltonian along with its hopping matrices have been created, the next step is to create the full Hamiltonian in order to solve the Schr\"{o}dinger equation for the system as a whole. This is an eigen-value/vector problem. The Schr\"{o}dinger equation needs to be solved to get the eigen energies for the system as they will be used to produce band structure plots later on. In essence the full Hamiltonian denoted \(\vb{H}\) is a sum of the on-site Hamiltonian and its corresponding hopping matrices multiplied by a complex exponential function that has the appropriate phase relative to the hopping matrix. Remember that the electrons moves as waves in the periodic system. Because of the periodicity it is practical to use the reciprocal space. Here the electron can move from \textit{k}-point to \textit{k}-point, where \(k\) represents a continuous variable between 0 and \(\pi\) along the \(\dfrac{1}{x}\)- and \(\dfrac{1}{y}\)-axis (hence the term ``reciprocal space'').
This means that when moving from one  \textit{k}-point to another, one needs to multiply the appropriate parts with a Bloch phase\cite{simon2013oxford}:
\begin{align}\label{hamileq}
	\vb{H}(k_x,k_y) = \vb{h}_0 & + (\vb{V}_{x}e^{-ik_x} + \vb{V}_{x}^{\dagger}e^{ik_x}                   \nonumber  \\
	                           & + \vb{V}_{y}e^{-ik_y} + \vb{V}_{y}^{\dagger}e^{ik_y}                               \\
	                           & + \vb{V}_{xy}e^{-ik_x}e^{-ik_y} + \vb{V}_{xy}^{\dagger}e^{ik_x}e^{ik_y}) \nonumber
\end{align}
Using the full Hamiltonian, the Schr\"{o}dinger equation can be solved
\begin{align}
	\vb{H}(k_x,k_y)\vb*{\phi}_{k} = \vb*{\epsilon}_n (k_x,k_y) \vb*{\phi}_k
\end{align}
Where \(\vb*{\phi}_k\) is the electron wave functions and \(\vb*{\epsilon}(k_x,k_y)\) is the eigen-energies. \\
In practice this is done by defining a function, here called \textit{Hkay}, that takes the on-site Hamiltonian, the hopping matrices, and \(k_{x/y}\) as inputs and outputs the eigenvalues. Using NumPy's \textit{numpy.linalg.eigh} the Hamiltonian matrix is diagonalised. The number of eigenvalues in the output corresponds to the dimension of the full Hamiltonian. In \cref{fullhamil} the code for the function is shown.\newpage
\im{Listings/Functions.py}{73}{80}
\vspace{-.5\baselineskip}
\captionof{listing}{Function producing the full Hamiltonian, corresponding to \cref{hamileq} the inputs x and y corresponds to the \(k_x\),\(k_y\) .\label{fullhamil}}\vspace{\baselineskip}
\subsection{Producing band structures}
\begin{wrapfigure}[20]{r}{.30\textwidth}
	\vspace{-2em}
	\includegraphics[width=0.30\textwidth]{Figures/BetaBandstructures.eps}
	\caption{Figure showing the band structure of the simple system.}\label{bandssimple}
\end{wrapfigure}
In order to calculate and visualise the band structure of the simple system, one need to define the full Hamiltonian \(\vb{H}\) in two directions. When working with band structures and periodic systems it is common to note points in space with respect to the \textit{Brillouin zone} which is a primitive cell in reciprocal space. Therefore a continuous variable \(k\) is introduced. it extends in two directions in (\(-k_{x}\)) and (\(k_{y}\)), which correspond to lengths between the symmetry points \(X\), \(\Gamma\) and \(Y\) in the Brillouin zone. Here \(\Gamma\) is the origin \((0,0)\). Practically this corresponds to making two plots, one for each pair of symmetry points. The y-values in each plot correspond to the eigenvalues obtained by the \textit{Hkay} function described in \cref{FullHam}. The number of eigen energies, and effectively the number of bands in the plot is dictated by the dimensions of the Hamiltonian, which again is dictated by the number of unit cell atoms. \textit{n}-atoms \(\rightarrow\) \(n\times n\)-matrix \(\rightarrow\) \textit{n} eigen-energies (bands). However plots produced in this report will only show a few of these bands in a small energy range. The full Hamiltonian for obtaining the eigen energies that corresponds to directions \(X\) and \(Y\) are:
\begin{align}
	X: \ \vb{H}_{X} = \vb{h}_0 + (\vb{V}_{x}e^{ik_x} + \vb{V}_{x}^{\dagger}e^{-ik_x} + \vb{V}_{y} + \vb{V}_{y}^{\dagger} + \vb{V}_{xy}e^{ik_x} + \vb{V}_{xy}^{\dagger}e^{-ik_x}) \\
	Y: \ \vb{H}_{Y} = \vb{h}_0 + (\vb{V}_{x} + \vb{V}_{x}^{\dagger} + \vb{V}_{y}e^{-ik_y} + \vb{V}_{y}^{\dagger}e^{ik_y} + \vb{V}_{xy}e^{-ik_y} + \vb{V}_{xy}^{\dagger}e^{ik_y})
\end{align}
Using the eigenvalues as y-values in the two plots, putting the two plots together will yield a final plot of the band structure shown in \cref{bandssimple}.


\section{Deep learning approach}
Conventional ML techniques are incapable of processing large registered data in their raw form.
In conventional ML, the process of features engineering requires high expertise and skills to extract damage-sensitive features for specific SHM applications.
Accordingly, there is no guarantee that such features can be reused for other structures due to the nonlinear behaviour.

It can be said that the huge development that occurred in the computational powers (e.g. central processing units (CPU), graphical processing units (GPU), etc.), in addition to the availability of big data, and the development of new learning algorithms~\cite{Yuan2020},  allowed DL techniques to develop rapidly.
Consequently, DL-based SHM methods have been utilized to overcome issues related to ML-based SHM.
DL approach makes it possible to use registered data in their raw form without any need to perform feature engineering, hence, such an approach has an end-to-end structure that will automatically learn and discover the hidden features in high dimensional input data~\cite{LeCun, Networks}.
Figure ~\ref{fig:DL_ML} illustrates the main differences between the conventional ML-based SHM and DL-based SHM approaches.

\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=1.0\linewidth]{figures/DL_vs_ML.png}
	\end{center}
	\caption{(a) Conventional ML based SHM vs. (b) DL based SHM.}
	\label{fig:DL_ML}
\end{figure}

In current section, Convolutional neural networks (CNN) and Recurrent neural networks (RNN) will be illustrated.

\subsection{Convolutional neural networks}
ConvNet or CNN is a feed-forward artificial neural network (ANN) inspired by visual cortex in the human brain.
Mainly, CNN architectures are utilised for computer vision applications in which images are processed for classification and segmentation purposes.
The main components of any ANN are neurons or perceptrons.
Figure~\ref{fig:neuron} depicts the general neuron architecture, further, a neuron is capable of performing some sort of non-linear computation through an activation function that acts as a gate to forward the processed signal to the next layer.
\begin{figure}
	\begin{center}
		\includegraphics[scale=1]{figures/fig_neron.png}
	\end{center}
	\captionof{figure}{Neuron architechture.}
	\label{fig:neuron}
\end{figure}
An example of an activation function is Relu (that changes all negative values of the feature map to zero and keeps all positive values unchanged).
Furthermore, a neuron has several connections of weighted inputs and outputs that are updated through a learning process referred as back-propagation.
Back-propagation is a procedure in which the weights are updated in a way the model learns how to predict the desired output.
For this purpose, a cost function (objective function) is utilised to estimate the difference between the predicted output and the targeted output.
Accordingly, the back-propagation (e.g Gradient descent, Adam and RMSprop) procedure is applied to minimize the cost function.

A typical structure of a CNN is presented in Fig. ~\ref{CNN}.
It consists of three parts: convolutional layer, downsampling layer and dense layer.
\begin{figure}
	\begin{center}
		\includegraphics[width=1.0\linewidth]{figures/cnn.png}
	\end{center}
	\captionof{figure}{CNN architecture.}
	\label{CNN}
\end{figure}
Convolutional layers are used to extract features from the input image.
Further, a convolution (dot product) is carried out through sliding a window (filter or kernel) of size \((w_f,h_f,d_f)\) all over the input image of a size \((w,h,d)\)  to produce feature maps that are locally correlated.
Usually, a convolutional operation is followed by a non-linear activation function such as Relu that changes all negative values of the feature map to zero.
The next layer is the downsampling (pooling) that joins the related features into one feature for reducing the computation complexity~\cite{LeCun}.
Dense layers can be fully or partially connected followed by the output layer which produces the predicted outputs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Recurrent neural networks}
A recurrent neural network (RNN) is a DL model that handles time-series data (sequential data).
Moreover, the RNN technique can remember its data input, because of its internal memory which makes it a powerful and promising technique in the field of DL.
Since there are temporal problems such as natural language processing, language translation, image captioning and so on, they require to be handled sequentially.
In the traditional deep neural networks (feed-forward) it is assumed that there is no correlation between the inputs and the outputs, while this assumption is not true for the RNN technique, that means the output of the RNN depends on the prior input sequence.
The future events can also be used in predicting the output of a given sequence.
Figure~\ref{rnn_vs_nn} depicts the difference between RNN and feed-forward deep neural networks.
\begin{figure}
	\begin{center}
		\includegraphics[width=1.0\linewidth]{figures/rnn_vs_nn.png}
	\end{center}
	\captionof{figure}{(a) RRN vs (b) feed-forward neural network.}
	\label{rnn_vs_nn}
\end{figure}
As shown in the Fig.~\ref{rnn_vs_nn} (a), for the RNN, the output of a certain layer is looped back to its input which helps in making the prediction.
However, in the feed-forward networks, the inputs and outputs are independent, and there is no connection between them.
Figure~\ref{unrolled_rnn} shows the visualisation of an unrolled RNN, where \(x_{t}\) corresponds to the sequential timestamped input at time \(t\), \(h_{t}\) corresponds to internal state  and \(Y_{t}\) corresponds to the predicted timestamped output at time \(t\).
Additionally, the figure shows that an unrolled RNN can be seen as a cascaded sequence of feed-forward networks.
\begin{figure}
	\begin{center}
		\includegraphics[width=1.0\linewidth]{figures/unrolled_rnn.png}
	\end{center}
	\captionof{figure}{Unrolled RNN.}
	\label{unrolled_rnn}
\end{figure}
In the feed-forward neural networks, as mentioned earlier, the learnable parameters (adjustable weights) are available only for the forward path of data propagation that are updated through back-propagation algorithm.
However, for RNN, since there are two paths of data propagation (forward and backward) there are learnable weights for both directions.
In RNN technique, weights are updated using back-propagation through time (BBTT)~\cite{Werbos1990}.
Essentially, BBTT performs back-propagation algorithm on unrolled RNN and since BBTT depends on the number of timestamps this could be computationally expensive when there are a high number of timestamps.

A gradient measures the change in all weights regarding the change in error (the difference between the actual predicted output and the ground truth).
As a result, when implementing RNNs, two issues may arise during updating the learnable weights using BBTT:
\begin{enumerate}
	\item \textbf{Exploding gradients}: that can occur when the assigned values to the weights become so large that leads to overflow and results in not a number (NaN) values~\cite{Brownlee2017a}.
	\item \textbf{Vanishing gradients}: that can occur when the assigned values to the weights become too small which affects the learning process to be very slow or to stop~\cite{Brownlee2017a}.
\end{enumerate}
To solve such issues, a long short-term memory (LSTM)  was introduced~\cite{Hochreiter1997} which is an memory extension for regular RNN.
LSTM addresses the problem of long-term dependencies.

LSTM is composed of four units: an input gate, a cell state, a forget gate, and a output gate.
\begin{figure}
	\begin{center}
		\includegraphics[width=1.0\linewidth]{figures/lstm.png}
	\end{center}
	\captionof{figure}{LSTM.}
	\label{lstm}
\end{figure}
The purpose of the forget gate is to figure out which information needs to be considered and which needs to be neglected.
The current input \(x_t\) and the previous hidden state \(h_{t-1}\) are passed through a sigmoid function which will produce values between \(0\) and \(1\) then the outputs of the sigmoid are multiplied with the previous cell state.
The input gate takes the current input \(x_t\) and the previous hidden state \(h_{t-1}\) and apply a sigmoid function over them in order to transform them to values in a range between \(0\) (not important) and \(1\) (important), then the same current input and the hidden state are passed through a \(tanh\) function which will regulate the network by transferring the values into a range between \(-1\) and \(1\).
Then, the outputs from the sigmoid and \(tanh\) functions are multiplied point-by-point in order to eliminate \(0\) values.
At this point, the network has sufficient information obtained from the input and forget gates.
Therefore, the current cell state \(c_t\) can be calculated through multiplying the previous cell state \(c_{t-1}\) with the output of the forget gate (all 0 values will be dropped) and the result is added to the calculated input values.
Afterward, the output gate computes the next hidden state \(h_t\) that holds information belongs to the current inputs.
Initially, the current input \(x_t\) and the previous hidden state \(h_{t-1}\) are passed through a third sigmoid function which will produce values between
\(0\) and \(1\), and the current cell state \(c_t\) is passed though a \(tanh\) function.
Then the calculated values from the third sigmoid function and the \(tanh\) function are multiplied point-by-point.
Finally, the computed hidden state \(h_t\) is used for the prediction and it is transferred to the next timestamp.


\chapter{Catalan Trees}
\label{chap:Catalan}

In part~\ref{part:linear}, we dealt with linear structures, like
stacks and queues, but, to really understand programs operating on
such structures, we needed the concept of tree. This is why we
introduced very early on abstract syntax trees\index{tree!abstract
  syntax $\sim$}, directed acyclic graphs (e.g.,
\fig~\vref{fig:cat_dag}), comparison trees (e.g.,
\fig~\vref{fig:cmp_tree}), binary trees (e.g.,
\fig~\vref{fig:comp_tree}), proof trees (e.g.,
\fig~\vref{fig:perm_proof}), evaluation trees (e.g.,
\fig~\vref{fig:2way_unbal}) and merge trees (e.g.,
\fig~\vref{fig:bot_up1}). Those trees were meta\hyp{}objects, or
concepts, used to understand the linear structure at hand.

In this chapter, we take a more abstract point of view and we consider
the general class of \emph{Catalan trees}, or general trees. We will
study them as mathematical objects with the aim to transfer our
results to the trees used as data structures to implement
algorithms. In particular, we will be interested in measuring,
counting them, and determining some average parameters relative to
their shape, the reason being that knowing what a random tree looks
like will tell us something about the cost of traversing it in
different ways.

% Wrapping figure better declared before a paragraph
%
\begin{wrapfigure}[9]{r}[0pt]{0pt}
% [9] vertical lines
% {r} mandatory right placement
% [0pt] of margin overhang
\centering
\includegraphics[bb=71 646 160 712]{catalan_tree}
\caption{Catalan tree of height~4\label{fig:catalan_tree}}
\end{wrapfigure}
Catalan trees are a special kind of graph, that is, an object made of
\emph{nodes} (also called \emph{vertices}) connected by \emph{edges},
without orientation (only the connection matters). What makes Catalan
trees is the distinction of a node, called the \emph{root}, and the
absence of cycles, that is, closed paths made of nodes successively
connected. Catalan trees are often called \emph{ordered trees}, or
\emph{planted plane trees}, in graph theory, and \emph{unranked
  trees}, or \emph{\(n\)-ary trees} in programming theory. An example
is given in \fig~\ref{fig:catalan_tree}. Note how the root is the
topmost node and has four subtrees, whose root are called
\emph{children}, given in order. The nodes drawn as white disks
\((\circ)\) make up a maximal path starting from the root (the number
of nodes along it is maximal). The ending node has no children; there
are actually~\(8\) such nodes in total, called the \emph{leaves}. The
number of edges connecting white disks is the \emph{height} of the
Catalan tree (there may be several maximal paths of same length), so
the given example has height~\(4\).

Programmers implement Catalan trees as a data structure, \emph{e.g.},
using \textsf{XML}, in which case some information is stored in the
nodes and its retrieval may --~in the worst case~-- require the
reaching of a leaf. The maximum cost of a search is thus proportional
to the height of the tree and the determination of the average height
becomes relevant when performing a series of random searches
\citep{VitterFlajolet_1990}. For this kind of analysis, we need first
to find the number of Catalan trees with a given size. There are two
common measures for the size: either we quantify the trees by their
number of nodes or we count the edges. In fact, using one or the other
is a matter of convenience or style: there are \(n\)~edges if there
are \(n+1\) nodes, simply because each node, save the root, has one
parent. It is often the case that formulas about Catalan trees are a
bit simpler when using the number of edges, so this will be our
measure of size in this chapter.

\section{Enumeration}
\label{sec:Catalan_enumeration}

% Wrapping figure better declared before a paragraph
%
\begin{wrapfigure}[10]{r}[0pt]{0pt}
% [10] vertical lines
% {r} mandatory right placement
% [0pt] of margin overhang
\centering
\subfloat[Dyck path\label{fig:dyck}]{%
  \includegraphics[bb=71 634 158 712]{dyck}}
\quad
\subfloat[Catalan tree\label{fig:ctree}]{%
\includegraphics[bb=65 663 127 721]{ctree}
}
\caption{Bijection\label{fig:bijection}}
\end{wrapfigure}
In most textbooks \cite[\S~5.1 \&~5.2]{SedgewickFlajolet_1996} is
shown how to find the number of Catalan trees with \(n\)~edges by
means of some quite powerful mathematical tools known as
\emph{generating functions}\index{enumerative combinatorics!generating
  function} \cite[chap.~7]{GrahamKnuthPatashnik_1994}. Instead, for
didactical purposes, we opt for a more intuitive technique in
enumerative combinatorics\index{enumerative combinatorics} which
consists in constructing a one\hyp{}to\hyp{}one correspondence between
two finite sets, so the cardinal of one set is the cardinal of the
other. In other words, we are going to relate bijectively, on the one
hand, Catalan trees, and, on the other hand, other combinatorial
objects which are relatively easy to count.

The objects most suitable for our purpose are \emph{monotonic lattice
  paths}\index{monotonic lattice path|see{Dyck path}} in a square grid
\citep{Mohanty_1979,Humphreys_2010}. These paths are made up of steps
oriented upwards (\(\uparrow\)), called \emph{rises}, and steps
rightwards (\(\rightarrow\)), called \emph{falls}, starting at the
bottom left corner \((0,0)\). \emph{Dyck paths}\index{Dyck path} of
length~\(2n\) are paths ending at \((n,n)\) which stay above the
diagonal, or touch it. We want to show that \emph{there exists a
  bijection between Dyck paths of length~\(2n\) and Catalan trees with
  \(n\)~edges.}

\begin{wrapfigure}[7]{r}[0pt]{0pt}
\centering
\includegraphics[bb=71 661 128 720]{ctree_pre}
\caption{\label{fig:ctree_pre}}
\end{wrapfigure}
To understand that bijection, we need first to present a particular
kind of \emph{traversal}\index{tree!traversal}, or
\emph{walk}\index{tree!walk|see{traversal}}, of Catalan trees. Let us
imagine that a tree is a map where nodes represent towns and edges
roads. A complete traversal of the tree consists then in starting our
trip at the root and, following edges, to visit all the nodes. (It is
allowed to visit several times the same nodes, since there are no
cycles.) Of course, there are many ways to achieve this tour and the
one we envisage here is called a \emph{preorder traversal}. At every
node, we take the leftmost unvisited edge and visit the subtree in
preorder; when back at the node, we repeat the choice with the
remaining unvisited children. For the sake of clarity, we show in
\fig~\ref{fig:ctree_pre} the \emph{preorder numbering}\index{tree!Catalan $\sim$!preorder numbering} of \fig~\ref{fig:ctree}, where the order in
which a node is visited first is shown instead of a black disk
\((\bullet)\).

\begin{wrapfigure}[12]{r}[0pt]{0pt}
% [11] vertical lines
% {r} mandatory right placement
% [0pt] of margin overhang
\centering
\includegraphics[bb=71 591 186 721]{reflection}
\caption{Reflection of a prefix w.r.t. \(y = x - 1\)
\label{fig:reflection}}
\end{wrapfigure}
The first part of the bijection is an injection from Catalan trees
with \(n\)~edges to Dyck paths of length~\(2n\). By traversing the
tree in preorder, we associate one rise to an edge on the way down,
and a fall to the same edge on the way up. Obviously, there are
\(2n\)~steps in the Dyck path. The surjection simply consists in
reversing the process by reading the Dyck path step by step,
rightwards, and build the corresponding tree. Now, we need to count
the number of Dyck paths of length~\(2n\), which we know now is also
the number of Catalan trees with \(n\)~edges.

The total number of monotonic paths of length~\(2n\) is the number of
choices of \(n\)~rises amongst \(2n\)~steps, that is,
\(\binom{2n}{n}\). We need now to subtract the number of paths that
start with a rise and cross the diagonal. Such a path is shown in
\fig~\ref{fig:reflection}, drawn as a bold continuous line. The first
point reached below the diagonal is used to plot a dotted line
parallel to the diagonal. All the steps from that point back to
\((0,0)\) are then changed into their counterpart: a rise by a fall
and vice-versa. The resulting segment is drawn as a dashed line. This
operation is called a \emph{reflection} \citep{Renault_2008}. The crux
of the matter is that we can reflect each monotonic path crossing the
diagonal into a distinct path from \((1,-1)\) to
\((n,n)\). Furthermore, all reflected paths can be reflected when they
reach the dotted line back into their original counterpart. In other
words, the reflection is bijective. (Another intuitive and visual
approach to the same result has been published by \cite{Callan_1995}.)
Consequently, there are as many monotonic paths from \((0,0)\) to
\((n,n)\) that cross the diagonal as there are monotonic paths from
\((1,-1)\) to \((n,n)\). The latter are readily enumerated:
\(\binom{2n}{n-1}\). As a conclusion, the number of Dyck paths of
length~\(2n\) is\label{eq:Ann}
\begin{align*}
C_n &= \binom{2n}{n} - \binom{2n}{n-1}
= \binom{2n}{n} - \frac{(2n)!}{(n-1)!(n+1)!}\\
&= \binom{2n}{n} - \frac{n}{n+1} \cdot \frac{(2n)!}{n!n!}
 = \binom{2n}{n} - \frac{n}{n+1} \binom{2n}{n} = \frac{1}{n+1}\binom{2n}{n}.
\end{align*}
The numbers \(C_n\) are called \emph{Catalan numbers}\index{Catalan
  numbers@$C_n$|see{Catalan numbers}}\index{Catalan
  numbers}. Using Stirling's
formula\index{Stirling's formula}, seen in
equation~\eqref{eq:Stirling} on page~\pageref{eq:Stirling}, we find
that the number of Catalan trees with \(n\)~edges is
\begin{equation}
  C_n = \frac{1}{n+1}\binom{2n}{n} \sim \frac{4^n}{n\sqrt{\pi n}}.
\label{eq:Cn}
\end{equation}

\section{Average path length}

The \emph{path length} of a Catalan tree is the sum of the lengths of
the paths from the root. We have seen this concept in the context of
binary trees, where it was declined in two variants, \emph{internal
  path length} (page~\pageref{insertion__internal_path_length}) and
\emph{external path length}
(page~\pageref{sorting__external_path_length}), depending on the end
node being internal or external. In the case of Catalan trees, the
pertinent distinction between nodes is to be a \emph{leaf} (that is, a
node without subtrees) or not, but some authors nevertheless speak of
external path length when referring to the distances to the leaves,
and of internal path length for the non\hyp{}leaf nodes, hence we must
bear in mind whether the context is the Catalan trees or the binary
trees.

In order to study the average path length of Catalan trees, and some
related parameters, we may follow \cite{DershowitzZaks_1981} by
finding first the average number of nodes of degree~\(d\) at
level~\(l\) in a Catalan tree with \(n\)~edges. The \emph{degree of a
  node} is the number of its children and its \emph{level} is its
distance to the root counted in edges and the root is at
level~\(0\).

% Wrapping figure better declared before a paragraph
%
%%\begin{wrapfigure}[10]{r}[0pt]{0pt}
\begin{figure}
% [10] vertical lines
% {r} mandatory right placement
% [0pt] of margin overhang
\centering
\subfloat[Dyck path\label{fig:dyck_deg}]{%
  \includegraphics[bb=71 634 158 712]{dyck_deg}}
\quad
\subfloat[Catalan tree\label{fig:ctree_deg}]{%
\includegraphics[bb=65 663 127 721]{ctree_deg}
}
\caption{Degree-based bijection\label{fig:degree_based_bijection}}
\end{figure}
%%\end{wrapfigure}
The first step of our method for finding the average path length
consists in finding an alternative bijection between Catalan trees and
Dyck paths. In \fig~\ref{fig:ctree}, we see a Catalan tree equivalent
to the Dyck path in \fig~\ref{fig:dyck}, built from the preorder
traversal of that tree. \Fig~\ref{fig:ctree_deg} shows the same tree,
where the contents of the nodes is their degree. The preorder
traversal (of the degrees) is \([3,0,0,2,1,0,0]\). Since the last
degree is always~\(0\) (a leaf), we remove it and settle for
\([3,0,0,2,1,0]\). Another equivalent Dyck path is obtained by mapping
the degrees of that list into as many occurrences of rises
(\(\uparrow\)) and one fall (\(\rightarrow\)), so, for instance,
\(3\)~is mapped to (\(\uparrow, \uparrow, \uparrow, \rightarrow\)) and
\(0\)~to (\(\rightarrow\)). In the end, \([3,0,0,2,1,0]\) is mapped
into \([\uparrow, \uparrow, \uparrow, \rightarrow, \rightarrow,
  \rightarrow, \uparrow, \uparrow, \rightarrow, \uparrow, \rightarrow,
  \rightarrow]\), which corresponds to the Dyck path in
\fig~\ref{fig:dyck_deg}. It is easy to convince ourself that we can
reconstruct the tree from the Dyck path, so we indeed have a
bijection.

The reason for this new bijection is that we need to find the average
number of Catalan trees whose root has a given degree. This number
will help us in finding the average path length, following an idea of
\cite{Ruskey_1983}. From the bijection, it is clear that the number of
trees whose root has degree~\(r=3\) is the number of Dyck paths made
of the segment from \((0,0)\) to \((0,r)\), followed by one fall (see
the dot at \((1,r)\) in \fig~\ref{fig:dyck_deg}), and then all
monotonic paths above the diagonal until the upper right corner
\((n,n)\). Therefore, we need to determine the number of such paths.

We have seen in section~\vref{sec:Catalan_height} the bijective
reflection of paths and the counting principle of inclusion and
exclusion. Let us add to our tools one more bijection which proves
often useful: the \emph{reversal}. It simply consists in reversing the
order of the steps making up a path. Consider for example
\fig~\ref{fig:path_reversal}.
\begin{figure}
\centering
\subfloat[Reversal of
\fig~\ref{fig:reflection} \label{fig:path_reversal}]{
\includegraphics[bb=71 606 188 721]{path_reversal}}
\qquad
\subfloat[Reversal and reflection of \fig~\ref{fig:dyck_deg} after \((1,3)\)\label{fig:path_deg}]{
\includegraphics[bb=71 634 162 721]{path_deg}}
\caption{Reversals and reflections}
\end{figure}
Of course, the composition of two bijections being a bijection, the
composition of a reversal and a reflection is bijective, hence the
monotonic paths above the diagonal from \((1,r)\) to \((n,n)\) are in
bijection with the monotonic paths above the diagonal from \((0,0)\)
to \((n-r,n-1)\). For example, \fig~\ref{fig:path_deg} shows the
reversal and reflection of the Dyck path of \fig~\ref{fig:dyck_deg}
after the point \((1,3)\), distinguished by the black disk
(\(\bullet\)).

Recalling that Catalan trees with \(n\)~edges are in bijection with
Dyck paths of length~\(2n\) (section~\vref{sec:Catalan_enumeration}),
we now know that the number of Catalan trees with \(n\)~edges and
whose root has degree~\(r\) is the number of monotonic paths above the
diagonal from the point \((0,0)\) to \((n-r,n-1)\). We can find this
number using the same technique we used for the total number~\(C_n\)
of Dyck paths. The principle of inclusion and exclusion says that we
should count the total number of paths with same extremities and
retract the number of paths that cross the diagonal. The former is
\(\binom{2n-r-1}{n-1}\), which enumerates the ways to interleave
\(n-1\)~rises (\(\uparrow\)) and \(n-r\) falls (\(\rightarrow\)). The
latter number is the same as the number of monotonic paths from
\((1,-1)\) to \((n-r,n-1)\), as shown by reflecting the paths up to
their first crossing, that is, \(\binom{2n-r-1}{n}\); in other words,
that is the number of interleavings of \(n\)~rises with \(n-r-1\)
falls. Finally, imitating the derivation of equation~\eqref{eq:Cn},
the number \(\mathcal{R}_n(r)\) of trees with \(n\)~edges and root of
degree~\(r\) is
\begin{equation*}
\mathcal{R}_n(r) = \binom{2n-r-1}{n-1} - \binom{2n-r-1}{n}
                 = \frac{r}{2n-r} \binom{2n-r}{n}.
\end{equation*}

Let \(\mathcal{N}_n(l,d)\) be the number of nodes in the set of all
Catalan trees with \(n\)~edges, which are at level~\(l\) and have
degree~\(d\). This number is the next step in determining the average
path length because \cite{Ruskey_1983} found a neat bijection to
relate it to \(\mathcal{R}_n(r)\) by the following equation:
\begin{equation*}
\mathcal{N}_n(l,d) = \mathcal{R}_{n+l}(2l+d).
\end{equation*}
In \fig~\ref{fig:level_deg}
\begin{figure}
\centering
\subfloat[\(n\)~edges, (\(\bullet\)) is of degree~\(d\) and at level~\(l\) \label{fig:level_deg}]{%
  \includegraphics[bb=71 594 150 721]{level_deg}}
\quad
\subfloat[\(n+l\) edges, root of degree~\(2l+d\)\label{fig:lifted_node}]{%
\includegraphics[bb=71 663 295 721]{lifted_node}
}
\caption{Bijection\label{fig:bij_root_level}}
\end{figure}
is shown the general pattern of a Catalan tree with node (\(\bullet\))
of level~\(d\) and degree~\(d\). The double edges denote a set of
edges, so the \(\mathcal{L}_i\), \(\mathcal{R}_i\) and
\(\mathcal{B}_i\) actually represent forests. In
\fig~\ref{fig:lifted_node} we see a Catalan tree in bijection with the
former, from which it is made by lifting the node of interest
(\(\bullet\)) to become the root, the forests \(\mathcal{L}_i\) with
their respective parents are attached below it, then the
\(\mathcal{B}_i\), and, finally, the \(\mathcal{R}_i\) for which new
parents are needed (inside a dashed frame in the figure). Clearly, the
new root is of degree \(2l+d\) and there are \(n+l\)
edges. Importantly, the transformation can be inverted for any tree
(it is injective and surjective), so it is indeed a bijection. We
deduce
\begin{equation}
\mathcal{N}_n(l,d) = \frac{2l+d}{2n-d}\binom{2n-d}{n+l}
= \binom{2n-d-1}{n+l-1} - \binom{2n-d-1}{n+l},
\label{eq:Nn_l_d}
\end{equation}
where the last step follows from expressing the binomial coefficient
in terms of the factorial function. In particular, this entails that
the total number of nodes at level~\(l\) in all Catalan trees with
\(n\)~edges is
\begin{equation*}
\sum_{d=0}^{n}\mathcal{N}_n(l,d)
  = \sum_{d=0}^{n}\binom{2n-d-1}{n+l-1}
    - \sum_{d=0}^{n}\binom{2n-d-1}{n+l}.
\end{equation*}
Let us consider the first sum:
\begin{equation*}
\sum_{d=0}^{n}\binom{2n-d-1}{n+l-1}
  = \sum_{i=n-1}^{2n-1}\binom{i}{n+l-1}
  = \sum_{i=n+l-1}^{2n-1}\binom{i}{n+l-1}.
\end{equation*}
We can now make use of the identity \eqref{eq:binom_sum}
\vpageref{eq:binom_sum}, which is equivalent to
\(\sum_{i=j}^{k}\binom{i}{j} = \binom{k+1}{j+1}\), so
\(j=n+l-1\) and \(k=2n-1\) yields
\begin{equation*}
\sum_{d=0}^{n}\binom{2n-d-1}{n+l-1} = \binom{2n}{n+l}.
\end{equation*}
Furthermore, replacing \(l\)~by \(l+1\) gives
\(\sum_{d=0}^{n}\binom{2n-d-1}{n+l} = \binom{2n}{n+l+1}\), so the
total number of nodes at level~\(l\) in all Catalan trees with
\(n\)~edges is
\begin{equation}
\sum_{d=0}^{n}\mathcal{N}_n(l,d)
 = \binom{2n}{n+l} - \binom{2n}{n+l+1}
 = \frac{2l+1}{2n+1}\binom{2n+1}{n-l}.
\label{eq:N_n_l_d}
\end{equation}

Let \(\Expected{P_n}\) be the \emph{average path length} of a Catalan
tree with \(n\)~edges. We have
\begin{equation*}
  \Expected{P_n} := \frac{1}{C_{n}} \cdot \sum_{l=0}^{n}l\sum_{d=0}^{n}\mathcal{N}_n(l,d),
\end{equation*}
because there are \(C_n\)~trees and the double summation is the sum of
the path lengths of all the trees. If we average again by the number
of nodes, \emph{i.e.,} \(n+1\), we obtain the average level of a node
in a random Catalan tree and beware that some authors take this as the
definition of the average path length. Alternatively, if we pick
distinct Catalan trees with \(n\)~edges at random and then pick
random, distinct nodes in them, \(\Expected{P_n}/(n+1)\) is the limit
of the average cost of reaching the nodes in question from the
root. Using equations \eqref{eq:N_n_l_d} and~\eqref{eq:Cn}
\vpageref{eq:Cn}, we get
\begin{align*}
\Expected{P_n} \cdot C_n
 &= \sum_{l=0}^{n}l\left[\binom{2n}{n+l} - \binom{2n}{n+l+1}\right]\\
 &= \sum_{l=1}^{n}l \binom{2n}{n+l} - \sum_{l=0}^{n-1}l \binom{2n}{n+l+1}\\
 &= \sum_{l=1}^{n}l \binom{2n}{n+l} - \sum_{l=1}^{n}(l-1)\binom{2n}{n+l}\\
 &= \sum_{l=1}^{n}\binom{2n}{n+l}
  = \sum_{i=n+1}^{2n}\binom{2n}{i}.
\end{align*}
The remaining summation is easy to crack because it is the sum of one
half of an even row in Pascal's triangle. We see in
\fig~\vref{fig:pascal_triangle} that the first half equals the second
half, only the central element remaining (there is an odd number of
entries in an even row). This is readily proven as follows:
\(\sum_{j=0}^{n-1}\binom{2n}{j} = \sum_{j=0}^{n-1}\binom{2n}{2n-j} =
\sum_{i=n+1}^{2n}\binom{2n}{i}\). Therefore
\begin{equation*}
\sum_{i=0}^{2n}\binom{2n}{i} = 2 \cdot \!\! \sum_{i=n+1}^{2n}\binom{2n}{i}
+ \binom{2n}{n},
\end{equation*}
and we can continue as follows:
\begin{equation*}
\frac{\Expected{P_n}}{n+1}
  = \frac{1}{2}\binom{2n}{n}^{-1}\left[\sum_{i=0}^{2n}\binom{2n}{i} -
    \binom{2n}{n}\right]
  = \frac{1}{2}\left[\binom{2n}{n}^{-1}\sum_{i=0}^{2n}\binom{2n}{i} - 1\right].
\end{equation*}
The remaining sum is perhaps the most famous combinatorial identity
because it is a corollary of the venerable \emph{binomial theorem},
which states that, for all real numbers \(x\)~and~\(y\), and all
positive integers~\(n\), we have the following equality:
\begin{equation*}
(x+y)^n = \sum_{k=0}^{n}\binom{n}{k}x^{n-k}y^k.
\end{equation*}
The truth of this statement can be seen by the following
reckoning. Since, by definition, \((x+y)^n =
\underline{(x+y)(x+y)\ldots(x+y)}_{\text{\(n\) times}}\), each term in
the expansion of \((x+y)^{n}\) has the form \(x^{n-k}y^k\), for
some~\(k\) ranging from~\(0\) to~\(n\), included. The coefficient of
\(x^{n-k}y^{k}\) for a given~\(k\) is simply the number of ways to
choose~\(k\) variables~\(y\) from the \(n\)~factors of \((x+y)^{n}\),
the \(x\)~variables coming from the remaining \(n-k\) factors.

Setting \(x=y=1\) yields the identity \(2^n =
\sum_{k=0}^{n}\binom{n}{k}\), which finally unlocks our last step:
\begin{equation}
\Expected{P_n} = \frac{n+1}{2}\left[4^{n}\binom{2n}{n}^{-1} - 1\right].
\label{eq:EPn}
\end{equation}
Recalling \eqref{eq:Cn} \vpageref{eq:Cn}, we obtain the asymptotic
expansion:
\begin{equation}
\Expected{P_n} \sim \frac{1}{2}n\sqrt{\pi n}.
\label{eq:Pn_asymptotics}
\end{equation}
Note that this equivalence also holds if~\(n\) denotes a number of
nodes, instead of edges. The exact formula for the average path length
of Catalan trees with \(n\)~nodes is \(\Expected{P_{n-1}}\) because
there are then \(n-1\) edges.

For some applications, it may be useful to know the external and
internal path lengths, which are, respectively, the path lengths up to
the leaves and to inner nodes (not to be confused with the external
and internal path lengths of binary trees). Let \(\Expected{E_n}\)~be
the former and \(\Expected{I_n}\) the latter. We have
\begin{align*}
\Expected{E_n} \cdot C_n
  &:= \sum_{l=0}^{n} l \cdot \mathcal{N}_n(l,0)
   = \sum_{l=0}^{n}l\left[\binom{2n-1}{n+l-1} -
     \binom{2n-1}{n+l}\right]\\
  &= \sum_{l=0}^{n-1}(l+1)\binom{2n-1}{n+l} -
     \sum_{l=0}^{n-1}l \binom{2n-1}{n+l}
   = \sum_{l=0}^{n-1}\binom{2n-1}{n+l},\\
\Expected{E_n} \cdot C_n
  &= \sum_{i=n}^{2n-1}\binom{2n-1}{i}
   = \frac{1}{2}\sum_{j=0}^{2n-1}\binom{2n-1}{j} = 4^{n-1},
\end{align*}
where the penultimate step follows from the fact that an odd row in
Pascal's triangle contains an even number of coefficients and the two
halves have equal sums. We conclude:
\begin{equation}
\Expected{E_n} = (n+1) 4^{n-1}\binom{2n}{n}^{-1} \sim
\frac{1}{4}n\sqrt{\pi n}.
\label{eq:EEn}
\end{equation}
The derivation of \(\Expected{I_n}\) is easy because
\begin{equation}
\Expected{P_n} = \Expected{E_n} + \Expected{I_n}.
\label{eq:PPn_EEn_EIn}
\end{equation}
From~\eqref{eq:EPn} and~\eqref{eq:EEn}, we express \(\Expected{P_n}\)
in terms of \(\Expected{E_n}\):
\begin{equation*}
\Expected{P_n} = 2 \Expected{E_n} - \frac{n+1}{2},
\end{equation*}
then, replacing it in~\eqref{eq:PPn_EEn_EIn}, we finally draw
\begin{equation*}
\Expected{I_n} = \Expected{E_n} - \frac{n+1}{2},
\end{equation*}
and
\begin{equation}
\Expected{I_n}
  = (n+1)4^{n-1}\binom{2n}{n}^{-1} - \frac{n+1}{2} \sim
  \frac{1}{4}n\sqrt{\pi n}.
\label{eq:EIn}
\end{equation}
Moreover, formulas \eqref{eq:EPn}, \eqref{eq:EEn} and~\eqref{eq:EIn}
entail
\begin{equation*}
\Expected{I_n} \sim \Expected{E_n} \sim \frac{1}{2}\Expected{P_n}.
\end{equation*}

\section{Average number of leaves}

The degree\hyp{}based bijection we introduced in
\fig~\vref{fig:degree_based_bijection} implies that there are
\((n+1)/2\) leaves in average in a random Catalan tree with
\(n\)~edges. Indeed, a leaf is a corner in the ordinary lattice path,
and it is \emph{not} a corner in the degree\hyp{}based lattice path,
that is, an internal node (non\hyp{}leaf), therefore, leaves and
non\hyp{}leaves are equinumerous and, since their total number is
\(n+1\), the average number of leaves is \((n+1)/2\).

This fact has an interesting consequence for the programming of
algorithms on Catalan trees, in particular, the data
structure. Indeed, a first idea would be to use a data constructor
\fun{tree} with a pair argument, whose first component is the data
stored at the root and the second component is the list of the
subtrees, for instance \(\fun{tree}(0,[\fun{tree}(1,\el),
\fun{tree}(2,\el)])\) denotes a tree with a root~\(0\), and two
leaves, \(1\)~and~\(2\). Note how leaves are uniquely distinguished by
an empty list of subtrees, which makes function definitions by case
easy because only two cases are enough to cover all
configurations. The downside becomes apparent when implementing the
algorithms in a real functional language, because storing a list means
having an indirection to the heap, where the contents of the list is
actually laid out. Therefore, with the previous encoding of Catalan
trees, each leaf means one indirection and no contents, which may seem
wasteful. If we assume that the Catalan trees to be processed are
random, the question boils down to determining the average number of
leaves, which we happily know now to be half of the nodes. In other
words, \(50\%\) of the nodes are represented in memory with an
indirection without contents. An alternative encoding consists in
using lists only if they contain at least one subtree, that is, only
for inner nodes, and leaves would be represented by a constructor
without list. The previous example would be thus rewritten:
\(\fun{inner}(0,[\fun{leaf}(1),\fun{leaf}(2)])\). This is not quite
right, because we could inadvertently write \(\fun{inner}(0,\el)\),
which is incorrect. Instead, we need to define a data structure for
encoding non\hyp{}empty lists, like so: \(\fun{single}(x)\), for the
singleton list containing~\(x\), and \(\fun{many}(x,l)\), for lists
containing at least two items, the first being~\(x\) and the remaining
ones being found in the sublist~\(l\). Consequently, the previous
example is now correctly written as
\(\fun{inner}(0,\fun{many}(\fun{leaf}(1),\fun{single}(\fun{leaf}(2))))\).

For further reading, we recommend the papers by
\citet*{DershwowitzZaks_1980,DershowitzZaks_1981,DershowitzZaks_1990}.

\section{Average height}
\label{sec:Catalan_height}

As mentioned earlier, the \emph{height} of a tree is the number of
edges on a maximal path from the root to a leaf, that is, a node
without subtrees; for example, we can follow down and count the edges
connecting the nodes~\((\circ)\) in \fig~\ref{fig:catalan_tree}. A
tree reduced to a single leaf has height~\(0\).

We begin with the key observation that a Catalan tree with \(n\)~edges
and height~\(h\) is in bijection with a Dyck path of length~\(2n\) and
height~\(h\) (see \fig~\vref{fig:catalan_tree} and
\fig~\ref{fig:bijection}). This simple fact allows us to reckon on the
height of the Dyck paths and transfer the result back to Catalan
trees.

We have already seen the correspondence between lattice paths and
Catalan trees, in which a rise reaching the $l$th diagonal corresponds
to a node at level~$l$ in the tree, counting levels from root
level~$0$. A simple bijection between paths will show that for every
node on level~$l$ of a tree of height~$h$ and size~$n$, there is a
corresponding node on either level $h-l+1$ or $h-l$ in another tree of
same height and size. (This bijection was found by Nachum Dershowitz.)

Consider the Dyck path in~\Fig~\ref{fig:h},
\begin{figure}
\centering
\includegraphics{h}
\caption{A Dyck path of length~\(2n\) and height~\(h\)\label{fig:h}}
\end{figure}
in bijection with a tree with~\(n=8\) edges and height~\(h=3\). Let us
find the last (rightmost) point on the path where it reaches its full
height (the dotted line of equation \(y = x + h\)), which we call the
\emph{apex} of the path (marked~$A$ in the figure). The immediately
following fall leads to~$B$ and it is drawn with a double line. Let us
rotate the segment from $(0,0)$ to~$A$, and the segment from~$B$
to~$(n,n)$ by 180$^\circ$. The invariant fall $(A,B)$ now connects the
rotated segments. This way, what was the apex becomes the origin and
vice\hyp{}versa, making this a height\hyp{}preserving bijection
between paths. See \fig~\ref{fig:h_prime}.
\begin{figure}
\centering
\includegraphics{h_prime}
\caption{Dyck path in bijection with \fig~\ref{fig:h}
\label{fig:h_prime}}
\end{figure}

The point is that every rise to level~\(l\) in \fig~\ref{fig:h},
representing a node on level~\(l\) in the corresponding Catalan tree,
ends up reaching level $h-l+1$ or $h-l$ in \fig~\ref{fig:h_prime},
depending on whether it was to the left (segment before~$A$) or right
(segment after~$B$) of the apex.  In the example in the figure, the
rise~$a$ reaches level~$1$, and its counterpart after the
transformation rises to level \(3-1+1=3\); the rise~$b$ reached
level~$2$ and still does so because \(3-2+1=2\); the rise~$c$ also
reached level~$2$, but because it was to the right of the apex, it
reaches now level \(3-2=1\). It follows from this bijection that
\emph{the average height is within one of twice the average level of a
  node, that is, the average path length}. Equation~\eqref{eq:EPn} is
equivalent to
\begin{equation*}
2\frac{\Expected{P_n}}{n+1} = 4^{n}\binom{2n}{n}^{-1} - 1.
\end{equation*}
If \(\Expected{H_n}\) is the average height of a Catalan tree with
\(n\)~edges, we then have, recalling~\eqref{eq:Pn_asymptotics},
\begin{equation*}
\Expected{H_n} \sim 2\frac{\Expected{P_n}}{n+1} \sim \sqrt{\pi n}.
\end{equation*}