Chapter 16.  Data structures that take control of organizing elements  Elements not in fixed positions  Advantage – better performance Adding Removing.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Processing Data in Collections Chapter Object Wrappers Collections can only hold objects. Primitive types ( int, double, float, etc.) are not objects.
An Introduction to Hashing. By: Sara Kennedy Presented: November 1, 2002.
Hashing as a Dictionary Implementation
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Copyright © 2013 by John Wiley & Sons. All rights reserved. THE JAVA COLLECTIONS FRAMEWORK CHAPTER Slides by Donald W. Smith TechNeTrain.com 15.
Maps, Dictionaries, Hashtables
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Sets and Maps (and Hashing)
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Sets and Maps Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 15 – The Java Collections Framework.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 10.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
Hashing Hashing is another method for sorting and searching data.
Hashing as a Dictionary Implementation Chapter 19.
CHAPTER 20 Advanced Data Structures. CHAPTER GOALS To learn about set and map data types To understand the implementation of hash tables To be able to.
1 Introduction to Hashing - Hash Functions Sections 5.1, 5.2, and 5.6.
CS201: Data Structures and Discrete Mathematics I Hash Table.
“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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
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.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 the hash table. hash table A hash table consists of two major components …
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 16 – Advanced Data Structures.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Hashing By Emily Nelson. The Official Definition Using a hash function to turn some kind of data in relatively small integers or Strings The “hash code”
Advanced Data Structures Advanced Programming ICOM 4015 Lecture 18 Reading: Java Concepts Chapter 21.
Building Java Programs Generics, hashing reading: 18.1.
Sets and Maps Chapter 9.
Slides by Donald W. Smith
Searching, Maps,Tries (hashing)
Slides by Donald W. Smith
Slides by Donald W. Smith
Ch 16: Data Structures - Set and Map
Chapter 16 – Advanced Data Structures
Slides by Steve Armstrong LeTourneau University Longview, TX
Advanced Associative Structures
CSE 373: Data Structures and Algorithms
Sets, Maps and Hash Tables
CSE 373 Data Structures and Algorithms
CSE 1020: The Collection Framework
Sets and Maps Chapter 9.
The Array is Not Enough.
Presentation transcript:

Chapter 16

 Data structures that take control of organizing elements  Elements not in fixed positions  Advantage – better performance Adding Removing Finding

 Unordered collection of distinct elements  Fundamental operations Adding an element Removing an element Containment testing (is something there) Listing – arbitrary order  Duplicates Aren’t permitted Attempt to add – ignored Attempt to remove non existent data - ignored

 Hash tables  Trees  Uses set interface > Hash SetTree Set

 Create a Hash Set of Strings Set names = new HashSet ();  Adding and deleting names.add(“Romeo”); names.remove(“Juliet”);  Test to see if element is in set if (names.contains(“Juliet”);

Iterator iter=names.iterator(); while (iter.hasNext()) { String name = iter.next(); do something with the string ……… }

for (String name: names) { do something with names { Iterating through elements Does not visit in order inserted Set implementation rearranges for quick location Cannot add in iterator position Create a print method Should be usable by has or tree

 Data type  Associates keys and values  A function from one set(key) to another set(value)  Every key has unique value  A value may have multiple keys  Two kinds of implementation Hash Map Tree Map Both implement Map Interface

Map favoriteColors = new HashMap<String, Color?(); Romeo Adam Eve Juliet Keys Values Green Red Pink

 Add an association favorite.put(“Juliet”,Color.PINK);  Change a value favorite.put(“Juliet”,Color.RED);  Return the value Color julietFavoriteColor = favorite.get(“Juliet”); If you ask for a value with no key return a null  Remove key and value favorite.remove(“Juliet”);

 Find all keys and values in map Set keySet = m.keySet(); For(String key: KeySet) { Color value=m.get(key); System.out.println(Key + “->” + value); }

 Find elements in a data structure quickly  No linear search through all elements  Can implement sets and maps  Utilizes hash codes Function that computes an integer value from an object Integer value = has code Object class has a hashCode method  Int h = x.hashCode();  Collision – 2 objects with same hash code  Good has functions minimizes collision  Hash Code – index into hash table

 Simplest form Array index to hash table In array insert each location of its hash code If anything already in array – object is present

Eve Jim Joe [70068] [74478] [74656]

Problem with this idea  Not possible to allocate an array that is large enough to hold all possible integer index positions  Must reduce hash function to fit the array size int h = x.hashCode(); if (h<0) h = -h; position = h.%buckets.length;  This approach increases likely hood of collisions

 Create a hash table that is implemented as an array of buckets.  Buckets are a sequence of nodes that hold elements with the same hash code. [65] [66] [67] [68] [69] [70] [71] [72] Harry Nina Susannah Sue Larry Adam Tony KatherineJuliet

 Compute hash code Reduce to table size using modulo Produces index into hash table int h = x.hashCode(); if (h<0) h = -h; position = h.%buckets.length;  Iterate through the elements of the bucket at position h  Check for match

 Depends on number of collisions or buckets  Few buckets – quick O(1)  Many buckets – much slower

 From String int h = 0; for (int i=0; I < s.length(); i++) h = h+s.charAt(i);  What is the problem with this? Same hash code for permutations of same letters (eat tea)

final int HASH_MULTIPLIER = 31; Int h=0; for(int i=0;i<s.length();i++) h = HASH_MULTIPLIER*h+s.charAt(i); eat 31*(31*’e’ +’a’) + ‘t’ = *(31*101+97) tea 31*(31*’t’ +’e’) + ‘a’ = *(31* ) + 97

 Example Coin class public class Coin { final int HASH_MULTIPLIER = 29; public int hashCode() { int h1 = name.hashCode(); int h2 – new Double(value.hashCode(); int h = HASH_MULTIPLIER*h1+h2; return h; }  In a hash map only the keys are hashed.