CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009.

Slides:



Advertisements
Similar presentations
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Advertisements

CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
Stack & Queues COP 3502.
Queues and Stacks.  Can receive multiple requests from multiple sources ◦ How do we services these requests?  First come, first serve processing 
Hashing as a Dictionary Implementation
Appendix I Hashing. Chapter Scope Hashing, conceptually Using hashes to solve problems Hash implementations Java Foundations, 3rd Edition, Lewis/DePasquale/Chase21.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Maps, Dictionaries, Hashtables
Hashing. 2 Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
Hashing. Searching Consider the problem of searching an array for a given value –If the array is not sorted, the search requires O(n) time If the value.
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.
1 CSE 326: Data Structures Hash Tables Autumn 2007 Lecture 14.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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:
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Information and Computer Sciences University of Hawaii, Manoa
Big Java Chapter 16.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Comp 335 File Structures Hashing.
Data structures Abstract data types Java classes for Data structures and ADTs.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
Understanding Data Types and Collections Lesson 2.
Hashing as a Dictionary Implementation Chapter 19.
CS201: Data Structures and Discrete Mathematics I Hash Table.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ Stack ◦ Queue & Priority Queue 2.
“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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
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.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Nov 22, 2010IAT 2651 Java Collections. Nov 22, 2010IAT 2652 Data Structures  With a collection of data, we often want to do many things –Organize –Iterate.
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. Searching Consider the problem of searching an array for a given value If the array is not sorted, the search requires O(n) time If the value.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Sets and Maps Chapter 9.
Review Array Array Elements Accessing array elements
Slides by Steve Armstrong LeTourneau University Longview, TX
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Lesson 6. Types Equality and Identity. Collections.
CS202 - Fundamental Structures of Computer Science II
Sets and Maps Chapter 9.
structures and their relationships." - Linus Torvalds
Presentation transcript:

CSE 501N Fall ‘09 11: Data Structures: Stacks, Queues, and Maps Nick Leidenfrost October 6, 2009

Lecture Overview More Data Structures! Stacks Queues Maps  Hashtables / Hashmaps Quiz #4

Stack: LIFO Allows insertion and removal of elements only at one end  Traditionally called the top of the stack New items are added to the top of the stack Items are removed at the top of the stack Called last in, first out or LIFO order Traditionally, addition and removal operations are called push and pop Think of a stack of books

A Stack of Books

Queue: FIFO Add items to one end of the queue (the tail) Remove items from the other end of the queue (the head) Queues store items in a first in, first out or FIFO fashion Items are removed in the same order in which they have been added Think of people lining up  People join the tail of the queue and wait until they have reached the head of the queue Traditionally, addition and removal operations are called enqueue and dequeue

Stacks and Queues: Uses in Computer Science Queue  Anything where resources are shared, and an attempt is made to give fair treatment  User input: Mouse clicks and keystrokes  Queue of print jobs Stack  Run-time stack that a processor or virtual machine keeps to organize the variables of nested method calls

Stacks and Queues: Implementation Stacks and Queues are commonly implemented using a Linked List  Aggregation / Delegation We can use a list internally and limit the public interface  Which methods can be publicly invoked  The Stack or Queue class delegates the actual storage to a list, and then defines how the list is used [ Example on Board ]

Map A map is a data structure which creates a relationship between two objects  A key and a value  The key is mapped to the value Typically, the key is an easily reproducible object  E.g. a String or an Integer The value is what the map is actually intended to store.

Maps: interface put / insert(key, value) : insert the key / value in the map if they key is not already in the map.  If the key is in the map, replace the associated value get(key) : retrieve the value associated with the key remove(key) : remove the key and value from the map

Maps: implementation Linked List [ Example on Board ] Binary Tree [ Example on Board ] Hash Table

Hashtables An efficient way to implement the map data structure: Can usually perform operations in constant time public void put (Object key, Object value); public Object get (Object key); public Object remove (Object key);

Hashtables: Implementation Internally, hashtables use an array Each element of the internal array in a hashtable is referred to as a bucket Where an element is stored in the array is determined by the hash function of the hash table Object[] array = new Object[11];

Hashtables: Implementation The hash function converts the supplied key to an integer “But Nick, our key is an Object! How the heck do we convert an object to an integer!?!?” public void put (Object key, Object value) { int hash = this.hashFunction(key);... }

Hashtables: Implementation Java helps us out with a predefined method: public void put (Object key, Object value) { int hash = key.hashCode();... }

Hashtables: Implementation We can override hashCode in our objects if we need to reflect equality, as with the.equals method String overrides hashCode so that all equivalent Strings are hashed identically String foo = “foo”; String foo2 = “foo”; // foo.hashCode() == foo2.hashCode()

Hashtables: Implementation The result of the hash function must next be sized to provide a valid index into the array  0 – array.length -1  Do any arithmetic operators come to mind? public void put (Object key, Object value) { int hash = key.hashCode(); int index = hash % this.array.length; // insert into this.array[index] }

Hashtables: Implementation Because of the Modulus operator is used with most simple hash functions, it is best to size your array with a prime number  Why do you think this is? It helps prevent collisions 2 or more values vying for the same bucket  Java’s java.util.Hashtable class has an initial array size of 11

Hashtables: Handling Collisions If all keys into a hashtable are known before time, a perfect hash function can be used to avoid collisions  Most often, this is not the case We won’t know all keys prior to execution  Two prevailing methods for handling collisions when they occur Open Addressing Chaining

Hashtables: Handling Collisions Open Addressing  Check next bucket to see if it is occupied If so, check the next bucket until we find an open one  -or- Apply a secondary hash function to find another bucket Pros: We only have to use our array for internal storage Cons: The hashtable fills up much more quickly

Hashtables: Handling Collisions Chaining  Simply have each bucket store a list  When a collision occurs, add the item to the bucket’s list DoublyLinkedList[] array = new DoublyLinkedList[11];

Hashtables: Chaining Pros:  Table can hold more elements before suffering bad performance Cons:  Table has a bigger memory footprint & requires allocation on each insert

Hashtables: Implementation Initialization public void put (Object key, Object value) public Object get (Object key) public Object remove (Object key)

Hashtables: Rehashing When a hashtable gets too full, its internal array must be resized in order to regain more efficient performance All elements must be rehashed with new array size in mind In Lab 4, you will not need to worry about rehashing

Conclusion Questions? Midterm in class next Tuesday (13 th )  Practice problems on the web Lab 3 Due tonight at Midnight Lab 4 Assigned now  Due Oct. 15  Get started early! I will be in lab now