Slides Cours7 Interfaces Adt
Slides Cours7 Interfaces Adt
INFORMATIQUE 2
Abstract Classes, Interfaces, Abstract Data-Types and
Intro to Data Structure
Design Challenge ?
• Designing a geometric drawing program shapes with functionalities:
• Shapes should have the capability to print information about itself (quite general for all
shapes).
class ShapeUtils {
return totalArea;
}
• This approach ensures that our program can grow and adapt over time without
necessitating alterations to the existing, stable parts of the code.
fi
Exercise
• If I want to introduce triangles, What do I need to modify in those two classes ?
class ShapeUtils {
public static double calculateTotalArea(Shape[] shapes) {
double totalArea = 0.0;
for (Shape shape : shapes) {
totalArea += shape.calculateArea();
}
return totalArea;
}
}
Answer: Nothing!
• We just extend the shape class:
class Triangle extends Shape {
private double base;
private double height;
public Triangle(double base, double height) {
super("Triangle");
this.base = base;
this.height = height;
}
@Override
public double calculateArea() {
return 0.5 * base * height;
}
}
Standard classes:
All methods Interfaces:
implemented No method implemented
Advantage of interfaces
• A class can implement more than one interface, not possible with class extension. You can
only extend one class. Remember:
Ipods iPhones
Coolpix
Solution: Use Interfaces
@Override
public void takePhoto() {
System.out.println("Taking a photo");
}
@Override
public void recordVideo() {
System.out.println("Recording video");
}
@Override
public void playAudio() {
System.out.println("Playing audio");
}
@Override
public void playVideo() {
System.out.println("Playing video");
}
}
iPod
@Override
public void playVideo() {
System.out.println("Playing your favorite video");
}
}
<<Interface>>
Camera <<Interface>>
MediaPlayer
+ takePhoto(): void
+ recordVideo(): void + playAudio(): void
+ playVideo(): void
class CoolPixV1 class CoolPixV2 class IPhoneFourteen class IPhoneFifteen class IPodShuffle class IPodTouch class IPodNano
Abstract Data Types
• An interface (set of methods) that allows you to store and retrieve some data.
• When more than one data can be added/retrieved/removed we generally speak about a
collection.
• It doesn’t tell you how those those methods are implemented and also not how e cient
(complexity) those methods are. This is why it is abstract.
ffi
A Data-Structure
• Is one possible implementation of an ADT
• Several possible implementations are generally possible but it complies with the
speci cation of the methods of the ADT
E get(int index);
List<E> subList(int fromIndex, int toIndex);
int hashCode();
Object[] toArray();
int indexOf(Object o);
<T> T[] toArray(T[] a);
}
boolean isEmpty();
Iterator<E> iterator();
List Example Usage
public static void main(String[] args) {
⚠ ADT
List<String> fruits; // declaring a List typereference
of the interface
fruits = new LinkedList<>(); // Initializing it using LinkedList
// fruits = new ArrayList<>(); This would also work
// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Removing an element
fruits.remove("Banana");
}
Other ADT
• Bag
• Queue
• Stack
Bag: Non Ordered (also called Multi-Set)
public interface Bag<E> {
// Returns the top item from this stack without removing it.
T peek();
}
Two different implementations
• Using a linked-list structure <<Interface>>
Stack
+ push(E):E
+ pop(): E
+ peek(): E
+ isEmpty(): boolean
+ size(): int
top
top
The stack implementation
• It can also be implemented with a linked data-structure or with an array-based
implementation (left as an exercise).
• Linked data-structure (FIFO): keep a reference to the head and do the push/pop operations
from there
• rst
Array-based data-structure: push/pop at the end to avoid the shifts. You may also want to
reduce the size when the capacity is too large.
5 8 2 6 2 5 6 8 5
fi
The LinkedStack data-structure
• A series of nodes each containing some element and each referencing the next.
• The series start with the special node that we call “top”
• The problem is that the arrays have a xed size in java. Say N, it must be large enough to
store all the elements of the stack. Otherwise we double its size and copy the old content in
it.
5 8 2 6 2 5
push(6)
5 8 2 6 2 5 6
push(8)
5 8 2 6 2 5 6 8
push(5)
5 8 2 6 2 5 6 8 5
fi
DynamicArrayStack
public class DynamicArrayStack<T> implements Stack<T> {
private T[] array;
private int top;
+ push(E):E
+ pop(): E
+ peek(): E
+ isEmpty(): boolean
+ size(): int
LinkedStack DynamicArrayStack
DynamicArrayStac
LinkedStack
k
O(1) ‣1
Push O(1) Sauf redim.
‣2
O(n)
‣4
O(1) ‣8
Pop O(1) Sauf redim. ‣ 16
O(n) ‣…
‣n
Number of resizes = log2n
n push O(n) O(n^2) ?
log2 n
n
∑ 2i
n pop O(n) O(n^2) ? < 2n ∈ (n)
𝒪
i=0
Time Complexity ArrayStack n x push
n/2
n/4
n/8
n/16
n/32
n/64
…
Conclusion
DynamicArrayStack has excellent amortised complexities for n operations
LinkedStack DynamicArrayStack
O(1)
Push O(1) Except redim.
O(n)
O(1)
Pop O(1) Except redim.
O(n)
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 2 ɸ
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
3 +
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
3 +
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
8 3+5
Stacks: 2 *
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 16 2*8 ɸ
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 16 /
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 16 /
evals ops
Evaluation of arithmetic Expressions
( ( 2 * ( 3 + 5 ) ) / 4 )
Stacks: 4 16/4 ɸ
evals ops
Arithmetic Expression Evaluation with a Stack
> java ArithmeticExpression “( ( 2 * ( 3 + 5 ) ) / 4 )"
OperatorExpressionTree
3
*
2 -
- 3
ValueExpressionTree
5 7
BinaryExpressionTree
public abstract class BinaryExpressionTree {
private static class OperatorExpressionTree extends BinaryExpressionTree {
private nal BinaryExpressionTree left;
private nal BinaryExpressionTree right;
private nal char operator;
/
/ /
3
*
3 3 *
*
2 - 2 - 2 -
3 3 - 3
- -
5 7 5 7 5 7
Evaluating Arithmetic Expressions
3
*
2 -
- 3
5 7
Evaluating Arithmetic Expressions
• Post x:
3
*
2 -
- 3
5 7
fi
public abstract class BinaryExpressionTree {
@Override
int evaluate() {
return value;
}
}
}
fi
fi
fi
fi
Printing an expression
• expr.toString() // (2 * ((5+7)-3)) / 3
Also known ad the “in x” notation for
arithmetic expression
3
*
2 -
- 3
5 7
fi
public abstract class BinaryExpressionTree {
@Override
public String in xString() {
return "("+left.in xString()+operator+right.in xString()+")";
}
@Override
public String in xString() {
return value+""; public static void main(String[] args) {
} BinaryExpressionTree expr = value(2).mul(value(5).plus(value(7))) // 2*(5+7)
System.out.println(expr.in xString());
}
}
}
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
Exercise
• (2 * ((5+7)-3)) / 3 => 2 5 7 + 3 - 3 / *
• n = number of nodes
• Empty
• A binary search tree = is a binary tree with a “key” in each node such that
• This key is larger that all the keys in the left subtree
• This key is smaller than all the keys in the right subtree
Example
S
E X
null
A R
C B
E X
A R
C H
M
Interesting questions:
• How to nd the smallest key in a binary search tree ?
E X
A R
C H
M
fi
Linked BinaryTrees
private class Node {
E X
A R
C H
M
Worst and Best-Case
1
3 6
2
1 2 5 7
4