Week 8 - Friday CS221.

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Hashing Hashing is another method for sorting and searching data.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Maps Nick Mouriski.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Collections Dwight Deugo Nesa Matic
Sections 10.5 – 10.6 Hashing.
Chapter 27 Hashing Jung Soo (Sue) Lim Cal State LA.
Binary Search Trees Implementing Balancing Operations Hash Tables
CSE 214 – Computer Science II Maps
CSCI 210 Data Structures and Algorithms
Programming Abstractions
Efficiency of in Binary Trees
Week 8 - Wednesday CS221.
Week 6 - Wednesday CS221.
EEE2108: Programming for Engineers Chapter 8. Hashing
Data Structures TreeMap HashMap.
Week 15 – Monday CS221.
Searching.
structures and their relationships." - Linus Torvalds
TreeSet TreeMap HashMap
Chapter 28 Hashing.
structures and their relationships." - Linus Torvalds
Introduction to Collections
CSE 373: Data Structures and Algorithms
Introduction to Collections
Part of the Collections Framework
Chapter 21 Hashing: Implementing Dictionaries and Sets
Maps.
Searching Tables Table: sequence of (key,information) pairs
Maps "He's off the map!" -Stan (Mark Ruffalo) Eternal Sunshine of the Spotless Mind.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Implementing Hash and AVL
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Augmented Data Structures and the Test
"He's off the map!" - Eternal Sunshine of the Spotless Mind
CS2110: Software Development Methods
Introduction to Collections
L5. Necessary Java Programming Techniques
Hash Tables Computer Science and Engineering
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Introduction to Collections
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CSE 373 Separate chaining; hash codes; hash maps
CSC 143 Binary Search Trees.
Chapter 8 The Map ADT.
slides created by Marty Stepp
CS Fall 2012, Lab 11 Haohan Zhu.
Hashing in java.util
Collision Handling Collisions occur when different elements are mapped to the same cell.
Hashing based on slides by Marty Stepp
structures and their relationships." - Linus Torvalds
Week 6 - Monday CS221.
Presentation transcript:

Week 8 - Friday CS221

Last time What did we talk about last time? Collisions Open addressing Linear probing Quadratic probing Double hashing Chaining

Questions?

Project 3

Assignment 4

Hash Table Implementation

Chaining hash table public class HashTable { private int size = 0; private int power = 10; private Node[] table = new Node[1 << power]; private static class Node { public int key; public Object value; public Node next; } …

Get (chaining) Return the object with the given key If none found, return null public Object get(int key)

Put (chaining) public boolean put(int key, Object value) If the load factor is above 0.75, double the capacity of the hash table and increase power by 1 Then, try to add the given key and value If the key already exists, update its value and return false Otherwise add the new key and value and return true public boolean put(int key, Object value)

Maps in the Java Collections Framework

Maps Recall that the symbol table ADT is sometimes called a map Both Java and C++ use the name map for the symbol table classes in their standard libraries Python calls it a dictionary (and supports it in the language, not just in libraries)

Concrete example We've been working so long on trees and hash tables, we might have forgotten what a symbol table is for: Anything you can imagine storing as data with two columns, a key and a value In this way you can look up the weight of anyone However, the keys must be unique Ahmad and Carmen might weigh the same, but Ahmad cannot weight two different values There are multimaps in which a single key can be mapped to multiple values But they are used much less often Name (Key) Weight (Value) Ahmad 210 Bai Li 145 Carmen 105 Deepak 175 Erica 205

JCF Map The Java interface for maps is, unsurprisingly, Map<K,V> K is the type of the key V is the type of the value Yes, it's a container with two generic types Any Java class that implements this interface can do the important things that you need for a map get(Object key) containsKey(Object key) put(K key, V value)

JCF implementation Because the Java gods love us, they provided two main implementations of the Map interface HashMap<K,V> Hash table implementation To be useful, type K must have a meaningful hashCode() method TreeMap<K,V> Balanced binary search tree implementation To work, type K must implement the compareTo() method Or you can supply a comparator when you create the TreeMap

Code example Let's see some code to keep track of some people's favorite numbers Map<String,Integer> favorites = new TreeMap<String,Integer>(); favorites("Barry", 42); //autoboxes int value favorites("Ting", 21); favorites("Joe", 13); favorites("Tom", 7); if( favorites.containsKey("Barry") ) System.out.println(favorites.get("Barry"));

JCF Set Java also provides an interface for sets A set is like a map without values (only keys) All we care about is storing an unordered collection of things The Java interface for sets is Set<E> E is the type of objects being stored Any Java class that implements this interface can do the important things that you need for a set add(E element) contains(Object object)

Time Trials

Time trials Let's code up an (unbalanced) BST and a hash table and see which one is faster

Upcoming

Next time… Introduction to graphs

Reminders Keep working on Project 3 Read 4.1