Presentation is loading. Please wait.

Presentation is loading. Please wait.

Midterm CSG 110 Karl Lieberherr. Managing Software Development Managers of software development must first be software developers.

Similar presentations


Presentation on theme: "Midterm CSG 110 Karl Lieberherr. Managing Software Development Managers of software development must first be software developers."— Presentation transcript:

1 Midterm CSG 110 Karl Lieberherr

2 Managing Software Development Managers of software development must first be software developers.

3 Tree Diameter The diameter of a tree T is the largest of the following quantities: – (1) diameter of T’s left subtree – (2) diameter of T’s right subtree – (3) the longest path between two leaves that goes through the root of T (computed from the heights of the subtrees of T)

4 Why do we need 1 and 2? diameter 9, NOT through root 1 2 5 6 9

5 Diameter class Diameter extends IDba{ // type unifying DiameterPair combine(BSTInt l) { return new DiameterPair(0,0);} DiameterPair combine(NodeInt t, Integer d, DiameterPair l, DiameterPair r){ return // normal combine: new DiameterPair(l.get_height() + // r.get_height(), l.get_diameter() + r.get_diameter()); new DiameterPair( Math.max(l.get_height(), r.get_height())+1, Math.max(l.get_height() + r.get_height() +1, Math.max(l.get_diameter(), r.get_diameter()))); }

6 Diameter class Diameter extends IDba{ // type unifying DiameterPair combine(BSTInt l) { return new DiameterPair(0,0);} DiameterPair combine(NodeInt t, Integer d, DiameterPair l, DiameterPair r){ return // normal combine: new DiameterPair(l.get_height() + // r.get_height(), l.get_diameter() + r.get_diameter()); new DiameterPair( // recursion Math.max(l.get_height(), r.get_height())+1, // height Math.max(l.get_height() + r.get_height() +1, Math.max(l.get_diameter(), r.get_diameter()))); // diameter }

7 Check BST Property An integer tree T, with integer n at its root, is a binary search tree (BST) if (all must hold) –the left and right tree of T are a BST –all nodes in the left tree of T are smaller than n –all nodes in right tree of T are greater than or equal to n

8 Reword: Check BST Property An integer tree T, with integer n at its root, is a binary search tree if (all must hold) –the left and right tree of T are a BST –the maximum of all nodes in the left tree of T is smaller than n –the minimum of all nodes in the right tree of T is greater than or equal to n

9 Why do we need the last 2? 8 1 3 9 10 BST not BST

10 Check for BST Property class Check extends IDb{ Pack combine(BST l){ return new Pack(true); } Pack combine(Node t, int d, Pack lt, Pack rt){ return new Pack((lt.good && rt.good && (lt.max < d) && (d <= rt.min)), Math.min(lt.min,d), Math.max(rt.max,d)); } static boolean check(BST tree){ return new Traversal(new Check()). traverse(tree).good; }}

11 Pack Helper Class class Pack{ boolean good; int min,max; Pack(boolean g, int mn, int mx) { good = g; min = mn; max = mx; } Pack(boolean g){ this(g, Integer.MAX_VALUE, Integer.MIN_VALUE); } }

12 BST Class Definitions class BST { BST insert(T d, Comp c){ return new Node (d, this, this); } public String toString() { return new Traversal(new ToStr()).traverse(this); } } class Node extends BST { T data; BST left, right; Node(T d, BST l, BST r){ data = d; left = l; right = r; } BST insert(T d, Comp c){ if(c.comp(d, data)) return new Node (data, left.insert(d,c), right); return new Node (data, left, right.insert(d,c)); }

13 Example 5 E 1 73 E E EE (t,+∞,-∞) (t,1,1) (t,1,3) (t,+∞,-∞) (t,7,7) (t,1,7) (t/f,min,max) for entire tree. min and max are only valid if tree is BST.

14 Recursion without abstracting traversal with abstracting traversal

15 Recipe for writing DemeterF programs combine method is activated when all subtraversals have returned. apply method is activated when combine method at the same node mis complete. update method is activated when the update method of the parent is complete.

16 Is the task type-unifying or type- preserving? Type-unifying We need to find the unifying type which may contain many more elements than only what we need to compute. We want a type T = (U1, U2,...) so that T carries just enough information to implement combine T combine (X x, T t1, T t2,...) at all nodes in the structure.

17 Type-unifying We do a problem decomposition: Assuming we have the result for t1 and t2, we need to fold t1 and t2 into a T-object to be returned. Simple if only t1. Might have several results to combine.

18 Type-unifying Examples Summation: Unifying type: (int sum). Base case: Leaf: (0) fold: addition

19 Type-unifying Examples Capacity Violation: Capacity violation of containers of items. violation depends on weight of container. Unifying type: (int weight, int violationCount). Base case: Item: (weight, 0) fold: vector addition

20 Type-unifying Examples Binary Search Tree Property: Given a binary tree of ints, are all nodes in the left tree smaller than the root and all nodes in the right subtree greater to or equal than the root. Consider the maximum max of the left subtree and the minimum min of the right subtree to do the max < d <= min check. Unifying type: (boolean BSTproperty, int min, int max) Base case: Empty tree: (true, +oo, -oo) fold: operation on two triples.

21 Type-unifying Examples Tree Diameter: height is needed to compute the longest path through root Unifying type: (int diameter, int height) Base case: Empty tree: (0,0) fold: operation on two pairs.

22 Type-unifying Examples CSP Formula Type: Unifying type: (List(Pair) v) where Pair = Relation Fraction. Fraction = float.

23 Type-unifying Examples Tree Height, top down: Unifying type: (int height) fold: max

24 Type-unifying Examples: Height In this top-down computation of the height, we use a separate class called Down* to avoid confusion with other arguments that are not related to sending information down. This is a good programming practice. // DownHeight = int. class Height3 extends IDba{ DownHeight update(NodeInt n, DownHeight h){ return new DownHeight(h.get_height()+1); } int combine(NodeInt t, int d, int l, int r){ return Math.max(l,r); } int combine(BSTInt l, DownHeight h){ return h.get_height(); } }

25 Type-unifying Examples Tree Height, bottom up: Unifying type: (int height) fold: max + 1 class Height2 extends IDb{ int combine(NodeInt t, int d, int l, int r){ return Math.max(l,r)+1; } int combine(BSTInt l) { return 0; } }

26 Type-preserving What are the exceptions? Put the exceptions into apply methods if the default combine at the same node is needed. Otherwise put exception into combine method.

27 Type-preserving Examples Node Increment: use apply method to increment each int by m. ;; Tree increment (define (Tree-add t m) (traverse t (extend-IDf ((number) (n) (+ n m))) Bc IDa everywhere))

28 Type-preserving Examples Reverse Binary Search Tree: Write combine method which swaps left and right.

29 Recipe Summary type-unifying? Find unifying type. Find fold operations. type-preserving? What are the exceptions?

30 Method Arguments in DemeterF We follow the rule that objects that are sent down must have a type of the form Down*.

31 combine arguments Y combine(X x,...) If X has k fields, combine has at least one and at most k+2 arguments. Argument k+2 must be of type Down*. Trailing arguments are optional.

32 Combine arguments class ToStr extends IDfb{ String apply(int i){ return ""+i; } String combine(NodeInt t, String d, String l, String r){ return "("+d+l+r+")"; } String combine(BSTInt l){ return ""; } }

33 apply arguments apply has one or two arguments. Y apply(X x) Y apply(X x, Down* d), where X is the return type of some combine method. class Incr extends IDf{ int apply(int i) { return i+1; } }

34 update arguments update has two arguments; the second is called the traversal argument. Down* update(X x, Down* d), where X is the type of an object that is visited while traversing down the object. The traversal (when called) must be passed some starting argument.


Download ppt "Midterm CSG 110 Karl Lieberherr. Managing Software Development Managers of software development must first be software developers."

Similar presentations


Ads by Google