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

Lehrmaterialien und erste Beispielprogramme 3.5.2018

parent aca02ca9
No related branches found
No related tags found
No related merge requests found
#include <iostream>
template <typename element>
class tree
{
struct leaf
{
element *content[3];
leaf *child[4];
};
leaf *root;
public:
tree ()
{
root = NULL;
}
};
int main ()
{
tree <int> t;
return 0;
}
#include <iostream>
using namespace std;
template <typename TElement>
class TTree
{
struct TEntry
{
TElement Content;
bool empty;
TEntry ()
{
empty = true;
}
};
struct TLeaf
{
TEntry Entry[3];
TLeaf *Leaf[4];
};
TLeaf *Root;
public:
TTree ()
{
Root = NULL;
}
void Insert (TElement e)
{
if (!Root)
Root = new TLeaf ();
Root->Entry[0].Content = e;
}
void Print ()
{
for (int i = 0; i < 3; i++)
cout << Root->Entry[i].Content << " ";
cout << endl;
}
};
int main ()
{
TTree <int> t;
t.Insert (42);
t.Print ();
return 0;
}
#include <iostream>
using namespace std;
template <typename TElement>
class TTree
{
struct TEntry
{
TElement Content;
bool empty;
TEntry ()
{
empty = true;
}
};
struct TLeaf
{
TEntry Entry[3];
TLeaf *Leaf[4];
};
TLeaf *Root;
public:
TTree ()
{
Root = NULL;
}
void Insert (TElement e)
{
if (!Root)
Root = new TLeaf ();
int i = 0;
while (i < 3 && !Root->Entry[i].empty)
i++;
if (i < 3)
Root->Entry[i].Content = e;
}
void Print ()
{
for (int i = 0; i < 3; i++)
cout << Root->Entry[i].Content << " ";
cout << endl;
}
};
int main ()
{
TTree <int> t;
t.Insert (42);
t.Insert (13);
t.Insert (137);
t.Print ();
return 0;
}
#include <iostream>
using namespace std;
template <typename TElement>
class TTree
{
struct TEntry
{
TElement Content;
bool empty;
TEntry ()
{
empty = true;
}
};
struct TLeaf
{
TEntry Entry[3];
TLeaf *Leaf[4];
TLeaf ()
{
for (int i = 0; i < 4; i++)
Leaf[i] = NULL;
}
void SetEntry (int i, TElement e)
{
Entry[i].Content = e;
Entry[i].empty = false;
}
};
TLeaf *Root;
public:
TTree ()
{
Root = NULL;
}
void Insert (TElement e)
{
if (!Root)
Root = new TLeaf ();
int i = 0;
while (i < 3 && !Root->Entry[i].empty)
i++;
if (i < 3)
Root->SetEntry (i, e);
}
void Print ()
{
for (int i = 0; i < 3; i++)
cout << Root->Entry[i].Content << " ";
cout << endl;
}
};
int main ()
{
TTree <int> t;
t.Insert (42);
t.Insert (13);
t.Insert (137);
t.Print ();
return 0;
}
../common/Zeichen_123.pdf
\ No newline at end of file
File added
% ad-20180503.pdf - Lecture Slides on Algorithms and Data Structures in C/C++
% Copyright (C) 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: Einführung in C++, Objekte
\documentclass[10pt,t]{beamer}
\usepackage{pgslides}
\usepackage{tikz}
\usepackage{soul}
\newcommand{\underconstruction}{%
\begin{picture}(0,0)
\color{black}
\put(7,-2.2){\makebox(0,0)[b]{\includegraphics[width=1.5cm]{Zeichen_123.pdf}}}
\put(7,-2.5){\makebox(0,0)[t]{\shortstack{Änderungen\\vorbehalten}}}
\end{picture}}
\title{Algorithmen und Datenstrukturen in C/C++}
\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski}
\date{3.\ Mai 2018}
\begin{document}
\maketitleframe
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\underconstruction
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/ad.git}}}
\item[\textbf{2}] \textbf{Einführung in C++}
\item[\textbf{3}] \textbf{Datenorganisation}
\begin{itemize}
\item[3.1] Standard-Container-Templates
\item[3.2] Iteratoren
\color{medgreen}
\item[3.3] Hash-Tabellen
\item[3.4] Balancierte Bäume
\color{red}
\item[3.5] Intelligente Zeiger
\end{itemize}
\item[\textbf{4}] \textbf{Datenkodierung}
% \begin{itemize}
% \item Fehlererkennung und -korrektur
% \item Kompression
% \item Kryptographie
% \end{itemize}
\item[\textbf{5}] \textbf{Hardwarenahe Algorithmen}
% \begin{itemize}
% \item FFT, CORDIC, \dots
% \end{itemize}
\item[\textbf{6}] \textbf{Optimierung}
% \begin{itemize}
% \item Wegfindung, \dots
% \end{itemize}
\color{gray}
\item[\textbf{7}] \textbf{Numerik}
\end{itemize}
\end{frame}
\setcounter{section}{2}
\section{Datenorganisation}
\setcounter{subsection}{2}
\subsection{Hash-Tabellen}
\begin{frame}[fragile]
\showsection
\showsubsection
Idee: Ein String ist auch nur eine große Zahl.\\
Wenn man die irgendwie zurechtstutzt,\\
kann man sie als Array-Index verwenden.
\bigskip
\textbf{Übungsaufgabe:}
\smallskip
Schreiben Sie ein Programm, das zu Strings (z.\,B.\ Namen)\\
Datensätze speichern und diese in $\mathcal{O}(1)$ wieder abrufen kann.
\begin{itemize}
\item[(a)]
Verwenden Sie das Hash-Tabellen-Template\\
aus der C++-Standardbibliothek.
\item[(b)]
Programmieren Sie die Hash-Tabelle selbst.
\end{itemize}
\smallskip
Teamarbeit, Internet-Recherchen\\
und die Verwendung anderer Container-Standard-Templates\\
sind ausdrücklich erlaubt.
\end{frame}
\subsection{Balancierte Bäume}
\begin{frame}[fragile]
\showsection
\showsubsection
\begin{itemize}
\item
\textbf{Problem:} Entartung von Bäumen\\
\textbf{Lösung:} variable Anzahl von Verzweigungen
\item
\textbf{Problem:} Datenstruktur zu groß für Hauptspeicher\\
\textbf{Lösung:} ``intelligentes'' Auslagern
\end{itemize}
\textbf{B-Baum:}
\begin{itemize}
\item
immer mindestens $t$, höchstens $2t$ Verzweigungen
\item
Einfügen: wenn Knoten voll, teilen
\item
Löschen:
\begin{itemize}\itemsep2pt
\item
unterste Ebene: ggf.\ verschieben, ggf.\ verschmelzen
\item
in inneren Knoten: Wert von unten ersetzen,
ggf.\ verschmelzen
\end{itemize}
\item
einfachster Fall: 2-3-4-Baum
\end{itemize}
\textbf{Übungsaufgabe}: Implementieren Sie einen 2-3-4-Baum\\
mit Methoden zum Suchen, Einfügen und Löschen
\end{frame}
\subsection{Intelligente Zeiger}
\begin{frame}[fragile]
\showsection
\showsubsection
\textbf{Warum?}
\begin{itemize}
\item
bereits freigegebene Zeiger werden u.\,U.\ weiterhin verwendet
\item
Speicherlecks
\item
uninitialisierte Zeiger
\end{itemize}
\medskip
\begin{itemize}
\item
\lstinline{shared_ptr}
\item
\lstinline{weak_ptr}
\item
\lstinline{unique_ptr}
\item
\lstinline{move()}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\showsection
\showsubsection
\textbf{Wie?}
\begin{itemize}
\item
R-Wert-Referenztypen: \lstinline{&&}
\item
\lstinline{move()}-Funktion
\end{itemize}
\bigskip
Literatur:
\begin{itemize}
\item
\url{http://thbecker.net/articles/rvalue_references/section_01.html}
\item
\url{http://www.artima.com/cppsource/rvalue.html}
\end{itemize}
\end{frame}
\if false
\subsection{Lambda-Ausdrücke}
\begin{frame}[fragile]
\showsection
\showsubsection
\textbf{Übergabe von Funktionszeigern}
\medskip
\begin{lstlisting}
int is_smaller (int a, int b)
{
return a < b;
}
sort (numbers.begin (), numbers.end (), is_smaller);
\end{lstlisting}
\bigskip
Stattdessen: \newterm{Lambda-Ausdrücke}
\end{frame}
\fi
\nosectionnonumber{\inserttitle}
\begin{frame}
\shownosectionnonumber
\begin{itemize}
\item[\textbf{1}] \textbf{Einführung}
\underconstruction
\hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/ad.git}}}
\item[\textbf{2}] \textbf{Einführung in C++}
\item[\textbf{3}] \textbf{Datenorganisation}
\begin{itemize}
\item[3.1] Standard-Container-Templates
\item[3.2] Iteratoren
\item[3.3] Hash-Tabellen
\color{medgreen}
\item[3.4] Balancierte Bäume
\item[3.5] Intelligente Zeiger
\end{itemize}
\item[\textbf{4}] \textbf{Datenkodierung}
% \begin{itemize}
% \item Fehlererkennung und -korrektur
% \item Kompression
% \item Kryptographie
% \end{itemize}
\item[\textbf{5}] \textbf{Hardwarenahe Algorithmen}
% \begin{itemize}
% \item FFT, CORDIC, \dots
% \end{itemize}
\item[\textbf{6}] \textbf{Optimierung}
% \begin{itemize}
% \item Wegfindung, \dots
% \end{itemize}
\color{gray}
\item[\textbf{7}] \textbf{Numerik}
\end{itemize}
\end{frame}
\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/pgslides.sty
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment