18
Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Embed Size (px)

Citation preview

Page 1: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Data Abstraction: SetsBinary Search Trees

CMSC 11500

Introduction to Computer Programming

October 30, 2002

Page 2: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Roadmap

• Recap: Binary Trees

• Data abstraction:Sets• Objects as functions:Member-of?, Adjoin, Intersection• Implementations: Binary Search Trees

» Data Definition» Invariants » Template» Functions» Analysis & Efficiency

• Summary

Page 3: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Recap: Binary Trees

• Binary trees:– (Multiply) self-referential structures– Data Definition -> Template -> Function

• (define-struct bt (val left right))– Where val: number; left, right: binary-tree

• A binary-tree: 1) ‘unknown, – 2) (make-bt val left right)

• (define (fn-for-bt abt)– (cond ((eq? abt #f)…)– ((bt? abt) – (cond (…(bt-val abt)…)

….(fn-for-bt (bt-left abt))… ….(fn-for-bt (bt-right abt))…

Page 4: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Data Abstraction: Sets

• Set: collection of objects• Defined by operations that can be performed

– Set operations:• Element-of?, Adjoin, Union, Intersection, Set difference

• Many possible concrete implementations– Unordered lists– Ordered lists– Binary trees– Binary search trees

Page 5: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Data Definition

Binary search tree (bst) is– #f or,– (make-bt val left right)

• Where val: number; left, right: bst

• (define-struct bt (val left right))• Invariant:

– For a node n, the bt-val’s of all nodes in (bt-left n) are less than (bt-val n) and all node values in (bt-right n) are greater.

Page 6: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Data Invariants

• “invariant”: if it is true of the input, must be true of the output

• Invariant must be maintained by all functions that operate on the type– E.g. adjoin: new element must be >= anything

to its left, < than anything to its right

Page 7: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Template(define (fn-for-bst abst) (cond ((eq? abst #f)..)

((bt? abst) (cond (( …bt-val abst)…) …(fn-for-bst (bt-left abst)).. …(fn-for-bst (ft-right abst))…

Page 8: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Sets as Binary Search Trees

• {3,5,6,7,9,11,13}

• Balanced: same # nodes in left & right

• Unbalanced: anything else

7

5 11

3 6 9 13

Page 9: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Element-of?

• Contract: – ;; element-of?: number bst -> boolean

• Purpose: – ;; To determine if element is in set

Page 10: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Element-of?

(define (element-of? num abst) (cond ((eq? abst #f) #f)

((bt? abst) (cond ((= num (bt-val abst)) #t)

((< num (bt-val abst))(element-of? num (bt-left abst)))

((> num (bt-val abst))(element-of? num (bt-right abst)))))

Page 11: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Adjoin

• Contract:– ;; adjoin: number bst -> bst

• Purpose– ;; To create a new set with members of

original set and new element

Page 12: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Adjoin: BST

(define (adjoin x abst) (cond ((null? abst) (make-bt x #f #f)

((= x (bt-val abst)) abst) ((< x (bt-val abst))

(make-bt (bt-val abst) (adjoin x (bt-left abst)) (bt-right abst)))

((> x (bt-val abst)) (make-bt (bt-val abst)

(bt-left abst) (adjoin x (bt-right abst))))

Page 13: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Intersection: BST

• Contract:– ;;intersection: bst bst -> bst

• Purpose:– ;;To build a set including items found in both

sets

Page 14: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

Intersection

(define (intersection bst1 bst2)(intersect-iter bst1 bst2 #f)

(define (intersect-iter bst1 bst2 bst-new) (cond ((or (not bst1)(not bst2)) bst-new)

((element-of? (bt-val bst2) bst1)(intersect-iter (bt-right bst2) bst1 (intersect-iter (bt-left bst2) bst1 (adjoin (bt-val bst2) bst-new)

(else (intersect-iter (bt-right bst2) bst1 (intersect-iter (bt-left bst2) bst1 bst-new)

Page 15: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

BST Sets

• Analysis: – If balanced tree

• Each branch reduces tree size by half

Successive halving -> O(log n) growth• Element-of?: O(log n) • Adjoin: O(log n)

Page 16: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

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

Page 17: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

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

Page 18: Data Abstraction: Sets Binary Search Trees CMSC 11500 Introduction to Computer Programming October 30, 2002

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