Pascal Schärli
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
boolean | char | byte | short | int | long | float | double | |
---|---|---|---|---|---|---|---|---|
boolean | ||||||||
char | ||||||||
byte | ||||||||
short | ||||||||
int | ||||||||
long | ||||||||
float | ||||||||
double |
int
int
int
int
long
float
double
int
int
int
long
float
double
int
int
int
int
int
int
int
int
int
long
long
long
long
long
long
long
float
float
float
float
float
float
float
float
float
double
double
double
double
double
double
double
double
double
double
double
a + b = c
a
b
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
6
Wert von a
5
Output
6
Wert von a
6
Output
int a = 5;
System.out.println(a++);
int a = 5;
System.out.println(++a);
Die Variabel wird erhöht und der alte Wert wird zurückgegeben
Die Variabel wird erhöht und der neue Wert wird zurückgegeben
Post Increment
Pre Increment
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
8
8
int a = 2;
if (x < 7) {
a = 8;
System.out.println(a);
}
System.out.println(a);
8
2
int a = 2;
if (x < 7) {
int a = 8;
System.out.println(a);
}
System.out.println(a);
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
public static void printListElements(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static void printListElements(ArrayList<Integer> list) {
for (Integer k : list) {
System.out.println(k);
}
}
For-Loops können kompakter notiert werden, falls man über alle Elemente einer Liste iterieren will:
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
1 2 4 8
int i = 1;
do{
System.out.println(i + " ");
i*=2;
} while(i < 10);
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
0 2 4 6 8
for (int i = 0; i < 10; i++) {
if (i % 2 != 0)
continue;
System.out.print(i + " ");
}
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
0
for (int i = 0; i < 10; i++) {
if (i % 2 != 0)
break;
System.out.print(i + " ");
}
Pascal Schärli - PVK Informatik II 2021
Voraussetzungen
boolean y = true;
String s = y ? "y was true" : "y was false";
System.out.println(s);
y was true
Pascal Schärli - PVK Informatik II 2021
int foo(int a, float b, boolean c){
if(c){
a += b;
}
else{
a-=b;
}
return a;
}
Datentyp vom Rückgabewert
Name der Funktion
Beliebig viele Übergabewerte
Funktions-Körper (Berechnung)
Rückgabe des Resultats
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
/**
* Checks wether x is within the interval [left, right]
*
* @param x
* @param left The left edge of the interval
* @param right The right edge of the interval
* @return true iff x is in the interval [left, right]
* @throws IllegalArgumentException if the interval is empty
*/
boolean inInterval(double x, double left, double right){
if(left > right){
throw new IllegalArgumentException("Empty Interval");
}
return x >= left && x <= right;
}
Voraussetzungen
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Prüfungsstatistiken
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer sbf1, StringBuffer sbf2) {
StringBuffer temp = sbf1;
sbf1 = sbf2;
sbf2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Hello World
Output:
Objektorientierung
Trotzdem dass beim Funktionsaufruf eine Referenz übergeben wird, werden die Stringbuffer nicht getauscht, warum?
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Hello World
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
s1
address1
s2
address2
swap
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
s1
address1
s2
address2
swap
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public static void swap(StringBuffer s1, StringBuffer s2){
StringBuffer temp = s1;
s1 = s2;
s2 =temp;
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Hello World
Hello World
public static void swap(StringBuffer s1, StringBuffer s2) {
StringBuffer temp = new StringBuffer(s1);
s1.delete(0, s1.length());
s1.append(s2);
s2.delete(0, s2.length());
s2.append(temp);
}
StringBuffer sbf1 = new StringBuffer("Hello");
StringBuffer sbf2 = new StringBuffer("World");
System.out.println(sbf1+" "+sbf2);
swap(sbf1,sbf2);
System.out.println(sbf1+" "+sbf2);
Hello World
Output:
sbf1
address1
address1
"Hello"
address2
"World"
sbf2
address2
Hello World
World Hello
s1
address1
s2
address2
swap
"Hello"
"World"
\(\rightarrow\) Man kann das zu Grunde liegende Objekt verändern!
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
{1, 4, 21, 8} → “1, 4, 21, 8, null“
Lists.java
package u5a1;
import node.Node;
public class Lists {
public static String toString(Node node) {
if (node == null) return "null";
StringBuffer buf = new StringBuffer();
buf.append(node.value).append(", ").append(toString(node.next));
return buf.toString();
}
}
Node.java
package node;
public class Node {
public int value;
public Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
public class Impedance {
private final String name;
protected double real, imag;
int i;
public Impedance(String name, double real,
double imag) {
this.name = name;
this.real = real;
this.imag = imag;
}
public static Impedance add(String name,
Impedance z1, Impedance z2) {
double real = z1.getReal() + z2.getReal();
double imag = z1.getImag() + z2.getImag();
return new Impedance(name, real, imag);
}
public String toString() {
return getName() + " = " + getReal() +
" + i" + getImag();
}
public String getName() {
return this.name;
}
public double getImag() {
return this.imag;
}
public double getReal() {
return this.real;
}
}
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public class Fahrzeug{
int radzahl;
public Fahrzeug(int radzahl){
this.radzahl = radzahl;
}
}
public class Auto extends Fahrzeug{
protected float hubraum;
public Auto(float hubraum){
super(4);
this.hubraum = hubraum;
}
public float getHubraum(){
return hubraum;
}
public void setHubraum(float hubraum){
if(hubraum>0) this.hubraum = hubraum;
}
}
public class Lastwagen extends Auto{
float capacity;
public Lastwagen(float hubraum,
float capacity){
super(hubraum);
this.capacity = capacity;
}
}
public class Fahrrad extends Fahrzeug{
public Fahrrad(){
super(2);
}
}
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myFahrrad = new Fahrrad();
System.out.println(myFahrrad.radzahl);
myFahrrad.setHubraum(300);
Fahrzeug myLastwagen = new Lastwagen(80000,50);
System.out.println(myLastwagen.radzahl);
myLastwagen.setHubraum(300);
Output: 4
Output: 2
Compile Error:
The method setHubraum(int) is undefined for the type Fahrzeug
Compile Error:
The method setHubraum(int) is undefined for the type Fahrzeug
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000,50);
((Lastwagen)myLastwagen).setHubraum(300);
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000,50);
System.out.println(myLastwagen.radzahl);
((Lastwagen)myLastwagen).setHubraum(300);
Fahrzeug myFahrrad = new Fahrrad();
System.out.println(myFahrrad.radzahl);
((Lastwagen)myFahrrad).setHubraum(300);
Runtime Error:
Exception in thread "main" java.lang.ClassCastException: Fahrzeug cannot be cast to Lastwagen
Fahrzeug
Auto
Lastwagen
Fahrrad
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Fahrzeug myLastwagen = new Lastwagen(80000, 50);
myLastwagen.setHubraum(300);
Fahrzeug myFahrrad = new Fahrrad();
((Lastwagen)myFahrrad).setHubraum(300);
Objektorientierung
Fahrrad
Lastwagen
Auto
Fahrzeug
Pascal Schärli - PVK Informatik II 2021
class Mensch { ... }
class ETHStudent extends Student { String legiNr; }
class ITETStudent extends ETHStudent { ... }
class Student extends Mensch { ... }
Student tim = new ETHStudent();
Object obj = tim;
Mensch mensch = tim;
Student student = tim;
ETHStudent ethStud = (ETHStudent) tim;
ITETStudent itetStud = (ITETStudent) tim;
Java denkt es ist ein Student
Wir wissen, es ist ein ETHStudent
ohne cast
mit cast
Runtime Error
zuweisung möglich ohne Cast
zuweisung möglich ohne Cast
zuweisung möglich mit Cast
Runtime Error
jede Java Klasse erbt von Object
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
class Mensch { ... }
class ETHStudent extends Student { String legiNr; }
class ITETStudent extends ETHStudent { ... }
class Student extends Mensch { ... }
Student tim = new ETHStudent();
Mensch mensch = tim;
Student student = tim;
ETHStudent ethStud = (ETHStudent) tim;
ITETStudent itetStud = (ITETStudent) tim;
System.out.println(mensch.legiNr);
System.out.println(student.legiNr);
System.out.println(ethStud.legiNr);
System.out.println(itetStud.legiNr);
Compile Error
Compile Error
Kein Compile Error
Kein Compile Error
haben legiNr
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Student tim = new ETHStudent();
System.out.println(tim instanceof Mensch);
System.out.println(tim instanceof Student);
System.out.println(tim instanceof ETHStudent);
System.out.println(tim instanceof ITETStudent);
true
true
true
false
Output:
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public abstract class AbstractStack {
// declare abstract functions
public abstract void push();
public abstract int pop();
public abstract int peek();
public abstract int size();
// already implement some functions
public boolean isEmpty(){
return size() == 0;
}
}
public class Stack extends AbstractStack {
// declare abstract functions
public void push(){/*TODO*/}
public int pop(){/*TODO*/}
public int peek(){/*TODO*/}
public int size(){/*TODO*/}
}
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public Interface IStack {
// declare abstract functions
public void push();
public int pop();
public int peek();
public int size();
}
public class Stack implements IStack {
// declare abstract functions
public int push(){/*TODO*/}
public int pop(){/*TODO*/}
public int peek(){/*TODO*/}
public int size(){/*TODO*/}
}
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Carina
Julia
Benutze ArrayTree
Meinung geändert, benutze ListTree
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
interface ITree{
public ITree leftChild();
public ITree rightChild();
public int getValue();
public int setValue();
}
public class TreeFactory {
public static ITree makeTree(){
return new ListTree();
}
}
Tree Interface
Tree Factory
public class FancyCode {
public ArrayTree important(){
ArrayTree myRoot = new ArrayTree();
myRoot.setValue(4);
return myRoot;
}
}
Julias Code Vorher
public class FancyCode {
public ITree important(){
ITree myRoot = TreeFactory.makeTree();
myRoot.setValue(4);
return myRoot;
}
}
Julias Code Jetzt
ArrayList myList = new ArrayList();
myList.add(42);
myList.add(“I <3 Info II”);
int i = (Integer)myList.get(0);
String s = (String)myList.get(1);
ArrayList Kann beliebige Typen speichern:
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(42);
int i = myList.get(0);
myList.add(“Hello World”);
Man kann einer ArrayList sagen, dass sie nur einen bestimmten Typ speichern darf:
Compile-Error
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
public class Tree<T> {
T value;
Tree right;
Tree left;
public Tree(T value) {
right = null;
left = null;
this.value = value;
}
public Tree<T> leftChild() {
return left;
}
public Tree<T> rightChild() {
return right;
}
}
Tree<Integer> tree1 = new Tree<Integer>(5);
tree1.left = new Tree<Integer>(3);
tree1.right = new Tree<Integer>(6);
Tree<String> tree2 = new Tree<String>("root");
tree2.right = new Tree<String>("right");
tree2.left = new Tree<String>("left");
5
3
6
"root"
"left"
"right"
tree1
tree2
Objektorientierung
Pascal Schärli - PVK Informatik II 2021
Herbst 2017, 1.17 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Activity{
private String name;
private double duration;
private double rating;
public Activity(String name, double duration, double rating) {
if(duration <= 0) {
throw new IllegalArgumentException("The duration can not be negative!");
}
if(rating < 0 || rating > 5) {
throw new IllegalArgumentException("The rating has to be between 0 and 5!");
}
this.name = name;
this.duration = duration;
this.rating = rating;
}
public String getName() {
return name;
}
public double getDuration() {
return duration;
}
public double getRating() {
return rating;
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Hike extends Activity {
private double difficulty;
public Hike(String name, double duration, double rating,
double difficulty) {
super(name, duration, rating);
if(difficulty < 1 || difficulty > 5) {
throw new IllegalArgumentException("The difficulty has to "
+ "be between 1 and 5!");
}
this.difficulty = difficulty;
}
public double getDifficulty() {
return difficulty;
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
import java.util.ArrayList;
public class ActivityManager{
ArrayList<Activity> activities;
public ActivityManager() {
activities = new ArrayList<Activity>();
}
public void addActivitiy(Activity activity){
activities.add(activity);
}
public Hike findBestHike(double maxDuration, double maxDifference){
// [..]
}
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public Hike findBestHike(double maxDuration, double maxDifficulty){
Hike bestHike = null;
for (Activity activity : activities) {
if (activity instanceof Hike) {
Hike hike = (Hike) activity;
if (hike.getDuration() <= maxDuration
&& hike.getDifficulty() <= maxDifficulty) {
if (bestHike == null
|| hike.getRating() > bestHike.getRating()) {
bestHike = hike;
}
}
}
}
return bestHike;
}
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 1.17 Notenpunkte
public class Main{
public static void main(String[] args){
ActivityManager = new ActivityManager();
// Adding activities
// Assume the parameter order for the Activity constructor is
// [name, duration, rating] and for the Hike constructor
// [name, duration, rating, difficulty]
manager.addActivitiy(new Activity("Spaziergang am See", 1.2, 2.5));
manager.addActivitiy(new Hike("Brunni - Kl. Mythen", 2, 4.5, 4.5));
manager.addActivitiy(new Activity("Velofahren Zuerich", 0.5, 1));
manager.addActivitiy(new Hike("Brunni - Gr. Mythen", 3, 3.5, 3.5));
manager.addActivitiy(new Activity("Schwimmen am Letten", 1, 4.5));
manager.addActivitiy(new Activity("Nichtstun", 0, 2.5));
manager.addActivitiy(new Hike("Weggis - Rigi Kulm", 5, 4, 3.5));
manager.addActivitiy(new Hike("Alpnach - Pilatus Kulm", 5, 5, 2));
double maxDuration = 4;
double maxDifficulty = 3.5;
Hike bestHike = manager.findBestHike(maxDuration, maxDifference);
System.out.println("Die beste Wanderung: " + bestHike.getName());
}
}
Die beste Wanderung: Brunni - Gr. Mythen
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeige, dass \(\frac{n^2 + 100}{2} \in O(n^2)\):
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 5 Wird n-Mal Ausgeführt.
private static int f1(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
out++;
}
return out;
}
\(O(n)\)
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 5 Wird ca. \(\lfloor \frac{n}{5} \rfloor \)-Mal Ausgeführt.
\(O(n)\)
private static int f2(int n) {
int out = 0;
for(int i = 0; i * 5 < n; i++) {
out++;
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(n^2\)-Mal Ausgeführt.
\(O(n^2)\)
private static int f3(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(1000 * n\)-Mal Ausgeführt.
\(O(n)\)
private static int f4(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < 1000; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Zeile 6 Wird \(\sum_{i=0}^{n-1}(i) = \frac{n*(n-1)}{2}\)-Mal Ausgeführt.
\(O(n^2)\)
private static int f5(int n) {
int out = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++){
out++;
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
\(O(n^6)\)
private static int f5(int n) {
int out = 0;
for(int i = 0; i < n*n; i++) {
for(int j = 0; j*j < i; j++){
for(int k = j*i; k > 0; k--){
out++;
}
}
}
return out;
}
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
public static int fibo(int n) {
if (n <= 1) return 1;
return fibo1(n - 1) + fibo1(n - 2);
}
Pascal Schärli - PVK Informatik II 2021
fibo(1) + fibo(0)
fibo(2) + fibo(1)
fibo(1) + fibo(0)
fibo(3) + fibo(2)
fibo(4)
Die Funktion ruft sich normalerweise ca. 2-mal rekursiv auf, der Resultierende Baum hat ungefähr Höhe \(n\) und somit ca \(2^n\) Knoten.
\(\rightarrow\) Zeit: \(O(2^n)\)
Der Baum wird "Depth First" traversiert, also ein Ast nach dem anderen, somit ist zu jedem Zeitpunkt höchstens ein Pfad von Wurzel bis Blatt im Speicher.
\(\rightarrow\) Speicher: \(O(n)\)
Speicher:
\(O(n)\)
Zeit:
\(O(2^n)\)
Komplexität
public static int fibo1(int n) {
if (n <= 1) return 1;
return fibo1(n - 1) + fibo1(n - 2);
}
public static int fibo2(int n) {
if(n <= 1) return 1;
int[] memory = new int[n + 1];
memory[0] = 1;
memory[1] = 1;
for (int i = 2; i <= n; i++) {
memory[i] = memory[i - 1] + memory[i - 2];
}
return memory[n];
}
public static int fibo3(int n) {
int last1 = 1;
int last2 = 1;
for (int i = 2; i <= n; i++) {
int temp = last1 + last2;
last1 = last2;
last2 = temp;
}
return last2;
}
Speicher:
\(O(n)\)
Zeit:
\(O(2^n)\)
Zeit:
\(O(n)\)
Speicher:
\(O(n)\)
Speicher:
\(O(1)\)
Zeit:
\(O(n)\)
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Komplexität
Pascal Schärli - PVK Informatik II 2021
Herbst 2017, 0.67 Notenpunkte
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.67 Notenpunkte
public static int algorithm1(int[] input){
assert input != null && input.length > 0;
int maxSum = input[0];
for(int i = 0; i < input.length; i++){
for(int j = 0; j < input.length; j++){
int sum = 0;
for(int k = i; k <= j; k++){
sum += input[k];
}
if(sum > maxSum){
maxSum = sum;
}
}
}
return maxSum;
}
Zeit:
\(O(input.length^3)\)
Zusätzlicher Speicher:
\(O(1)\)
Pascal Schärli - PVK Informatik II 2021
Prüfungsaufgabe
Herbst 2017, 0.67 Notenpunkte
public static int algorithm2(int[] input){
assert input != null && input.length > 0;
// calculate partial sums
int[] p = new int[input.length];
for(int i = 0; i < input.length; i++){
if(i == 0) p[i] = input[i];
else p[i] = p[i-1] + input[i];
}
// search for the maximum partial sum
int maxSum = input[0];
for(int i = 0; i < input.length; i++){
for(int j = i; j < input.length; j++){
int sum = (i == j) ? p[j] : p[j] - p[i];
if(sum > maxSum){
maxSum = sum;
}
}
}
return maxSum;
}
Zeit:
\(O(input.length^2)\)
Zusätzlicher Speicher:
\(O(input.length)\)
Pascal Schärli - PVK Informatik II 2021
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Vorteile
Wichtigste Operationen
Nachteile
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Vorteile
Wichtigste Operationen
Nachteile
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Wichtigste Operationen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Konzept
Anwendungen
Wichtigste Operationen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Kanten
Knoten
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Kanten haben eine Richtung
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
Zusammenhängender, Gerichteter Graph ohne Zyklen
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Knoten ohne Kindknoten
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
jeder Knoten besitzt höchstens zwei Kindknoten, die Reihenfolge der Kindknoten ist relevant.
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
jeder Knoten besitzt entweder zwei oder keine Kinder
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Alle Blätter haben die selbe Tiefe
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Ungleich verteilter Baum
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Anzahl “Levels” ohne Wurzel
→ hier: 3
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Graph
Gerichteter Graph
Baum
Blätter
Binärbaum
Voller Baum
Vollständiger Baum
Entarteter Baum
Höhe
Pfad
Weg durch Graph (Nur in Pfeilrichtung)
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
12 5 3 1 9 10 19 14 20 22
14
Gib den Wert vom Knoten aus
Gib den linken Teilbaum in Pre-Order aus
Gib den rechten Teilbaum in Pre-Order aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1 3 5 9 10 12 14 19 20 22
14
Gib den linken Teilbaum in In-Order aus
Gib den Wert vom Knoten aus
Gib den rechten Teilbaum in In-Order aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1 3 10 9 5 14 22 20 19 12
14
Gib den linken Teilbaum in Post-Order aus
Gib den rechten Teilbaum in Post-Order aus
Gib den Wert vom Knoten aus
22
20
19
12
10
9
5
3
1
Pascal Schärli - PVK Informatik II 2021
[ , , , , , , ]
Datenstrukturen
left child: 2*i+1
right child: 2*i+2
parent: ⌊(i-1)/2⌋
'H'
0
'B '
1
'J'
2
'A '
3
'E'
4
' '
5
'M'
6
0
1
3
4
5
6
2
Pascal Schärli - PVK Informatik II 2021
Indent = 1
Indent = 2
Indent = 3
Indent = 0
5
5
.8
5
.8
..3
5
.8
..3
..2
5
.8
..3
..2
...1
5
.8
..3
..2
...1
...9
5
.8
..3
..2
...1
...9
.7
5
.8
..3
..2
...1
...9
.7
..3
5
8
3
2
1
9
7
3
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
5( )
5(8( ) )
5(8(3 ) )
5(8(3,2( )) )
5(8(3,2(1 )) )
5(8(3,2(1,9)) )
5(8(3,2(1,9)),7( ))
5(8(3,2(1,9)),7(3))
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
5
8
7
3
2
9
3
1
Pascal Schärli - PVK Informatik II 2021
Datenstrukturen
1
3
5
6
9
10
12
18
20
19
22
Datenstrukturen
Pascal Schärli - PVK Informatik II 2021
1
3
5
6
9
10
12
18
20
19
22
Gesucht: 9
Datenstrukturen
Element Finden
Pascal Schärli - PVK Informatik II 2021