Data Abstraction: Sets CMSC 11500 Introduction to Computer Programming October 21, 2002
Administration Midterm Wednesday – in-class Based on lecture/notes/hwk Hand evaluations Structures Self-referential structures: lists, structures of structs Data definitions, Templates, Functions Recursion in Fns follow recursion in data def Recursive/Iterative processes Based on lecture/notes/hwk Not book details Extra office hrs: Tues 3-5, RY 178
Roadmap Recap: Structures of structures Data abstraction Summary Black boxes redux Objects as functions on them Example: Sets Operations: Member-of?, Adjoin, Intersection Implementations and Efficiency Unordered Lists Ordered Lists Binary Search Trees Summary
Recap: Structures of Structures Family trees: (Multiply) self-referential structures Data Definition -> Template -> Function (define-struct ft (name eye-color mother father)) Where name, eye-color: symbol; mother, father: family tree A family-tree: 1) ‘unknown, 2) (make-ft name eye-color mother father)
Template -> Function (define (bea aft) (cond ((eq? ‘unknown aft) #f) ((ft? aft) (let ((bea-m (bea (ft-mother aft))) (bea-f (bea (ft-father aft)))) (cond ((eq ?(ft-eye-color aft) ‘blue) (ft-name aft)) ((symbol? bea-m) bea-m) ((symbol? bea-f) bea-f) (else #f)))))) (define (fn-for-ft aft) (cond ((eq? ‘unknown aft)..) ((ft? aft) (cond …(ft-eye-color aft)… …(ft-name aft)…. …(fn-for-ft (ft-mother aft)).. …(fn-for-ft (ft-father aft))…
Data Abstraction Analogous to procedural abstraction Procedure is black box Don’t care about implementation as long as behaves as expected: contract, purpose Data object as black box Don’t care about implementation as long as behaves as expected
Abstracting Data Compound data objects Points, families, rational numbers, sets Data has many facets Also many possible representations Key: Data object defined by operations E.g. points: distance, slope, etc Any implementation acceptable if performs operations specified
Data Abstraction: Sets Set: Collection of distinct objects Venn Diagrams Defining functions Element-of?: true if element is member of set Adjoin: Adds element to set Union: Set containing elements of input sets Intersection: Set of elements in both input sets Any implementation of these functions defines a set data object
Sets: Representations & Efficiency Many possible representations: Unordered lists Ordered lists Binary Search Trees How choose among representations? Efficiency: Order of growth Tradeoffs in operations
Sets as Unordered Lists A Set-of-numbers is: 1) ‘() 2) (cons n set-of-numbers) Where n is number Set: Each element appears once Base template: (define (fn-for-set set) (cond ((null? set) …) (else (…(car set) ….(fn-for-set (cdr set)))))
Member-of? (define (member-of x set) ; member-of: number set -> boolean ; true if x in set, false otherwise (cond ((null? set) #f) ((eq? (car set) x) #t) (else (member-of x (cdr set))))) Order of growth: Length of list: O(n)
Member-of? Hand-Evaluation (define (member-of x set) ; member-of: number set -> boolean ; true if x in set, false otherwise (cond ((null? set) #f) ((eq? (car set) x) #t) (else (member-of x (cdr set))))) (member-of 3 ‘(1 2 3 4)) (cond ((null? ‘(1 2 3 4)) #f) ((eq? (car ‘(1 2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) ((eq? (car ‘(1 2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4)))))
Hand-Evaluation Cont’d (cond (#f #f) ((eq? 1 3) #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 (cdr ‘(1 2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 ‘(2 3 4)))) (cond ((null? ‘(2 3 4)) #f) ((eq? (car ‘(2 3 4)) 3) #t) (else (member-of 3 (cdr ‘(2 3 4)))))
Hand-Evaluation Cont’d (cond ((#f #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 ‘(3 4)))) (cond (#f #f) ((eq? (car ‘(2 3 4) 3) #t) (else (member-of 3 (cdr ‘(2 3 4))))) (cond ((#f #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 ‘(3 4)))) (cond (#f #f) ((eq? 2 3) #t) (else (member-of 3 (cdr ‘(2 3 4))))) (cond (#f #f) (#f #t) (else (member-of 3 (cdr ‘(2 3 4)))) (cond ((#f #f) ((eq? 3 3) #t) (else (member-of 3 ‘(3 4)))) (cond ((#f #f) (#t #t) (else (member-of 3 ‘(3 4)))) (cond ((#f #f) (#f #t) (else (member-of 3 ‘(3 4)))) (cond ((null? ‘(3 4) #f) ((eq? (car ‘(3 4) 3) #t) (else (member-of 3 (cdr ‘(3 4))))) #t
Hand-Evaluation: Recursion Only (member-of 3 ‘(2 3 4)) (member-of 3 ‘(3 4)) ((eq? 3 3) #t) #t
Adjoin (define (adjoin x set) (cond ((null? set) (cons x set)) Question: Single occurrence of element Maintain at adjoin? Always insert? Add condition to maintain invariant (one occurrence) Order of Growth: Member-of? test: O(n) (define (adjoin x set) (cond ((null? set) (cons x set)) ((eq? (car set) x) set) (else (cons (car set) (adjoin x (cdr set))))) (define (adjoin x set) (if (member-of? x set) set (cons x set)))
Intersection Order of growth: (define (intersection set1 set2) ;intersection: set set -> set (cond ((null? set1) ‘()) ((null? set2) ‘()) ((member-of? (car set1) set2) (cons (car set1) (intersection (cdr set1) set2)) (else (intersection (cdr set1) set2))) Order of growth: Test every member of set1 in set2 Member: O(n); Length of set1: O(n) O(n^2)
Alternative: Ordered Lists Set-of-numbers: 1) ‘() 2) (cons n set-of-numbers) Where n is a number, and n <= all numbers in son Maintain constraint: Anywhere add element to set
Adjoin (define (adjoin x set) (cond ((null? set) (cons x ‘()) ((eq? (car set) x) set) ((< x (car set)) (cons x set)) (else (cons (car set) (adjoin x (cdr set)))))) Note: New invariant adds condition Order of Growth: On average, check half
Sets as Binary Search Trees Bst: 1) ‘() 2) (make-bstn val left right) Where val is number; left, right are bst All vals in left branch less than current, right gtr (define-struct bstn (val left right)) 7 5 11 3 6 9 13
Adjoin: BST (define (adjoin x set) (cond ((null? set) (make-bstn x ‘() ‘()) ((= x (bstn-val set)) set) ((< x (bstn-val set)) (make-bstn (bstn-val set) (adjoin x (bstn-left set)) (bstn-right set))) ((> x (bstn-val set)) (bstn-left set) (adjoin x (bstn-right set))))
BST Sets Analysis: If balanced tree Each branch reduces tree size by half Successive halving -> O(log n) growth
Summary Data objects Defined by operations done on them Abstraction: Many possible implementations of operations All adhere to same contract, purpose Different implications for efficiency
Next Time Midterm Hand evaluations Structures Self-referential structures: lists, structures of structs Data definitions, Templates, Functions Recursion in Fns follow recursion in data def