LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Java Programming Abstract classes and Interfaces.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
© 2004 Goodrich, Tamassia Hash Tables1  
Problem of the Day  What do you get when you cross a mountain climber and a grape?
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSC 212 – Data Structures Lecture 29: Dictionary ADT.
Hash Tables1 Part E Hash Tables  
1 Trees III: Binary Search Trees. 2 A forest full of trees The generic toolkit of functions we have seen thus far can be applied to many types of data.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
Chapter 19 Java Data Structures
Problem Of The Day  Decipher the following phrase: STANDS 0 _
CSC 213 – Large Scale Programming. Today’s Goal  Consider what will be important when searching  Why search in first place? What is its purpose?  What.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Problem of the Day  What do you get when you cross a mountain climber and a grape?
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.
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.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Hash Tables1   © 2010 Goodrich, Tamassia.
Chapter 18 Java Collections Framework
LECTURE 36: DICTIONARY CSC 212 – Data Structures.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Built-in Data Structures in Python An Introduction.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
CSC 212 – Data Structures Lecture 37: Course Review.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Comparison-Based Sorting & Analysis Smt Genap
© 2004 Goodrich, Tamassia Hash Tables1  
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
LECTURE 35: COLLISIONS CSC 212 – Data Structures.
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
CSC 212 – Data Structures Lecture 26: Hash Tables.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
CSC 212 Trees & Recursion. Announcements Midterm grades were generally good  Wide distributions of scores, however  Will hand back at end of class 
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
SSE693 C# and.NET Dr. MacNeil Summer 2005 Jay Clary / Lance Hilliard Team Project 3.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Week 9 - Friday.  What did we talk about last time?  Collisions  Open addressing ▪ Linear probing ▪ Quadratic probing ▪ Double hashing  Chaining.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CSC 212 – Data Structures Lecture 31: Last Word On Dictionaries.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Problem of the Day  Why are manhole covers round?
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
CSC 213 – Large Scale Programming. Today’s Goal  Review when, where, & why we use Map s  Why Sequence -based approach causes problems  How hash can.
Arrays in PHP are quite versatile
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CH 9 : Maps And Dictionary
Week 6 - Monday CS221.
Introduction to Java Collection
Presentation transcript:

LECTURE 38: ORDERED DICTIONARY CSC 212 – Data Structures

MAP VS. DICTIONARY

 Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table Map ADTDictionary ADT MAP VS. DICTIONARY

 Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  key in at most 1 Entry  Collection of Entry s  key – searched for  value – cared about  Implemented with:  List w/ Entry s in order they were added  List w/ Entry s in increasing order of keys  Hash table  Entry s can share key Map ADTDictionary ADT MAP VS. DICTIONARY

Map ADTDictionary ADT MAP VS. DICTIONARY

Comparing Data Items  Keeping Entry s ordered means comparing keys  Cannot rely upon equals() for all comparisons  Need to find smaller, bigger, & even-steven-equals  Use, == when keys limited to numeric type  String also has simple method: compareTo()  Do not want to rewrite for each key type  But this requires a general way to compare keys

Comparable Interface  In Java as a standard from java.lang  Defines single method used for comparison  compareTo(E obj) compares instance with obj  Returns int which is either negative, zero, positive

class Team implements Comparable { private int wins, losses, lossesInOTSO; private int points() { return (wins * 2) + (lossesInOTSO); } /** Order Team instances in standings */ public int compareTo(Team o) { int myPoints = points(); int oPoints = o.points(); if (myPoints == oPoints) { return 0; } else if (myPoints oPoints) { return 1; } } COMPARABLE Example

class Team implements Comparable { private int wins, losses, lossesInOTSO; private int points() { return (wins * 2) + (lossesInOTSO); } /** Order Team instances in standings */ public int compareTo(Team o) { int myPoints = points(); int oPoints = o.points(); return (myPoints - oPoints); } Simpler COMPARABLE

 Entry s maintained in increasing order of key  Use array-based List for efficient searching  Simplify process: keys must be Comparable Ordered Dictionary

 Subinterface of Dictionary  Classes will define all methods in Dictionary  Use anywhere that Dictionary could be used  Adds efficiency of O(log n) search times  Interface also defines the following methods:  Entry first();  Entry last();  Iterator > successors(K k);  Iterator > predecessors(K k); ORDEREDDICTIONARY ADT

 Simplify life by requiring keys by Comparable  Using generic types we can do this  Use a special bounded generic type for key class ODict,V> implements OrderedDictionary { ODict happy; ODict glad; ODict sad;  Writing ORDEREDDICTIONARY

class ODict,V> implements... { /** Array-based list we use to store the Entry s */ private IndexList > table; public Entry first() throws EmptyDictionaryException { // Check if we need to throw an Exception if (table.isEmpty()) { throw new EmptyDictionaryException(“No Entry”); } else { // Return the Entry with the smallest key return table.get(0); } } Writing ORDEREDDICTIONARY

public Iterator > successors(K k) { IndexList > retVal = // instantiation goes here // Loop from Entry with largest key back to Entry with smallest key for (int i = table.size()–1; i >= 0; i--) { Entry ent = table.get(i); // Stop once we find an Entry with a smaller or equal key if (k.compareTo(ent) >= 0) { break; } retVal.add(0, ent); // Keep Entry s in order } // Return the Iterator from the Iterable return retVal.iterator(); } More ORDEREDDICTIONARY

 Finish week #13 assignment  Due at usual time, whatever that may be  Work on programming project #4 (due 12/1)  See you at lab tomorrow and Happy Thanksgiving! Before Next Lecture…