29
CS221 Week 6 - Friday

Week 6 - Friday. What did we talk about last time? Recursive running time Fast exponentiation Merge sort Introduced the Master theorem

Embed Size (px)

Citation preview

CS221Week 6 - Friday

Last time

What did we talk about last time? Recursive running time Fast exponentiation Merge sort Introduced the Master theorem

Questions?

Recursion

Assignment 3

Infix to Postfix Converter

Project 2

Master Theorem

Basic form that recursion must take

where

a is the number of recursive calls made b is how much the quantity of data is

divided by each recursive call f(n) is the non-recursive work done at

each step

Case 1

If for some constant , then

Case 2

If for some constant , then

Case 3

If for some constant , and if

for some constant and sufficiently large , then

Stupid Sort

Stupid Sort algorithm (recursive)

Base case: List has size less than 3 Recursive case:

Recursively sort the first 2/3 of the list Recursively sort the second 2/3 of the

list Recursively sort the first 2/3 of the list

again

Let's code up Stupid Sort!

But, how long does it take?

Stupid Sort

We need to know logb aa = 3b = 3/2 = 1.5 Because I’m a nice guy, I’ll tell you

that the log1.5 3 is about 2.7

Binary Search

We know that binary search takes O(log n) time

Can we use the Master Theorem to check that?

Student Lecture: Symbol Tables

Symbol Tables

Symbol tables

A symbol table goes by many names: Map Lookup table Dictionary

The idea is a table that has a two columns, a key and a value

You can store, lookup, and change the value based on the key

Example

A symbol table can be applied to almost anything:

The key doesn't have to be a String

But it should be unique

Key Value

Spiderman Climbing and webs

Wolverine Super healing

Professor X Telepathy

Human Torch

Flames and flying

Deadpool Super healing

Mr. Fantastic

Stretchiness

Key Value

121 Programming I

122 Programming II

221 Data Structures and Algorithms

322 Formal methods

361 Computer Graphics

363 Computer Security

Symbol table ADT

We can define a symbol table ADT with a few essential operations: put(Key key, Value value)▪ Put the key-value pair into the table

get(Key key):▪ Retrieve the value associated with key

delete(Key key)▪ Remove the value associated with key

contains(Key key)▪ See if the table contains a key

isEmpty() size()

It's also useful to be able to iterate over all keys

Ordered symbol tables

The idea of order in a symbol table is reasonable: You want to iterate over all the keys in

some natural order Ordering can give certain kinds of data

structures (like a binary search tree) a way to organize

Ordered symbol table ADT An ordered symbol table ADT adds the following

operations to a regular symbol table ADT: Key min()▪ Get the smallest key

Key max()▪ Get the biggest key

void deleteMin()▪ Remove the smallest key

void deleteMax()▪ Remove the largest key

Other operations might be useful, like finding keys closest in value to a given key or counting the number of keys in a range between two keys

Implementations

Like other ADTs, a symbol table can be implemented in a number of different ways: Linked list Sorted array Binary search tree Balanced binary search tree Hash table

Note that a hash table cannot be used to implement an ordered symbol table And it's inefficient to use a linked list for ordered

Sorted array

We know how to make a sorted array symbol table

A search is Θ(log n) time, which is great!

The trouble is that doing an insert takes Θ(n) time, because we have to move everything in the array around

A sorted array is a reasonable model for a symbol table where you don't have to add or remove items

Trees

Trees will allow us to make a sorted symbol table with the following miraculous properties: Θ(log n) get Θ(log n) put Θ(log n) delete Θ(n) traversal (iterating over everything)

Unfortunately, only balanced binary search trees will give us this property

We'll start next week with binary search trees and then built up to balanced ones

Upcoming

Next time…

Binary trees Read section 3.2

Reminders

Finish Assignment 3 Due tonight!

Keep working on Project 2 Due next Friday

No class on Monday!