Title: Data Abstraction: Sets
1Data Abstraction Sets
- CMSC 11500
- Introduction to Computer Programming
- October 21, 2002
2Administration
- Midterm Wednesday in-class
- 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
3Roadmap
- Recap Structures of structures
- Data abstraction
- 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
4Recap Structures of Structures
- Family trees
- (Multiply) self-referential structures
- Data Definition -gt Template -gt 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)
5Template -gt 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))
6Data Abstraction
- Analogous to procedural abstraction
- Procedure is black box
- Dont care about implementation as long as
behaves as expected contract, purpose - Data object as black box
- Dont care about implementation as long as
behaves as expected
7Abstracting 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
8Data 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
9Sets Representations Efficiency
- Many possible representations
- Unordered lists
- Ordered lists
- Binary Search Trees
- How choose among representations?
- Efficiency Order of growth
- Tradeoffs in operations
10Sets 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)))))
11Member-of?
(define (member-of x set) member-of number set
-gt 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)
12Member-of? Hand-Evaluation
(define (member-of x set) member-of number set
-gt 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)))))
13Hand-Evaluation Contd
(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)))))
14Hand-Evaluation Contd
(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) ((eq? 3 3) t) (else
(member-of 3 (3 4))))
(cond (f f) (f t) (else (member-of
3 (cdr (2 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
15Hand-Evaluation Recursion Only
(member-of 3 (2 3 4))
(member-of 3 (3 4))
((eq? 3 3) t) t
16Adjoin
- 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)))
17Intersection
(define (intersection set1 set2) intersection
set set -gt 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(n2)
18Alternative Ordered Lists
- Set-of-numbers
- 1) ()
- 2) (cons n set-of-numbers)
- Where n is a number, and n lt all numbers in son
- Maintain constraint
- Anywhere add element to set
19Adjoin
(define (adjoin x set) (cond ((null? set) (cons x
()) ((eq? (car set) x) set) ((lt 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
20Sets 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
21Adjoin BST
(define (adjoin x set) (cond ((null? set)
(make-bstn x () ()) (( x (bstn-val set))
set) ((lt x (bstn-val set)) (make-bstn
(bstn-val set) (adjoin x (bstn-left set))
(bstn-right set))) ((gt x (bstn-val set))
(make-bstn (bstn-val set) (bstn-left
set) (adjoin x (bstn-right set))))
22BST Sets
- Analysis
- If balanced tree
- Each branch reduces tree size by half
- Successive halving -gt O(log n) growth
23Summary
- Data objects
- Defined by operations done on them
- Abstraction
- Many possible implementations of operations
- All adhere to same contract, purpose
- Different implications for efficiency
24Next Time
- Midterm
- Hand evaluations
- Structures
- Self-referential structures lists, structures of
structs - Data definitions, Templates, Functions
- Recursion in Fns follow recursion in data def