Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6 - Monday CS221.

Similar presentations


Presentation on theme: "Week 6 - Monday CS221."— Presentation transcript:

1 Week 6 - Monday CS221

2 Last time What did we talk about last time? More recursion examples
Recursive running time

3 Questions?

4 Assignment 3 Recursion

5 Project 2 Infix to Postfix Converter

6 Wittman's Guide to Quick Debugging
a = b; a and b have to have the same type Exception: b is a subtype of a == is a comparison operator that produces a boolean, = is a left-pointing arrow if statements always have parentheses No semicolons after if or while headers (and rarely after for headers) The following is always wrong: x = y; x = z; Know your String methods A non-void method has to return something The new keyword is always followed by a type and either parentheses (possibly with arguments) or square brackets with integers inside Local variables cannot be declared private, public, or protected Don't use the name of the type for method arguments

7 Know your syntax By now, you should know your syntax inside and out
If you don't know how something in Java works, find out! Syntax is your basic tool for everything Read about some syntax once? Doesn't help! Write some code to see it in practice! CS121 CS122 CS221 Syntax Problem Solving Design

8 Merge Sort

9 Merge Sort algorithm (recursive)
Beautiful divide and conquer Base case: List has size 1 Recursive case: Divide your list in half Recursively merge sort each half Merge the two halves back together in sorted order

10 Let’s code that up… Great. Now, how long does it take?

11 Student Lecture: Symbol Tables

12 Symbol Tables

13 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

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

15 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

16 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

17 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

18 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

19 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

20 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 this week with binary search trees and then built up to balanced ones

21 Upcoming

22 Next time… Binary search trees BST implementation
Keep reading Chapter 3.1

23 Reminders Finish Assignment 3 Keep working on Project 2
Due Wednesday, October 4 Keep working on Project 2 Due Friday, October 13


Download ppt "Week 6 - Monday CS221."

Similar presentations


Ads by Google