Skip to content
Snippets Groups Projects
Commit acc2a744 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vorbereitung 29.1.2018

parent 39963a9f
No related branches found
No related tags found
No related merge requests found
Showing
with 1167 additions and 1 deletion
......@@ -20,7 +20,7 @@
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Datenstrukturen: Stack und FIFO
% README: Datenstrukturen: Stack und FIFO, verkettete Listen
\documentclass[10pt,t]{beamer}
......
File added
% hp-20180129.pdf - Lecture Slides on Low-Level Programming
% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Datenstrukturen: Bäume
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{rotating}
\usepackage{tikz}
\title{Angewandte Informatik\\Hardwarenahe Programmierung}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{29.\ Januar 2018}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}}
\item[\textbf{2}] \textbf{Einführung in C}
\item[\textbf{3}] \textbf{Bibliotheken}
\item[\textbf{4}] \textbf{Hardwarenahe Programmierung}
\item[\textbf{5}] \textbf{Algorithmen}
\item[\textbf{6}] \textbf{Objektorientierte Programmierung}
\item[\textbf{7}] \textbf{Datenstrukturen}
\begin{itemize}
\color{medgreen}
\item[7.1] Stack und FIFO
\item[7.2] Verkettete Listen
\color{red}
\item[7.3] Bäume
\end{itemize}
\end{itemize}
\vspace*{-1cm}
\end{frame}
\setcounter{section}{6}
\section{Datenstrukturen}
\subsection{Stack und FIFO}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{minipage}{0.48\textwidth}
Im letzten Praktikumsversuch:
\begin{itemize}
\item
Array nur zum Teil benutzt
\item
Variable speichert genutzte Länge
\item
Elemente hinten anfügen\\
oder entfernen
\arrowitem
Stack\\
\strut
\end{itemize}
\bigskip
\begin{itemize}
\item
hinten anfügen/entfernen: $\mathcal{O}(1)$\hspace*{-1cm}
\item
vorne oder in der Mitte einfügen/entfernen: $\mathcal{O}(n)$
\end{itemize}
\end{minipage}\hfill
\begin{minipage}{0.52\textwidth}
Auch möglich:
\begin{itemize}
\item
Array nur zum Teil benutzt
\item
2 Variable speichern\\genutzte Länge (ringförmig)
\item
Elemente hinten anfügen\\
oder vorne entfernen
\arrowitem
FIFO
\end{itemize}
\bigskip
\begin{itemize}
\item
vorne oder hinten\\
anfügen oder entfernen: $\mathcal{O}(1)$
\item
in der Mitte einfügen/entfernen: $\mathcal{O}(n)$
\end{itemize}
\end{minipage}\hspace*{-1.5mm}
\end{frame}
\begin{frame}[fragile]
\showsection
\showsubsection
\bigskip
\begin{minipage}[b]{6cm}
\begin{center}
"`First In -- First Out"'
\bigskip
\begin{picture}(6,4)
\thicklines
\color{structure}
\put(0.5,0){\line(1,0){5}}
\put(3.5,0){\only<1-5>{\line(0,1){1}}}
\put(4.5,0){\only<1-4>{\line(0,1){1}}}
\put(3.5,1){\only<1-4>{\line(1,0){1}}}
\put(4.0,0.5){\only<1-4>{\makebox(0,0){\lstinline{3}}}}
\put(3.0,1.5){\only<1>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(3.0,1.5){\only<1>{\makebox(0,0)[b]{\lstinline{push (3)}}}}
\put(2.5,0){\only<2-6>{\line(0,1){1}}}
\put(2.5,1){\only<2-5>{\line(1,0){1}}}
\put(3.0,0.5){\only<2-5>{\makebox(0,0){\lstinline{7}}}}
\put(2.0,1.5){\only<2>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,1.5){\only<2>{\makebox(0,0)[b]{\lstinline{push (7)}}}}
\put(1.5,0){\only<3-6>{\line(0,1){1}}}
\put(1.5,1){\only<3-6>{\line(1,0){1}}}
\put(2.0,0.5){\only<3-6>{\makebox(0,0){\lstinline{137}}}}
\put(1.0,1.5){\only<3>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(1.0,1.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(4.55,1.05){\only<4>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(5.00,1.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 3}}}
\put(3.55,1.05){\only<5>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,1.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(2.55,1.05){\only<6>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(3.00,1.60){\only<6>{\makebox(0,0)[b]{\lstinline{pop ()}: 137}}}
\end{picture}
\bigskip
FIFO = Queue = Reihe
\end{center}
\end{minipage}\hfill
\begin{minipage}[b]{6cm}
\begin{center}
"`Last In -- First Out"'
\bigskip
\begin{picture}(6,4)
\thicklines
\color{structure}
\put(1.5,0){\line(1,0){3}}
\put(2.5,0){\line(0,1){1}}
\put(3.5,0){\line(0,1){1}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,0.5){\makebox(0,0){\lstinline{3}}}
\put(2.0,1.5){\only<1>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,1.5){\only<1>{\makebox(0,0)[b]{\lstinline{push (3)}}}}
\put(2.5,1){\only<2-5>{\line(0,1){1}}}
\put(3.5,1){\only<2-5>{\line(0,1){1}}}
\put(2.5,2){\only<2-5>{\line(1,0){1}}}
\put(3.0,1.5){\only<2-5>{\makebox(0,0){\lstinline{7}}}}
\put(2.0,2.5){\only<2>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,2.5){\only<2>{\makebox(0,0)[b]{\lstinline{push (7)}}}}
\put(2.5,2){\only<3-4>{\line(0,1){1}}}
\put(3.5,2){\only<3-4>{\line(0,1){1}}}
\put(2.5,3){\only<3-4>{\line(1,0){1}}}
\put(3.0,2.5){\only<3-4>{\makebox(0,0){\lstinline{137}}}}
\put(2.0,3.5){\only<3>{\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,-0.45);}}}}
\put(2.0,3.5){\only<3>{\makebox(0,0)[b]{\lstinline{push (137)}}}}
\put(3.55,3.05){\only<4>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,3.60){\only<4>{\makebox(0,0)[b]{\lstinline{pop ()}: 137}}}
\put(3.55,2.05){\only<5>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,2.60){\only<5>{\makebox(0,0)[b]{\lstinline{pop ()}: 7}}}
\put(3.55,1.05){\only<6>{\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.45,0.45);}}}}
\put(4.00,1.60){\only<6>{\makebox(0,0)[b]{\lstinline{pop ()}: 3}}}
\end{picture}
\bigskip
LIFO = Stack = Stapel
\end{center}
\end{minipage}
%
% \dots
\end{frame}
\begin{frame}
\showsection
\showsubsection
\bigskip
\begin{minipage}[t]{6cm}
Array (Stack, FIFO):\\
in der Mitte einfügen
\begin{center}
\begin{picture}(6,3.8)
\thicklines
\color{structure}
\put(1.5,0){\line(1,0){3}}
\put(2.5,0){\line(0,1){3}}
\put(3.5,0){\line(0,1){3}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,0.5){\makebox(0,0){\lstinline{3}}}
\put(2.5,1){\line(1,0){1}}
\put(3.0,1.5){\makebox(0,0){\lstinline{7}}}
\put(2.5,2){\line(1,0){1}}
\put(3.0,2.5){\makebox(0,0){\lstinline{137}}}
\put(2.5,3){\line(1,0){1}}
\put(1.5,1.5){\makebox(0,0)[b]{\lstinline{push (5)}}}
\put(1.5,1.45){\makebox(0,0)[tl]{\tikz{\draw[-latex, line width=1pt](0,0)--(0.95,-0.45);}}}
\put(3.55,2.5){\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)..controls(0.5,0.45)..(0,0.9);}}}
\put(3.55,1.5){\makebox(0,0)[bl]{\tikz{\draw[-latex, line width=1pt](0,0)..controls(0.5,0.45)..(0,0.9);}}}
% \pause
\color{red}
\put(4.1,3.0){\makebox(0,0)[l]{\textbf{1.}}}
\put(4.1,2.0){\makebox(0,0)[l]{\textbf{2.}}}
\put(1.5,1.9){\makebox(0,0)[b]{\textbf{3.}}}
% \pause
\put(6.0,1.5){\makebox(0,0)[tl]{$\mathcal{O}(n)$}}
\put(5.95,1.45){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-1.3,0.4);}}}
\put(5.95,1.49){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-1.3,1.1);}}}
\end{picture}
\end{center}
\end{minipage}\hfill % \pause\hfill
\begin{minipage}[t]{5cm}
In Array (Stack, FIFO) \dots
\begin{itemize}
\item
einfügen: $\mathcal{O}(n)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:\\
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}
\end{frame}
\subsection{Verkettete Listen}
\begin{frame}
\showsection
\showsubsection
\begin{tikzpicture}
\color{structure}
\node(first) at (0,0.5) {first};
\node[shape=rectangle,draw,line width=1pt](3) at (1,2) {3};
\node[shape=rectangle,draw,line width=1pt](5) at (2,1) {5};
\node[shape=rectangle,draw,line width=1pt](7) at (3,2) {7};
\node[shape=rectangle,draw,line width=1pt](137) at (5,2) {137};
\node(NULL) at (7,2) {NULL};
\draw[-latex](first)--(3);
\only<1>{\draw[-latex](3)--(7);}
\only<2>{\draw[-latex](3) to[out=0] (5);}
\only<2>{\draw[-latex](5) to[in=180] (7);}
\draw[-latex](7)--(137);
\draw[-latex](137)--(NULL);
\end{tikzpicture}
\begin{itemize}
\item
Jeder Datensatz enthält einen Zeiger auf das nächste Element.
\item
Beim letzten Element zeigt der Zeiger auf \lstinline{NULL}.
\item
Eine Variable zeigt auf das erste Element.
\item
Wenn die Liste leer ist, zeigt die Variable auf \lstinline{NULL}.
\arrowitem
(einfach) \textbf{verkettete Liste}
\end{itemize}
\end{frame}
\begin{frame}
\showsection
\showsubsection
\begin{minipage}[t]{5cm}
In Array (Stack, FIFO) \dots
\begin{itemize}
\item
in der Mitte einfügen: $\mathcal{O}(n)$
\item
wahlfreier Zugriff: $\mathcal{O}(1)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:\\
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}\hfill
\begin{minipage}[t]{6cm}
In (einfach) verkettete/r Liste \dots
\begin{itemize}
\item
in der Mitte einfügen: $\mathcal{O}(1)$
\item
wahlfreier Zugriff: $\mathcal{O}(n)$
\item
suchen: $\mathcal{O}(n)$
\item
geschickt \hspace*{-1.7cm}\tikz{\draw[red](-1.7,0)--(0,0.2);\draw[red](-1.7,0.2)--(0,0);}
suchen: {\color{red}$\mathcal{O}(n)$}
\item
beim Einfügen sortieren:
$\mathcal{O}(n \log n)$ \hspace*{-1.8cm}\tikz{\draw[red](-1.8,0)--(0,0.2);\draw[red](-1.8,0.2)--(0,0);}
$\mathcal{O}(n^2)$
\end{itemize}
\end{minipage}
% \pause
\medskip
\begin{center}
\begin{minipage}[t]{6cm}
In (ausbalancierten) Bäumen \dots
\begin{itemize}
\item
in der Mitte einfügen: $\mathcal{O}(\log n)$
\item
wahlfreier Zugriff: $\mathcal{O}(\log n)$
\item
suchen: $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:
$\mathcal{O}(n \log n)$
\end{itemize}
\end{minipage}
\end{center}
\vspace*{-1cm}
\end{frame}
\subsection{Bäume}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{minipage}[t]{5cm}
\vspace*{-6cm}
\begin{lstlisting}[gobble=6]
¡typedef struct node
{
int content;
struct node *left, *right;
} node;¿
\end{lstlisting}
\bigskip
\bigskip
\begin{onlyenv}<6->
\begin{itemize}
\item
Einfügen: rekursiv, $\mathcal{O}(\log n)$
\item
Suchen: rekursiv, $\mathcal{O}(\log n)$
\item
beim Einfügen sortieren:\\
rekursiv, $\mathcal{O}(n\log n)$
\begin{onlyenv}<7->
\smallskip
\item
{\color{red}
\emph{Worst Case\/}: $\mathcal{O}(n^2)$\\
vorher bereits sortiert}\\
\textarrow\ balancierte Bäume\\
\hspace*{2.15em}Anwendung: Datenbanken\hspace*{-2cm}
\end{onlyenv}
\end{itemize}
\vspace*{-1cm}
\end{onlyenv}
\end{minipage}\hfill
\begin{minipage}[t]{7cm}
\begin{center}
\begin{tikzpicture}
\color{structure}
\node(root) at (0,0) {\lstinline{node *root;}};
\begin{onlyenv}<2>
\node(3) at (-2,-3) {\lstinline{NULL}};
\node(137) at (2,-3) {\lstinline{NULL}};
\end{onlyenv}
\begin{onlyenv}<2->
\node[shape=rectangle,draw,line width=1pt](7) at (0,-1.5) {7};
\draw[-latex](root)--(7);
\draw[-latex](7)--(3);
\draw[-latex](7)--(137);
\end{onlyenv}
\begin{onlyenv}<3>
\node(3) at (-2,-3) {\lstinline{NULL}};
\end{onlyenv}
\begin{onlyenv}<3->
\node[shape=rectangle,draw,line width=1pt](137) at (2,-3) {137};
\node(137_left) at (1,-4.5) {\lstinline{NULL}};
\node(137_right) at (3,-4.5) {\lstinline{NULL}};
\draw[-latex](137)--(137_left);
\draw[-latex](137)--(137_right);
\end{onlyenv}
\begin{onlyenv}<4>
\node(5) at (-1,-4.5) {\lstinline{NULL}};
\end{onlyenv}
\begin{onlyenv}<4->
\node[shape=rectangle,draw,line width=1pt](3) at (-2,-3) {3};
\node(3_left) at (-3,-4.5) {\lstinline{NULL}};
\draw[-latex](3)--(3_left);
\draw[-latex](3)--(5);
\end{onlyenv}
\begin{onlyenv}<5->
\node[shape=rectangle,draw,line width=1pt](5) at (-1,-4.5) {5};
\node(5_left) at (-2,-6) {\lstinline{NULL}};
\node(5_right) at (0,-6) {\lstinline{NULL}};
\draw[-latex](5)--(5_left);
\draw[-latex](5)--(5_right);
\end{onlyenv}
\end{tikzpicture}
\end{center}
\end{minipage}
\end{frame}
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\item[\textbf{2}] \textbf{Einführung in C}
\item[\textbf{3}] \textbf{Bibliotheken}
\item[\textbf{4}] \textbf{Algorithmen}
\item[\textbf{5}] \textbf{Hardwarenahe Programmierung}
\item[\textbf{6}] \textbf{Objektorientierte Programmierung}
\item[\textbf{7}] \textbf{Datenstrukturen}
\begin{itemize}
\item[7.1] Stack und FIFO
\item[7.2] Verkettete Listen
\color{medgreen}
\item[7.3] Bäume
\end{itemize}
\end{itemize}
\vspace*{-1cm}
\begin{flushright}
\rightskip 2cm
\large
\em
\color{orange}%
Viel Erfolg\hspace*{0.25cm}\strut\\
bei der Klausur!
\end{flushright}
\end{frame}
\end{document}
File added
% hp-uebung-20180122.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016, 2017, 2018 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Doppelt verkettete Liste, ternärer Baum
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{ifthen}
\usepackage{gensymb}
\usepackage{tikz}
\newcounter{points}
\newcommand{\points}[1]{\ifthenelse{#1=1}{(1 Punkt)}{(#1 Punkte)}\addtocounter{points}{#1}}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 22.\ Januar 2018}
Diese Übung enthält Punkteangaben wie in einer Klausur.
Um zu "`bestehen"', müssen Sie innerhalb von 60 Minuten
unter Verwendung ausschließlich zugelassener Hilfsmittel
10 Punkte (von insgesamt \totalpoints) erreichen.
\exercise{Doppelt verkettete Liste}
In der Vorlesung wurde ein Beispiel-Programm
(\gitfile{hp}{20180122}{lists-11.c})
zur Verwaltung einfach verketteter Listen erstellt
(an die Liste anhängen, in die Liste einfügen, Liste ausgeben usw.).
\begin{enumerate}[\quad(a)]
\item
Ergänzen Sie eine Funktion \lstinline{delete_from_list()}
zum Löschen eines Elements aus der Liste
mit Freigabe des Speicherplatzes.
\points{3}
\item
Ergänzen Sie eine Funktion \lstinline{print_reverse()}
die die Liste in umgekehrter Reihenfolge ausgibt.\\
(Hinweis: Rekursion)
\points{3}
\end{enumerate}
Eine doppelt verkettete Liste hat in jedem Knotenpunkt (\lstinline{node})
\emph{zwei\/} Zeiger -- einen auf das nächste Element (\lstinline{next})
und einen auf das vorherige Element (z.\,B.\ \lstinline{prev} für "`previous"').
Dadurch ist es leichter als bei einer einfach verketteten Liste,
die Liste in umgekehrter Reihenfolge durchzugehen.
\begin{quote}
\begin{tikzpicture}
\color{blendedblue}
\node(first) at (0,3.5) {first};
\node(NULL1) at (-1,1.25) {NULL};
\node[shape=rectangle,draw,line width=1pt](3) at (1,2) {3};
\node[shape=rectangle,draw,line width=1pt](7) at (3,2) {7};
\node[shape=rectangle,draw,line width=1pt](137) at (5,2) {137};
\node(NULL2) at (7,2.75) {NULL};
\draw[-latex](first)--(3);
\draw[-latex](3) to[out=45,in=135] (7);
\draw[-latex](3) to[out=-135,in=0] (NULL1);
\draw[-latex](7) to[out=-135,in=-45] (3);
\draw[-latex](7) to[out=45,in=135] (137);
\draw[-latex](137) to[out=-135,in=-45] (7);
\draw[-latex](137) to[out=45,in=180] (NULL2);
\end{tikzpicture}
\end{quote}
Der Rückwärts-Zeiger (\lstinline{prev}) des ersten Elements zeigt,
genau wie der Vorwärts-Zeiger (\lstinline{next}) des letzten Elements,
auf \emph{nichts}, hat also den Wert \lstinline{NULL}.
\begin{enumerate}[\quad(a)]\setcounter{enumi}{2}
\item
Schreiben Sie das Programm um für doppelt verkettete Listen.
\points{5}
\end{enumerate}
\clearpage
\exercise{Ternärer Baum}
Der in der Vorlesung vorgestellte \newterm{binäre Baum\/}
ist nur ein Spezialfall;
im allgemeinen können Bäume auch mehr als zwei Verzweigungen
pro Knotenpunkt haben.
Dies ist nützlich bei der Konstruktion \emph{balancierter Bäume},
also solcher, die auch im \emph{Worst Case\/}
nicht zu einer linearen Liste entarten,
sondern stets eine -- möglichst flache -- Baumstruktur behalten.
Wir betrachten einen Baum mit bis zu drei Verzweigungen pro Knotenpunkt,
einen sog.\ \newterm{ternären Baum}.
Jeder Knoten enthält dann nicht nur einen,
sondern \emph{zwei\/} Werte als Inhalt:
\begin{lstlisting}
typedef struct node
{
int content_left, content_right;
struct node *left, *middle, *right;
} node;
\end{lstlisting}
Wir konstruieren nun einen Baum nach folgenden Regeln:
\vspace{-\medskipamount}
\begin{itemize}\itemsep0pt
\item
Innerhalb eines Knotens sind die Werte sortiert:
\lstinline{content_left} muß stets kleiner sein als \lstinline{content_right}.
\item
Der Zeiger \lstinline{left} zeigt auf Knoten,
deren enthaltene Werte durchweg kleiner sind als \lstinline{content_left}.
\item
Der Zeiger \lstinline{right} zeigt auf Knoten,
deren enthaltene Werte durchweg größer sind als \lstinline{content_right}.
\item
Der Zeiger \lstinline{middle} zeigt auf Knoten,
deren enthaltene Werte durchweg größer sind als \lstinline{content_left},
aber kleiner als \lstinline{content_right}.
\item
Ein Knoten muß nicht immer mit zwei Werten voll besetzt sein;
er darf auch \emph{nur einen\/} gültigen Wert enthalten.
Der Einfachheit halber lassen wir in diesem Beispiel
nur positive Zahlen als Werte zu.
Wenn ein Knoten nur einen Wert enthält,
setzen wir \lstinline{content_right = -1},
und der Zeiger \lstinline{middle} wird nicht verwendet.
\item
Wenn wir neue Werte in den Baum einfügen,
werden \emph{zuerst\/} die nicht voll besetzten Knoten aufgefüllt
und \emph{danach erst\/} neue Knoten angelegt und Zeiger gesetzt.
\item
Beim Auffüllen eines Knotens darf nötigenfalls \lstinline{content_left}
nach \lstinline{content_right} verschoben werden.
Ansonsten werden einmal angelegte Knoten nicht mehr verändert.
\end{itemize}
\vspace*{-\medskipamount}
(In der Praxis dürfen Knoten gemäß speziellen Regeln
nachträglich verändert werden,
um Entartungen gar nicht erst entstehen zu lassen --
siehe z.\,B.\ \url{https://de.wikipedia.org/wiki/2-3-4-Baum}.)
\begin{enumerate}[\quad(a)]
\item
Zeichnen Sie ein Schaubild, das veranschaulicht,
wie die Zahlen 7, 137, 3, 5, 6, 42, 1, 2 und 12
nacheinander und in dieser Reihenfolge
in den oben beschriebenen Baum eingefügt werden
-- analog zu den Vortragsfolien (\gitfile{hp}{20180129}{hp-20180129.pdf}),
Seite 20.
\points{3}
%
% Lösung:
%
% 7 137
% / |
% 3 5 12 42
% / \
% 1 2 6
%
% (NULL-Zeiger sind hier nicht dargestellt,
% gehören aber dazu.)
%
\item
Dasselbe, aber in der Reihenfolge
2, 7, 42, 12, 1, 137, 5, 6, 3.
\points{3}
%
% Lösung:
%
% 2 7
% / | \
% 1 5 6 12 42
% / \
% 3 137
%
% (NULL-Zeiger sind hier wieder nicht dargestellt,
% gehören aber dazu.)
%
\item
Beschreiben Sie in Worten und/oder als C-Quelltext-Fragment,
wie eine Funktion aussehen müßte, um den auf diese Weise entstandenen Baum
sortiert auszugeben.
\points{4}
\end{enumerate}
\bigskip
\begin{flushright}
\textit{Viel Erfolg -- auch in der Klausur!}
\end{flushright}
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}}
\makeatother
\end{document}
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
../common/pgscript.sty
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
#include <stdio.h>
typedef struct
{
int content;
node *left, *right;
} node;
int main (void)
{
node *root = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
else
insert (&(*root)->left, content);
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
for (int i = 0; i < 30; i++)
insert (&root, rand () % 1000 + 1);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
else
insert (&(*root)->left, content);
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int output_tree_layer (node *root, int depth, int current_depth)
{
int result = 0;
if (root)
{
if (output_tree_layer (root->left, depth, current_depth + 1))
result = 1;
if (depth == current_depth)
{
printf ("%d ", root->content);
result = 1;
}
if (output_tree_layer (root->right, depth, current_depth + 1))
result = 1;
}
return result;
}
void output_tree (node *root)
{
if (root)
{
int depth = 0;
while (output_tree_layer (root, depth, 0))
{
printf ("\n");
depth++;
}
}
}
int main (void)
{
node *root = NULL;
for (int i = 0; i < 30; i++)
insert (&root, rand () % 1000 + 1);
output_tree (root);
return 0;
}
#include <stdio.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
int main (void)
{
node *root = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node *root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
root = p;
}
void output (node *root)
{
if (root)
printf ("%d\n", root->content);
}
int main (void)
{
node *root = NULL;
insert (root, 7);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
void output (node *root)
{
if (root)
printf ("%d\n", root->content);
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
void output (node *root)
{
if (root)
printf ("%d\n", root->content);
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else if (content > (*root)->content)
{
if ((*root)->right == NULL)
(*root)->right = p;
else
/* ??? */;
}
else
{
if ((*root)->left == NULL)
(*root)->left = p;
else
/* ??? */;
}
}
void output (node *root)
{
if (root)
printf ("%d\n", root->content);
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
if (*root == NULL)
*root = p;
else if (content > (*root)->content)
{
if ((*root)->right == NULL)
(*root)->right = p;
else
/* ??? */;
}
else
{
if ((*root)->left == NULL)
(*root)->left = p;
else
/* ??? */;
}
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
else if (content > (*root)->content)
insert ((*root)->right, content);
else
insert ((*root)->left, content);
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *left, *right;
} node;
void insert (node **root, int content)
{
if (*root == NULL)
{
node *p = malloc (sizeof (node));
p->content = content;
p->left = NULL;
p->right = NULL;
*root = p;
}
else if (content > (*root)->content)
insert (&(*root)->right, content);
else
insert (&(*root)->left, content);
}
void output (node *root)
{
if (root)
{
output (root->left);
printf ("%d\n", root->content);
output (root->right);
}
}
int main (void)
{
node *root = NULL;
insert (&root, 7);
insert (&root, 137);
insert (&root, 3);
insert (&root, 5);
output (root);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment