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

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Playground Design Karl Lieberherr. Playground A collection of algorithms to define – what is wanted from a solve algorithm S in a particular domain (inputs/outputs)
CS 171: Introduction to Computer Science II
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Binary Trees Chapter 6.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
Tree.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 10-A Trees Modified
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
CS 367 Introduction to Data Structures Lecture 8.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
"Teachers open the door, but you must enter by yourself. "
UNIT III TREES.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Trees.
Week 11 - Friday CS221.
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures: Segment Trees, Fenwick Trees
DemeterF: Functions and Traversals in Combination
Binary Search Tree AVL Tree
"Teachers open the door, but you must enter by yourself. "
CMSC 202 Trees.
Binary Trees, Binary Search Trees
CSC 143 Binary Search Trees.
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
B-Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Midterm CSG 110 Karl Lieberherr

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

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)

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

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()))); }

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 }

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

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

Why do we need the last 2? BST not BST

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; }}

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); } }

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)); }

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.

Recursion without abstracting traversal with abstracting traversal

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.

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.

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.

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

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

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.

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.

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

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

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(); } }

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; } }

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.

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))

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

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

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

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.

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 ""; } }

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; } }

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.