ArraySet Methods and ArrayIterator

Slides:



Advertisements
Similar presentations
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.
Advertisements

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
COMP 103 Linked Stack and Linked Queue.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
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.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
Iterators CS 367 – Introduction to Data Structures.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
An Array-Based Implementation of the ADT List
Linked Data Structures
Iterators.
The List ADT.
EECE 310: Software Engineering
Storage Strategies: Dynamic Linking
Recap: Solution of Last Lecture Activity
ADT’s, Collections/Generics and Iterators
Week 3 - Friday CS221.
CSE 373: Data Structures and Algorithms
Implementing ArrayList Part 1
Trees Tree nomenclature Implementation strategies Traversals
Top Ten Words that Almost Rhyme with “Peas”
Iteration Abstraction
Java Software Structures: John Lewis & Joseph Chase
TCSS 342, Winter 2006 Lecture Notes
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
CIT Nov-18.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Adapted from Pearson Education, Inc.
Iteration Abstraction
Example: LinkedSet<T>
Recitation 7 October 7, 2011.
List Implementation via Arrays
Stacks CS-240 Dick Steflik.
Introduction to Stacks
Introduction to Stacks
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Part of the Collections Framework
CHAPTER 3: Collections—Stacks
Presentation transcript:

ArraySet Methods and ArrayIterator Homework review ArraySet and ArrayIterator Methods with Big-O analysis Reading: L&C 3rd ed:15.3, Pages 40-41 2nd ed: 3.6. (Book has some mistakes.) 1

Problem size parameter In analyzing the time complexity of the operations of the ArraySet class we need to choose the parameters which specifies the size of our problem. We choose the size of the set: n to be the parameter. Then, we analyze the time complexity of ArraySet with respect to changes in the size of the set. In Big-O analysis we always consider the worst case.

ArrayIterator Methods Default constructor should NOT be used May want to include this code to prevent any calls to the default constructor: private ArrayIterator() { } Overloaded Constructor: public ArrayIterator(T[] collection, int count) { items = collection; // NOTE: creates an alias // not a new array this.count = count; current = 0; } 3

ArrayIterator Methods hasNext – O(1) public boolean hasNext() { return current < count; } next – O(1) public T next() if (!hasNext()) throw new NoSuchElementException(); return items[current++]; 4

ArrayIterator Methods remove – O(1) We don’t need to implement real code for the remove method, but there is no return value that we can use to indicate that it is not implemented If we don’t implement it, we indicate that the code is not implemented by throwing an exception public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } 5

ArrayIterator Methods If we do implement the remove method, notice that we don’t specify the element that is to be removed and we do not return a reference to the element being removed It is assumed that the calling code has been iterating on condition hasNext() and calling next() and already has a reference The last element returned by next() is the element that will be removed 6

ArrayIterator Method Analysis All three ArrayIterator methods are O(1) However, they are usually called inside an external while loop or “for-each” loop Hence, the process of “iterating” through a collection using an Iterator is O(n) 7

ArraySet Methods size - O(1) isEmpty – O(1) public int size() { return count; } isEmpty – O(1) public boolean isEmpty() return count == 0;

ArraySet Methods expandCapacity – O(n) private void expandCapacity() { T[] larger = (T[]) new Object[2 * contents.length]; for (int i = 0; i < contents.length; i++) larger[i] = contents[i]; contents = larger; // original array // becomes garbage }

ArraySet Methods contains – O(n): The code is a bit different than the one used in the book (both O(n)). public boolean contains(T target) { for (T element : contents) if (target.equals(element)) return true; return false; }

ArraySet Methods add – O(n) public void add (T element) { if (!contains(element)) if (size() == contents.length) expandCapacity(); contents[count] = element; count++; }

ArraySet Methods addAll – O( (2n + m).m) (Note: We also can code addAll using a for- each loop because we added extends Iterable to SetADT interface definition) public void addAll(setADT<T> set) { for(T element : set) add(element); }

ArraySet Methods remove – O(n) public T remove(T target) throws EmptySetException, NoSuchElementException { if (isEmpty()) throw new EmptySetException(); for (int i = 0; i < count; i++) { if (contents[i].equals(target)) { T result = contents[i]; contents[i] = contents[--count]; contents[count] = null; return result; } throw new NoSuchElementException();

ArraySet Methods removeRandom – O(1) public T removeRandom() throws EmptySetException { if (isEmpty()) throw new EmptySetException(); int choice = rand.nextInt(count); T result = contents[choice]; contents[choice] = contents[count-1]; contents[count-1] = null; count--; return result; }

ArraySet Methods removeRandom (Bad Alternative?) We may want to avoid some duplicate code in the removeRandom method and make it shorter. Throws any EmptySetException that occurs in remove public T removeRandom() throws EmptySetException { return remove(contents[rand.nextInt(count)]); } The above alternative algorithm is O(n) instead of O(1)!

ArraySet Methods union: this set size: n parameter set size m public SetADT<T> union (SetADT<T> set) { ArraySet<T> both = new ArraySet<T>(); both.addAll(this);//O((0+n).n)=O(n.n) both.addAll(set); //O((2n+m).m) return both; } O(n.n) + O((2n+m).m) = O(n.n + 2m.n + m.m) 16

ArraySet Methods toString – O(n) public String toString() { String result = “”; for (T obj : contents) { if (obj == null) // first null is at count return result; result += obj + “\n”; } return result; // exactly full – no nulls 17

ArraySet Methods iterator – O(1) public Iterator<T> iterator() { return new ArrayIterator<T>(contents, count); } 18