Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Programming Languages and Techniques (CIS120)

Name: CIS 120e Midterm I Review

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

TREES Lecture 12 CS2110 Spring 2019

Data Structures in Java

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSC148 Week 7. Larry Zhang

Binary Search Tree (3A) Young Won Lim 6/2/18

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Balanced Trees. Nate Foster Spring 2018

Programming Languages and Techniques (CIS120)

Programming Languages

Programming Languages and Techniques (CIS120)

CS 171: Introduction to Computer Science II. Binary Search Trees

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Programming Languages and Techniques (CIS120)

Binary Search Tree (2A) Young Won Lim 5/17/18

Which of the following expressions has the type int list?

Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Binary Search Trees. Analysis of Algorithms

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

INF2220: algorithms and data structures Series 1

Friday Four Square! 4:15PM, Outside Gates

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/6/18

Programming Languages and Techniques (CIS120)

CS 206 Introduction to Computer Science II

Recap: ML s Holy Trinity

Data Structures in Java

Programming Languages

CSC/MAT-220: Lab 6. Due: 11/26/2018

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

CS115 - Module 10 - General Trees

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Module 9: Binary trees

Lecture 7. Binary Search Trees and Red-Black Trees

CSE 130 [Winter 2014] Programming Languages

(Provisional) Lecture 32: Estimated Value, Minimax, Functional Data 10:00 AM, Nov 20, 2017

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Maps; Binary Search Trees

Trees. Eric McCreath

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Programming Languages

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Lists. Michael P. Fourman. February 2, 2010

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Chapter 20: Binary Trees

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

Priority Queues Heaps Heapsort

Lecture 15 CIS 341: COMPILERS

CS F-11 B-Trees 1

Module 8: Binary trees

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

TREES. Trees - Introduction

Programming Languages and Techniques (CIS120)

Data Structures and Algorithms

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Programming Languages

Programming Languages and Techniques (CIS120)

Principles of Functional Programming

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

List Functions, and Higher-Order Functions

Programming Languages

Typed Racket: Racket with Static Types

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Red-Black trees are usually described as obeying the following rules :

Programming II (CS300)

Programming Languages and Techniques (CIS120)

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

8. Binary Search Tree

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Programming Languages and Techniques (CIS120)

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Programming, Block C. Today s Topics. wstomv/2ip05/ Branching dynamic data structures with pointers: Lecture 15.

Balanced Search Trees. CS 3110 Fall 2010

Transcription:

Programming Languages and Techniques (CIS120) Lecture 8 February 1, 2016 BST Delete Generics (Chapters 7 & 8)

Announcements Read Chapters 7 & 8 (BSTs, generics) of lecture notes Read Chapter 9 of lecture notes (Higher- order functions) HW2 due tomorrow at midnight My office hours: Today 3:30 5:00

Trees as containers Big idea: find things faster by searching less

Trees as Containers Like lists, binary trees aggregate data type tree = Empty Node of tree * int * tree As we did for lists, we can write pure functions for working with this container lookup: determine whether the tree contains a particular element insert: return a new tree containing a particular element delete: return a new tree with a particular element removed (if present)

Binary Search Trees A binary search tree (BST) is a binary tree with some additional invariants*: Node(lt,x,rt) is a BST if - lt and rt are both BSTs - all nodes of lt are x - all nodes of rt are > x Empty is a BST The BST invariant means that container functions can take time proportional to the height instead of the size of the tree. CIS120

Constructing BSTs from other BSTs Deleting an element delete :: tree - > int - > tree

Deletion No Children: (delete t 3) 3 5 5 > 1 3 > 1 > > 7 0 3 9 8

Deletion No Children: (delete t 3) 5 > 1 7 > 0 9 If the node to be deleted has no children, simply replace it by the Empty tree. 8

Deletion One Child: (delete t 7) 5 7 > 5 > 1 > > 7 0 3 9 8

Deletion One Child: (delete t 7) 5 > If the node to be delete has one child, replace the deleted node by its child. 1 > 9 0 3 8

Deletion Two Children: (delete t 5) 5 > 1 > > 7 0 3 9 8

Deletion Two Children: (delete t 5) 3 > If the node to be delete has two children, promote the maximum child of the left tree. 1 7 > 0 3 9 8

How to Find the Maximum Element? What is the max element of this subtree? 1 5 7 0 3 9 8

How to Find the Maximum Element? Just for fun, how do we find the max element of the whole tree? 5 1 7 0 3 9 8

Tree Max: A partial* function let rec tree_max (t:tree) : int = begin match t with Node(_,x,Empty) -> x Node(_,_,rt) -> tree_max rt _ -> failwith tree_max called on Empty end We never call tree_max on an empty tree This is a consequence of the BST invariants and the case analysis done by the delete function BST invariant guarantees that the maximum- value node is farthest to the right * Partial, in this context, means not defined for all inputs.

Code for BST delete trees.ml

Deleting From a BST let rec delete (t: tree) (n: int) : tree = begin match t with Empty -> Empty Node(lt, x, rt) -> if x = n then begin match (lt, rt) with (Empty, Empty) -> Empty (Node _, Empty) -> lt (Empty, Node _) -> rt _ -> let m = tree_max lt in Node(delete lt m, m, rt) end else if n x then Node(delete lt n, x, rt) else Node(lt, x, delete rt n) end

If we insert a label n into a BST and then immediately delete n, do we always get back a tree of exactly the same shape? 1. yes 2. no Answer: no, what if the node is in the tree

If we insert a value n into a BST that does not already contain n and then immediately delete n, do we always get back a tree of exactly the same shape? 1. yes 2. no Answer: yes

If we delete n from a BST (containing n) and then immediately insert n again, do we always get back a tree of exactly the same shape? 1. yes 2. no Answer: no, what if we delete the root?

Generic Functions and Data Wow, implementing BSTs took quite a bit of typing... Do we have to repeat it all again if we want to use BSTs containing strings, or characters, or floats? or How not to repeat yourself, Part I.

Structurally Identical Functions Observe: many functions on lists, trees, and other datatypes don t depend on the contents, only on the structure. Compare: length for int list vs. string list let rec length (l: int list) : int = begin match l with [] -> 0 _::tl -> 1 + length tl end let rec length (l: string list) : int = begin match l with [] -> 0 _::tl -> 1 + length tl end The functions are identical, except for the type annotation. CIS120

Notation for Generic Types OCaml provides syntax for functions with generic types let rec length (l:'a list) : int = begin match l with [] -> 0 _::tl -> 1 + (length tl) end Notation: 'a is a type variable; the function lengthcan be used on a t list for any type t. Examples: length [1;2;3] use length on an intlist length [ a ; b ; c ] use length on a string list CIS120

Generic List Append Note that the two input lists must have the same type of elements. The return type is the same as the inputs. let rec append (l1:'a list) (l2:'a list) : 'a list = begin match l1 with [] -> l2 h::tl -> h::(append tl l2) end Pattern matching works over generic types! In the body of the branch: h has type 'a tl has type 'a list CIS120

Generic Zip Functions can operate over multiple generic types. let rec zip (l1:'a list) (l2:'b list) : ('a*'b) list = begin match (l1,l2) with (h1::t1, h2::t2) -> (h1,h2)::(zip t1 t2) _ -> [] end Distinct type variables can be instantiated differently: zip [1;2;3] [ a ; b ; c ] Here, 'a is instantiated to int, 'b to string Result is [(1, a );(2, b );(3, c )] of type (int * string) list CIS120

User- Defined Generic Datatypes Recall our integer tree type: type tree = Empty Node of tree * int * tree We can define a generic version by adding a type parameter, like this: Parameter 'a used here type 'a tree = Empty Node of 'a tree * 'a * 'a tree CIS120 Note that the recursive uses also mention 'a

User- Defined Generic Datatypes BST operations can be generic too; only change is to the type annotation (* Insert n into the BST t *) let rec insert (t:'a tree) (n:'a) : a tree = begin match t with Empty -> Node(Empty,n,Empty) Node(lt,x,rt) -> if x = n then t else if n x then Node(insert lt n, x, rt) else Node(lt, x, insert rt n) end CIS120 Equality and comparison are generic they work for any type of data too.

Does the following function typecheck? 1. yes 2. no let f (l : 'a list) : 'b list = begin match l with [] -> true::l _::rest -> 1::l end Answer: no, even though the return type is generic, the two branches must agree (so that b can be consistently instantiated). CIS120

Does the following function typecheck? let f (x : 'a) : 'a = x + 1 ;; print_endline (f hello ) 1. yes 2. no Answer: no, the type annotations and uses of f aren t consistent CIS120

First- class Functions Higher- order Programs or How not to repeat yourself, Part II.