CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Java Review Interface, Casting, Generics, Iterator.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Iterators CS 367 – Introduction to Data Structures.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
(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.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Intro To Classes Review
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Dynamic Data Structures and Generics
Lists and the Collection Interface
Assessment – Java Basics: Part 1
CSE 12 – Basic Data Structures
Presentation transcript:

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics Algorithm analysis 1. Iterators 2. Java nested/inner classes 3. (If we have time) finish up a few thoughts on benchmarking of algorithms, from last time 2

Iterator interface

Reading quiz! Iterator.remove is not the only safe way to modify a collection during iteration. A. TRUE B. FALSE

Reading quiz! If we implement the Iterator interface as a public static inner class, then it can not be instantiated without an outer class instance A. TRUE B. FALSE

Reading quiz! If we implement the Iterator interface as a public static inner nested class, then it can not be instantiated without an outer class instance A. TRUE B. FALSE

Suppose we have a reference (current) to the node containing the item to be removed. What additional information do we need to successfully remove the node? A) Nothing additional. B) A reference to the node immediately prior to the deleted node. C) A reference to the node immediately after the node to be deleted. D) Both B and C. NULL head current (we want to remove this one)

 Here we keep:  A next pointer to what should be returned by the next call to next  A last pointer to what should be deleted by the next call to remove (we remove the most recent element returned by a call to next )  A pred pointer to assist with implementing remove NULL head pred last (we want to remove this one) next iter:

We initialize iterator as follows: this.pred = null; //predecessor of “last” node this.last = null; //last node returned this.next = head; //node to return next NULL head list: pred last (we want to remove this one) next iter:

What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); head list: A.pred is null, last is null, next points to A B.pred is null, last points to A, next points to B C.pred points to A, last points to B, next points to C D.Other/none/more pred last next iter: next data X next data X next data X next data X next data X X

What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); NULL head list: A.pred points to A, last points to B, next points to C B.pred points to B, last points to C, next points to D C.pred points to C, last points to D, next points to E D.pred points to D, last points to E, next is null E.Other/none/more pred last (we want to remove this one) next iter:

What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); NULL head list: pred last (we want to remove this one) next iter: A.pred points to A, last points to B, next points to D B.pred points to B, last points to D, next points to E C.pred points to B, last is null, next points to D D.pred is null, last is null, next points to D E.Other/none/more

What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); iter.next(); NULL head list: pred last (we want to remove this one) next iter: A.pred is null, last points to D, next points to E B.pred points to B, last points to D, next points to E C.Other/none/more

What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); iter.next(); iter.remove(); NULL head list: pred last (we want to remove this one) next iter: A.pred points to B, last points to E, next points to E B.pred points to B, last points to E, next is null C.pred is null, last is null, next points to E D.Other/none/more

Which code is more efficient? Iterator i = c.iterator(); while ( i.hasNext() ) { String x = i.next(); if ( x.length() > 10 ) System.out.println(x); } for( String x: c ) { if (x.length() > 10 ) { System.out.println(x); } A. Right one is MORE efficient because it doesn’t involve iterators B. Right one is LESS efficient because it doesn’t involve iterators C. Other/none/moe

Which code is more efficient? Iterator i = c.iterator(); while ( i.hasNext() ) { String x = i.next(); if ( x.length() > 10 ) System.out.println(x); } for( String x: c ) { if (x.length() > 10 ) { System.out.println(x); } A. Right one is MORE efficient because it doesn’t involve iterators B. Right one is LESS efficient because it doesn’t involve iterators C. Other/none/moe Trick question! The one on the right DOES use iterators!

Java Nested/Inner Classes

Reading quiz! Suppose we have a linked list implementation that is defined as follows public class SinglyLinkedList implements java.util.List { private static class Node { // nested class T data; Node next; //etc… } private Node head; public LinkedList() { head = null; //etc… } //etc… If we implement the Iterator interface as an instance (i.e. nonstatic) inner class, its instance methods can access everything about its backing structure (the enclosing LinkedList instance) A.TRUE B.FALSE

What this means for your project  Node does not need to access anything about the List class  An individual node has no need to know where the head of the list is  Good idea to make this a static nested class:  private static class Node {  Static does not have access to outer class instance variables like head  Design principle: Do not grant more access than necessary  What about iterator?

Should L12Iter be a static nested class, or an (instance) inner class? A. private class L12Iter implements java.util.Iterator { B. private static class L12Iter implements java.util.Iterator {  WHY?

Iterator does need access to list private variable head  Remember this slide showing iterator constructor code:

What this means for your project  L12Iter does need to access information about the outer List instance  Iterator needs to know where the head of the list is  Good idea to make this a non-static inner class:  private class L12Iter implements java.util.Iterator {

What this means for your project  Pitfall:  Inner classes do not specify parameter because they automatically match outer instance:  private class L12Iter implements java.util.Iterator {  Static nested classes do specify parameter because they do not have an outer instance  private static class Node {

Benchmarking code An alternative/supplement to big-O style analysis

Sources of inaccuracy in benchmarking  Multiple processes running on system  Open up Task Manager on your PC  Or on Linux/UNIX or Mac, in command window run “ps –eaf | more”  Even if you shut down all your major application programs, all kinds of utilities and other things still running  Hardware interrupts  Network activity  Mouse/keyboard activity  JVM garbage collection could start running unexpectedly

IMPROVED procedure for benchmarking for problem size N = min,...max 1. initialize the data structure 2. for K runs: 1. get the current (starting) time 2. run the algorithm on problem size N 3. get the current (finish) time 4. timing = finish time – start time 3. Average timings for K runs for this N

Measurement accuracy 1. 50, 1, 3, 34, 26, 10, 44, 36, 5, 37, , 22, 20, 27, 21, 29, 25, 25, 22, 28, 26  Average is 25 in both cases  Second set of measurements would inspire much more confidence than the first! Why? A. Standard deviation of first set of measurements is HIGHER than standard deviation of second set of measurements B. Standard deviation of first set of measurements is LOWER than standard deviation of second set of measurements

Explain these results... A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800, ,000, ,000, From these measurements, what is the likely big-O time cost of findMax() on array of int ?

Explain these results... A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800, ,000, ,000, From these measurements, what is the likely big-O time cost of findMax() on array of Integer ?

Explain these results... Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800, ,000, ,000, Discussion: Why would Integer take more time? If Integer takes more time, why does it have the same Big-O cost as int?

See Kube’s slides for HW tips!  How to calculate standard deviation  How to disable garbage collection so it doesn’t happen at unexpected times