Podcast Ch21d Title: Hash Class Iterators

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
Double-Linked Lists and Circular Lists
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
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:
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 18 Binary.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Hashing as a Dictionary Implementation Chapter 19.
RED-BLACK TREE SEARCH THE FOLLOWING METHOD IS IN TreeMap.java:
“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.
COMP 121 Week 11: Linked Lists.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 7 Queues Introduction Queue applications Implementations.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Podcast Ch24c Title: Breadth First Search
Podcast Ch23e Title: Implementing Huffman Compression
Chapter 4 Linked Structures.
Podcast Ch17b Title: Iterative Tree Traversal
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Podcast Ch26a Title: Representing Graphs
Lecture 15: More Iterators
Podcast Ch17d Title: Drawing a Binary Tree
Podcast Ch17a Title: Expression Trees
Efficiency add remove find unsorted array O(1) O(n) sorted array
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Java Software Structures: John Lewis & Joseph Chase
Linked Lists, Stacks and Queues Textbook Sections
Data Structures for Java William H. Ford William R. Topp
A Data Structure Bestiary
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
A Data Structure Bestiary
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
Podcast Ch24b Title: Graphs and Digraphs
Podcast Ch22c Title: Deleting from a Heap
Podcast Ch23b Title: BitArray Implementation
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch23f Title: Serialization
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
ITI Introduction to Computing II Lab-12
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Podcast Ch24b Title: Strongly Connected Components
8 List ADTs List concepts. List applications.
Podcast Ch21b Title: Collision Resolution
Podcast Ch23a Title: Bit Arrays
Podcast Ch23c Title: Binary Files
Presentation transcript:

Podcast Ch21d Title: Hash Class Iterators Description: Overview; IteratorImpl; next() method; remove() method Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Hash Class Iterators Search the hash table for the first nonempty bucket in the array of linked lists. Once the bucket is located, the iterator traverses all of the elements in the corresponding linked list and then continues the process by looking for the next nonempty bucket. The iterator reaches the end of the table when it reaches the end of the list for the last nonempty bucket.

Hash Class Iterators (continued) Iterator objects are instances of the inner class IteratorImpl whose variables are: Integer index that identifies the current bucket (table[index]) scanned by the iterator. The Entry reference next pointing to the current node in the current bucket. The variable lastReturned that references the last value returned by next(). The iterator variable expectedModCount used in conjunction with the collection variable modCount.

Hash Class Iterators (continued) // inner class that implements hash table iterators private class IteratorImpl implements Iterator<T> { // next entry to return Entry<T> next; // to check iterator consistency int expectedModCount; // index of current bucket int index; // reference to the last value returned by next() T lastReturned; . . . }

Hash Class Iterators (cont) The elements enter the collection in the order (19, 32, 11, 27) using the identify hash function. The iterator visits the elements in the order (11, 32, 27, 19).

Hash Iterator Constructor A loop iterates up the list of buckets until it locates the first nonempty bucket. The loop variable i becomes the initial value for index and table[i] references the front of the list. This is the initial value for next.

Hash Iterator Constructor (concluded) IteratorImpl() { int i = 0; Entry<T> n = null; // the expected modCount starts at modCount expectedModCount = modCount; // find the first nonempty bucket if (hashTableSize != 0) while (i < table.length && ((n = table[i]) == null)) i++; next = n; index = i; lastReturned = null; }

Hash Iterator next() The method next() first determines that the operation is valid by checking that modCount and expectedModCount are equal and that we are not at the end of the hash table. If the iterator is in a consistent state, next() saves entry.value in lastReturned and uses a loop index i and entry to perform the iterator scan for the subsequent element in the hash table. The return value is lastReturned.

Hash Iterator next() (continued) public T next() { // check for iterator consistency if (modCount != expectedModCount) throw new ConcurrentModificationException(); // we will return the value in Entry object next Entry<T> entry = next; // if entry is null, we are at the end if (entry == null) throw new NoSuchElementException(); // capture the value we will return lastReturned = entry.value; // move to the next entry in the current // linked list Entry<T> n = entry.next; // record the current bucket index int i = index;

Hash Iterator next() (concluded) if (n == null) { // we are at the end of a bucket; search for // the next nonempty bucket i++; while (i < table.length && (n = table[i]) == null) } index = i; next = n; return lastReturned;

Hash Iterator remove() The remove() method first determines that the operation is valid by checking that lastReturned is not null and that modCount and expectedModCount are equal. If all is well, the iterator remove() method calls the Hash class remove() method with lastReturned as the argument. By assigning to expectedModCount the current value of modCount, the iterator remains consistent.

Hash Iterator remove() (concluded) public void remove() { // check for a missing call to next() or // previous() if (lastReturned == null) throw new IllegalStateException( "Iterator call to next() " + "required before calling remove()"); if (modCount != expectedModCount) throw new ConcurrentModificationException(); // remove lastReturned by calling remove() // in Hash; this call will increment modCount Hash.this.remove(lastReturned); expectedModCount = modCount; lastReturned = null; }