\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\author{Piyush P Kurur\\
  Office no: 224\\
  Dept. of Comp. Sci. and Engg.\\
  IIT Kanpur}


\newcommand{\Token}[2][]{\node(#2#1)[token]{#2};}
\newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};}
\newcommand{\Point}[1]{\node(#1)[point]{#1};}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes,chains,fit}
\usetikzlibrary{shapes.symbols}
\usetikzlibrary{matrix}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes.geometric}
\usepackage{multicol}

\usepackage{algorithm2e}
\usepackage{pgfkeys}


%% \newcommand{\SingleLinkedList}[1]{
%%   \foreach \datum/\anchor in {#1}
%%   \begin{scope}
%%     \node[rectangle, fill=yellow!30]{#2};
%%     \node[rectangle, below, fill=green!30](#1){$\cdot$};
%%   \end{scope}
%% }
%% \newcommand{\Null}{
%%   \node[circle, fill=black, minimum size=1em]{};
%% }
\pgfkeys {%
  /tikz/redbox/.style={%
    shape=rectangle,%
    minimum size=1.5em,%
    top color = white, %
    bottom color=red!50!black!50,
    inner sep=0pt,
    draw=black
  },
  /tikz/null/.style={%
    shape=circle,
    minimum size=0.5em,
    draw=black,
  },
  /tikz/greybox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black!20,
    inner sep=0pt,
    draw=black
  },
  /tikz/blackbox/.style={ 
    shape=rectangle,%
    minimum width=7em,%
    minimum height=1.5em,
    fill=black,
    inner sep=0pt,
    draw=black
  },
  /tikz/whitebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluebox/.style={
    shape=rectangle,
    minimum width=7em,
    minimum height=1.5em,
    fill=blue!40,
    draw=black
  },
  /tikz/whitetape/.style={
    shape=rectangle,
    minimum width=4em,
    minimum height=1.5em,
    draw=black
  },
  /tikz/bluearr/.style={
    ->,
    line width=0.25em, 
    draw=blue!40
  },
  /tikz/greenarr/.style={
    ->,
    line width=0.25em, 
    draw=green!40!black!60
  }
}

    
\newcommand{\arraythree}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1, right=of a0#3](a1#3){#2};
    \node[#1, right=of a1#3](a1#3){#2};
  \end{tikzpicture}
}

\newcommand{\arraytwo}[4]{
  \begin{tikzpicture}[node distance=0]
    \pgfsetstrokeopacity{#4};
    \node[#1](a0#3){#2};
    \node[#1,right=of a0#3]{#2};
  \end{tikzpicture}
}
  
\newcommand{\Array}[0]{
  \arraythree{box}{}{a}{1}
}

\newcommand{\AArray}[0]{%
  \arraythree{bbox}{\Array}{b}{0.5}}
\newcommand{\AAArray}[0]{%
  \arraytwo{bbbox}{\AArray}{c}{0.25}}


\newcommand{\T}[1]{\ensuremath{T\left(#1\right)}}  
\newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}}
\title{Fundamentals of Computing: Lecture 27}
\date{October 9, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Recursive data types}
  \begin{itemize}
  
  \item List.
    \pause
    A list is either an empty list or 
    an element followed by a list.
    \pause
  
  \item Binary trees.
    \pause     
    A binary tree is either an empty Tree or a root
    node with two children left subtree and right subtree.
    \pause

  \item General trees (some times called Rose trees).  \pause Either
    an empty tree or a node with a Forest of subtrees.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{List}
  \begin{tabbing}\tt
~~~~~data~List~a~\char61{}~Empty~\char124{}~Cons~a~\char40{}List~a\char41{}
\end{tabbing}
  \pause

  \begin{tabbing}\tt
~~~~~typedef~struct~Cons~Cons\char59{}\\
\tt ~~~~~typedef~Cons~\char42{}List\\
\tt ~~~~~struct~Cons~\char123{}\\
\tt ~~~~~~~int~datum\char59{}\\
\tt ~~~~~~~List~next\char59{}\\
\tt ~~~~~\char125{}\char59{}\\
\tt ~~~~~List~emptyList~\char61{}~\char40{}List\char41{}~NULL\char59{}
\end{tabbing}

\end{frame}

\begin{frame}
  \begin{tabbing}\tt
~~~~~int~head\char40{}List~l\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~if\char40{}~l~\char61{}\char61{}~NULL\char41{}~\char123{}error\char40{}\char34{}head~of~an~empty~list\char34{}\char41{}\char59{}\char125{}\\
\tt ~~~~~~~else~return~l~\char45{}\char62{}~datum\\
\tt ~~~~~\char125{}\\
\tt ~~~~~List~tail~\char40{}List~l\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~if\char40{}~l~\char61{}\char61{}~NULL\char41{}~\char123{}error\char40{}\char34{}tail~of~an~empty~list\char34{}\char41{}\char59{}\char125{}\\
\tt ~~~~~~~else~return~l~\char45{}\char62{}~next\\
\tt ~~~~~\char125{}
\end{tabbing}

\end{frame}

\begin{frame}
  \frametitle{Some list functions}
  Function \text{\tt singleton\char40{}x\char41{}} creates a list of just one element.

  \pause
  \begin{tabbing}\tt
~~~~~List~singleton\char40{}int~x\char41{}\char123{}\\
\tt ~~~~~~~List~l\char59{}\\
\tt ~~~~~~~l~\char61{}~\char40{}List\char41{}~malloc\char40{}sizeof\char40{}Cons\char41{}\char41{}\\
\tt ~~~~~~~if\char40{}~l~\char33{}\char61{}~NULL\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~l~\char45{}\char62{}~datum~\char61{}~x\char59{}\\
\tt ~~~~~~~~~l~\char45{}\char62{}~next~~\char61{}~NULL\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~~~return~l\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
     
  \begin{tabbing}\tt
~~~~~void~appendTo\char40{}List~\char42{}a\char44{}~List~b\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~List~ptr\char59{}\\
\tt ~~~~~~~if\char40{}~\char42{}a~\char61{}\char61{}~NULL\char41{}\char123{}\\
\tt ~~~~~~~~~\char42{}a~\char61{}~b\char59{}\\
\tt ~~~~~~~~~return\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~~~ptr~\char61{}~\char42{}a\char59{}\\
\tt ~~~~~~~while\char40{}ptr~\char45{}\char62{}~next~\char33{}\char61{}~NULL\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~ptr~\char61{}~ptr~\char45{}\char62{}~next\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~~~ptr~\char45{}\char62{}~next~\char61{}~b\char59{}\\
\tt ~~~~~~~return\char59{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
  \frametitle{Reverse a list}
  
  \only<1> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots, label=above:$\texttt{p}$](p){$42$};
      \node[redbox, right=of p, label=above:$\texttt{q}$](q){$15$};
      \node[redbox, right=of q, label=above:$\texttt{r}$](r){$100$};
      \node[redbox, right=of r](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q) -- (r);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
   \end{tikzpicture}
  }

  \only<2> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots, label=above:$p$](p){$42$};
      \node[redbox, right=of p, label=above:$q$](q){$15$};
      \node[redbox, right=of q, label=above:$r$](r){$100$};
      \node[redbox, right=of r](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q.east) to [out=0, in = 90] (p.north);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
    \end{tikzpicture}
  }

  \only<3> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots](p){$42$};
      \node[redbox, right=of p, label=above:$\texttt{p,q}$](q){$15$};
      \node[redbox, right=of q, label=above:$\texttt{r}$](r){$100$};
      \node[redbox, right=of r](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q.east) to [out=0, in = 90] (p.north);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
    \end{tikzpicture}
  }

  \only<4> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots](p){$42$};
      \node[redbox, right=of p, label=above:$\texttt{p}$](q){$15$};
      \node[redbox, right=of q, label=above:$\texttt{q,r}$](r){$100$};
      \node[redbox, right=of r](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q.east) to [out=0, in = 90] (p.north);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
    \end{tikzpicture}
  }


  \only<5> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots](p){$42$};
      \node[redbox, right=of p, label=above:$\texttt{p}$](q){$15$};
      \node[redbox, right=of q, label=above:$\texttt{q}$](r){$100$};
      \node[redbox, right=of r, label=above:$\texttt{r}$](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q.east) to [out=0, in = 90] (p.north);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
    \end{tikzpicture}
  }

  \only<6> {
    \begin{tikzpicture}[node distance=1.25em]
      \node[null](leftnull){};
      \node[redbox, right=of leftnull](a){2};
      \node[right=of a](leftdots){$\ldots$};
      \node[redbox, right=of leftdots](p){$42$};
      \node[redbox, right=of p, label=above:$\texttt{p}$](q){$15$};
      \node[redbox, right=of q, label=above:$\texttt{q}$](r){$100$};
      \node[redbox, right=of r, label=above:$\texttt{r}$](rnext){$34$};
      \node[right=of rnext](rightdots){$\ldots$};
      \node[redbox, right=of rightdots](b){$1$};
      \node[null, right=of b](rightnull){};
      
      \draw[->, thick] (a) -- (leftnull);
      \draw[->, thick] (leftdots) -- (a);
      \draw[->, thick](p) -- (leftdots);
      \draw[->,thick](q) -- (p);
      \draw[->,thick](r) -- (rnext);
      \draw[->, thick](rnext)--(rightdots);
      \draw[->, thick](rightdots) -- (b);
      \draw[->, thick](b) -- (rightnull);
    \end{tikzpicture}
  }

\end{frame}

\begin{frame}
  \begin{tabbing}\tt
~~~~~void~reverse\char40{}List~a\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~List~p\char44{}q\char44{}r\char59{}\\
\tt ~~~~~~~if\char40{}~a~\char61{}\char61{}~NULL\char41{}~return\char59{}\\
\tt ~~~~~~~p~\char61{}~NULL\char59{}\\
\tt ~~~~~~~q~\char61{}~a\char59{}\\
\tt ~~~~~~~r~\char61{}~a~\char45{}\char62{}~next\char59{}\\
\tt ~~~~~~~while\char40{}~r~\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~q~\char45{}\char62{}~next~\char61{}~p\char59{}\\
\tt ~~~~~~~~~p~\char61{}~q\char59{}\\
\tt ~~~~~~~~~q~\char61{}~r\char59{}\\
\tt ~~~~~~~~~r~\char61{}~r~\char45{}\char62{}~next\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~\char125{}
\end{tabbing}
\end{frame}
\end{document}
