CS100J Lecture 13 Previous Lecture Java constructs

Slides:



Advertisements
Similar presentations
Array based Queues A circular implementation. Implementation Option 1 As with a array based stack, there are multiple ways that a queue can be implemented.
Advertisements

CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Implementing an ADT Having the JCFs is great – most of the significant data structures are already available But you still may need to implement your own.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
An Array-Based Implementation of the ADT List
Chapter VII: Arrays.
CSE 1342 Programming Concepts
Big-O notation Linked lists
Efficiency of in Binary Trees
Exceptions, Interfaces & Generics
Data Structures Lakshmish Ramaswamy.
Stacks.
October 30th – Priority QUeues
Linked List Yumei Huo Department of Computer Science
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Object Oriented Programming COP3330 / CGS5409
Lecture 2: Implementing ArrayIntList reading:
Programming in Java Lecture 11: ArrayList
Building Java Programs
Data Structures ADT List
Building Java Programs
Stacks.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CS 200 Objects and ArrayList
Lecture 2: Implementing ArrayIntList reading:
Arrays and Collections
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS100J Lecture 11 Previous Lecture This Lecture
Stacks Abstract Data Types (ADTs) Stacks
Data Structures Sorted Arrays
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
List Implementation via Arrays
CSC 143 Java Linked Lists.
Data Structures & Algorithms
CSE 143 Lecture 2 ArrayIntList, binary search
Collision Handling Collisions occur when different elements are mapped to the same cell.
Queues Definition of a Queue Examples of Queues
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
8 List ADTs List concepts. List applications.
TCSS 143, Autumn 2004 Lecture Notes
A type is a collection of values
CS100A Lect. 12, 8 Oct More About Arrays
Presentation transcript:

CS100J Lecture 13 Previous Lecture Java constructs for( initialized-declaration; … ) throw Programming Concepts Multiple class implementations Use of an array for maintaining a list of items Exception handling This Lecture Additional material on the use of arrays and alternative implementations of a class The use of auxiliary private methods Searching CS 100 Lecture 13

Motivating Example In many applications, we need to maintain a list of natural numbers, where each natural number may occur more than once. Such a list is called a “multi-set” of natural numbers. For example, the set of grades obtained on an exam is such a multi-set. Specification of class NatMultiset //Construct NatMultiset of items <= maxValue. public NatMultiset(int maxValue) // Insert item into multiset. public void insert(int item) // Is item in multiset? public boolean isElement(int item) // Delete item from multiset. public void delete(int item) // Multiplicity of item in multiset. public int multiplicity(int item) CS 100 Lecture 13

Frequencies of Grades, revisited int grade; // the grade being processed. // Let histogram be represented by a NatMultiset. NatMultiset hist = new NatMultiset(100000); /* “Process” grades until (but not including) a stopping signal of -1. */ grade = in.readInt(); while (grade != -1 ) { hist.insert(grade); } /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100000; grade++ ) System.out.println( grade + ” ” + hist.multiplicity(grade) ); CS 100 Lecture 13

Motivation for a Third Implementation As before: Grades are in the range 0..100,000. The are only 350 students in the class. However: It is very likely that the number of distinct grades will be quite small. Shortcomings of second implementation: Wasted time searching for all duplicate elements. Arbitrary upper bound on maxSize The application still wastes time iterating over 100,001 different grades, most of which have multiplicity 0. CS 100 Lecture 13

Third Implementation of natMultset Representation Invariant: The distinct multiset elements are list[0..currentSize-1], where currentSize <= maxSize, and the multiplicity of element list[i] is freq[i]. currentSize maxSize list freq currentSize maxSize . . . ? ? ? ? ? ? . . . ? ? ? ? ? public class NatMultiset { private int currentSize; private int maxSize; private int[] list; private int[] freq; . . . } CS 100 Lecture 13

Constructor NatMultiset /* Construct a NatMultiset for up to maxElements elements in the range 0 through maxValue. */ public NatMultiset( int maxValue ) { maxSize = _____________________; list = new int[maxSize+1]; freq = new int[maxSize]; currentSize = 0; } Running time: proportional to maxSize CS 100 Lecture 13

Auxiliary Method search /* Return the index of the leftmost occurrence of item in list, or currentSize if item isn't in list. */ private int search(int item) { list[currentSize] = item; int k = 0; while (list[k] != item) k++; return k; } Running time: Worst case proportional to currentSize CS 100 Lecture 13

Method insert Running time: Worst case proportional to currentSize /* Insert item into the multiset; throw RuntimeException if out of space. */ public void insert(int item) { int k = search(item); if (k != currentSize) /* item is already in multiset. */ freq[k]++; else if ( currentSize!=maxSize ){ /* First occurrence of item. */ list[currentSize] = item; freq[currentSize] = 1; currentSize++; } else throw new RuntimeException( "NatMultiset too large for bounds” ); Running time: Worst case proportional to currentSize CS 100 Lecture 13

Method isElement Running time: Worst case proportional to currentSize /* Return true if the multiset contains 1 or more instances of item. */ public boolean isElement(int item) { return search(item) != currentSize; } Running time: Worst case proportional to currentSize CS 100 Lecture 13

Method delete Running time: Worst case proportional to currentSize /* Delete one instance of item from the multiset, if there is one. */ public void delete(int item) { int k = search(item); if ( k != currentSize ) if (freq[k] > 1) freq[k]--; else { /* Last occurrence of item. */ currentSize--; list[k] = list[currentSize]; freq[k] = freq[currentSize]; } Running time: Worst case proportional to currentSize CS 100 Lecture 13

Method multiplicity /* Return the number of occurrences of item in the multiset. */ public int multiplicity(int item) { int k = search(item); if (k != currentSize) return freq[k]; else return 0; } Running time: Worst case proportional to currentSize CS 100 Lecture 13

Removing arbitrary size limitations Recall the arbitrary initialization of maxSize. Plan: instead of aborting execution when there is no more room, double the size of the arrays when needed. CS 100 Lecture 13

Doubling the Capacity /* Double maxSize and the lengths of both list and freq. */ private void stretch() { int newSize = 2 * maxSize; // Allocate new arrays. int[] new_list = new int[newSize+1]; int[] new_freq = new int[newSize]; // Copy old arrays to new arrays. for (int k = 0; k < currentSize; k++) new_list[k] = list[k]; new_freq[k] = freq[k]; } // Adopt new arrays. list = new_list; freq = new_freq; maxSize = newSize; CS 100 Lecture 13

Method insert, revisited /* Insert item into the multiset; throw RuntimeException if out of space. */ public void insert(int item) { int k = search(item); if (k != currentSize) /* item is already in multiset. */ freq[k]++; else { /* First occurrence of item. */ if (currentSize == maxSize) stretch(); list[currentSize] = item; freq[currentSize] = 1; currentSize++; } Running time: Worst case proportional to currentSize CS 100 Lecture 13

Major Shortcoming The interface of NatMultset forces us to iterate through entire range of value. /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100000; grade++ ) System.out.println( grade + ” ” + hist.multiplicity(grade) ); Need to redesign the interface to support more efficient retrieval of elements in the multiset. CS 100 Lecture 13