Presentation is loading. Please wait.

Presentation is loading. Please wait.

7.2 Spezifikation... am Beispiel funktionaler markierter Binärbäume, mit minimaler Signatur: Modell: data Tree t = Empty | Node(Tree t) t (Tree t) Invariante:

Similar presentations


Presentation on theme: "7.2 Spezifikation... am Beispiel funktionaler markierter Binärbäume, mit minimaler Signatur: Modell: data Tree t = Empty | Node(Tree t) t (Tree t) Invariante:"— Presentation transcript:

1 7.2 Spezifikation... am Beispiel funktionaler markierter Binärbäume, mit minimaler Signatur: Modell: data Tree t = Empty | Node(Tree t) t (Tree t) Invariante: - Signatur: -- Empty :: Tree t -- Node :: Tree t -> t -> Tree t -> Tree t root :: Tree t -> t left, right :: Tree t -> Tree t empty :: Tree t -> Bool Semantik: root (Node l v r) = v left (Node l v r) = l right(Node l v r) = r empty t = t == Empty

2 Bäume als veränderliche Objekte am Beispiel Dateibaum (Vielwegbaum!): Modell: data File t = Data t | Directory(String -> File t) Invariante: - Signatur: empty :: File t -- empty root directory list :: [String] -> File t -> [String] -- list directory contents remove :: [String] -> File t -> File t -- remove data file mkdir :: [String] -> File t -> File t -- make directory...

3 empty = Directory undefined list[](Directory dir) = domain dir -- = {name| defined(dir name)} list(name:path)(Directory dir) | defined(dir name ) = list path (dir name) | otherwise = error no such directory list _ (Data _) = error no such directory remove(name:path)(Directory dir) | defined(dir name) = Directory(redefine dir name new) | otherwise = error no such data file where new = remove path(dir name) remove[](Data _) = undefined remove[] _ = error no such data file

4 7.3 Implementierung imperativ z.B. als Geflecht interface BinTree { // funktionaler Baum ! // model: t :: BT x // data BT x = E | N(BT x)x(BT x) // inv: -- // BinTree null; // -- null == E // BinTree constructor(BinTree l, X x, // BinTree r); // pre: -- // post: result == N l x r.

5 X root(); // pre: -- (!) // post: result == x // where (N l x r) = t BinTree left(); // pre: -- // post: result == l // where (N l x r) = t BinTree right(); // pre: -- // post: result == r // where (N l x r) = t }

6 class LinkedTree implements BinTree { X root; LinkedTree left, right; // inv: defined(alpha left) && defined(alpha right) // -- precludes circular structures // abs: alpha null = E // alpha (root,left,right) = // N(alpha left)root(alpha right) public LinkedTree(LinkedTree l, X x, LinkedTree r) { // pre: -- // post: root == x && left == l && right == r root = x; left = l; right = r; }.

7 public X root() { // pre: -- // post: result == root return root; } public BinTree left() { // LinkedTree ? Only with GJ // pre: -- // post: result == left return left; } public BinTree right() { // pre: -- // post: result == right return right; }

8 Beispiel Operatorbaum: interface OpTree { // model:... // inv:... Operand eval(); // pre:... // post:... // OpTree simple(...); // Optree composite(...); // Optree empty(); does not exist ! }

9 class Simple implements OpTree { Operand value; public Operand eval() {return value;} public Simple(Operand v) {value = v;} } class Composite implements OpTree { DyadicOperator op; OpTree left,right; // never null! public Operand eval() { return op.call(left.eval(), right.eval()); } public Composite(DyadicOperator o, OpTree l, OpTree r) { op = o; left = l; right = r; } } interface DyadicOperator { T call(T l, T r); }

10 Beispiel Dateisystem: interface File { // T = type of data in data files // File directory(); constructor (empty file system) List list(List path) throws NoSuchDirectoryException; void remove(List path) throws NoSuchDataFileException; void mkdir (List path) throws MkdirException;... }

11 class Data implements File { public List list(List path) throws NoSuchDirectoryException { throw new NoSuchDirectoryException(); }... } class Directory implements File { class Entry{String name; File file; Entry(String n, File f){name=n; file=f;} } List dir = new ArrayList ();.

12 public List list(List path) throws NoSuchDirectoryException { List result = new ArrayList (); Iterator pathIt = path.iterator(); Iterator dirIt; List d = dir; descent: do {dirIt = d.iterator(); if (!pathIt.hasNext()) break; String nextname = pathIt.next(); while (dirIt.hasNext()) { Entry e = dirIt.next(); if (e.name.equals(nextname)) { d = ((Directory)e.file).dir; continue descent; } throw new NoSuchDirectoryException(); }while(true); while (dirIt.hasNext()) { boolean b = result.add(dirIt.next().name); } return result; }..... } // end class Directory

13 Vielwegbäume können als Binärbäume dargestellt werden: class MWTree { T root; MWTree firstborn, nextsibling; // Invariante: (keine Zyklen) // Bedeutung: (.......... !). }

14 Lineare Repräsentation binärer Bäume, z.B. in Feld: a) ebenenweise: WurzelEbene 1Ebene 2..... für vollständige Bäume (alternativ mit Leermarken für unbesetzte Positionen) b) rekursiv: Wurzellinker Teilbaumrechter Teilbaum bei Operatorbäumen identisch mit Präfix- (Polnischer) Notation !


Download ppt "7.2 Spezifikation... am Beispiel funktionaler markierter Binärbäume, mit minimaler Signatur: Modell: data Tree t = Empty | Node(Tree t) t (Tree t) Invariante:"

Similar presentations


Ads by Google