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

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
Data Structures: Binary Trees CMSC Introduction to Computer Programming October 28, 2002.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
A Binary Search Tree Binary Search Trees.
SICP Search Algorithms Why search Data structures that support search Breadth first vs. depth first.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS 367 Introduction to Data Structures Lecture 8.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002.
Data Structures Red-Black Trees Design and Analysis of Algorithms I.
Binary Search Trees Chapter 7 Objectives
Data Abstraction: Sets
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 373 Binary search trees; tree height and balance
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
BST Trees
Efficiency of in Binary Trees
CSC 533: Programming Languages Spring 2017
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CSC 533: Programming Languages Spring 2015
CSC 533: Programming Languages Spring 2016
CSC 533: Organization of Programming Languages Spring 2008
COMP 103 Binary Search Trees.
Topic 18 Binary Search Trees
Binary Search Trees.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Binary Search Tree AVL Tree
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
CSC 143 Binary Search Trees.
CSC 533: Organization of Programming Languages Spring 2007
Bringing it all Together: Family Trees
Presentation transcript:

Data Abstraction: Sets Binary Search Trees CMSC 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

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

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

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.

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

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

Sets as Binary Search Trees {3,5,6,7,9,11,13} Balanced: same # nodes in left & right Unbalanced: anything else

Element-of? Contract: –;; element-of?: number bst -> boolean Purpose: –;; To determine if element is in set

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

Adjoin Contract: –;; adjoin: number bst -> bst Purpose –;; To create a new set with members of original set and new element

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

Intersection: BST Contract: –;;intersection: bst bst -> bst Purpose: –;;To build a set including items found in both sets

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)

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)

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

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