CS 367 Introduction to Data Structures Charles N. Fischer Fall 2015 s367.html.

Slides:



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

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
5/19/2015CS 2011 CS 201 – Data Structures and Discrete Mathematics I Syllabus Spring 2014.
1-1 CMPT 225 Data Structures and Programming Instructor: Aaron Hunter Section: E300 Campus: Harbour Centre Semester: Spring 2007.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
Class 0: Review and Perspective. cis 335 Fall 2001 Barry Cohen Class info n Barry Cohen n n Office hours: W 3:15-4:40.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
EE 220 (Data Structures and Analysis of Algorithms) Instructor: Saswati Sarkar T.A. Prasanna Chaporkar, Programming.
CENG 213 Data Structures Department of Computer Engineering Middle East Technical University Fall 2014 CENG 213 Data Structures 1.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
July 16, Introduction to CS II Data Structures Hongwei Xi Comp. Sci. Dept. Boston University.
Computer Science 102 Data Structures and Algorithms V Fall 2009 Lecture 1: administrative details Professor: Evan Korth New York University 1.
COMP 151: Computer Programming II Spring Course Topics Review of Java and basics of software engineering (3 classes. Chapters 1 and 2) Recursion.
WEEK 1 CS 361: ADVANCED DATA STRUCTURES AND ALGORITHMS Dong Si Dept. of Computer Science 1.
© 2004 Goodrich, Tamassia CS2210 Data Structures and Algorithms Lecture 1: Course Overview Instructor: Olga Veksler.
Math 125 Statistics. About me  Nedjla Ougouag, PhD  Office: Room 702H  Ph: (312)   Homepage:
Course Introduction Bryce Boe 2012/08/06 CS32, Summer 2012 B.
Data Structures and Programming.  Today:  Administrivia  Introduction to 225, Stacks  Course website: 
Data Structures Lecture 1: Introduction Azhar Maqsood NUST Institute of Information Technology (NIIT)
Lecture 1 Page 1 CS 111 Summer 2015 Introduction CS 111 Operating System Principles.
EECE 310 Software Engineering Lecture 0: Course Orientation.
CSCA48 Course Summary.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Introduction. 2COMPSCI Computer Science Fundamentals.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
CSE 113 Introduction to Computer Programming Lecture slides for Week 5 Monday, September 26 th, 2011 Instructor: Scott Settembre.
CSCI 51 Introduction to Computer Science Dr. Joshua Stough January 20, 2009.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
Introduction to Data Structures
Computer Science 102 Data Structures and Algorithms CSCI-UA.0102 Fall 2012 Lecture 1: administrative details Professor: Evan Korth New York University.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
1 - 1 CS230: Data Structures Spring 2007 Reading:Downey: Chapters 1, 2, 3, Sections , 4.13, , Chapters 5 and 6 Problem Set:Problem Set.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
CSE 113 Introduction to Computer Programming Lecture slides for Week 12 Monday, November 14 th, 2011 Instructor: Scott Settembre.
Course Introduction Bryce Boe 2013/09/30 CS24, Fall 2013.
CSC 212 – Data Structures Lecture 37: Course Review.
Introduction to ECE 2401 Data Structure Fall 2005 Chapter 0 Chen, Chang-Sheng
CS 210 DATA STRUCTURES AND ALGORITHIMS Fall 2006.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
ICS202 Data Structures King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
CS 367 Introduction to Data Structures Lecture 5.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Data Structures and Algorithms in Java AlaaEddin 2012.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Principles of Imperative Computation Lecture 1 January 15 th, 2012.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
1 Welcome Alireza Humber College Lecture 1 Game 540 Alireza
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Using the Java Collection Libraries COMP 103 # T2
Lecture 2 of Computer Science II
September 27 – Course introductions; Adts; Stacks and Queues
Introduction to Data Structures
Introduction to CS II Data Structures
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
Stacks Abstract Data Types (ADTs) Stacks
Principles of Imperative Computation
Presentation transcript:

CS 367 Introduction to Data Structures Charles N. Fischer Fall s367.html

Class Meets Tuesdays, 5:30 — 8:30 Supernova Room, Epic Campus

Instructor Charles N. Fischer Office Hours: Mondays & Thursdays, 5:00 – 7:00, Fahrenheit 451

Teaching Assistant Wei-Chen Chen Office Hours: Wednesdays, 5:00 – 7:00, Fahrenheit 451

Course Components Five programming assignments, each worth 8% Ten homework assignments, each worth 3% A midterm and final exam, each worth 15% Final grades are not curved.

Class Text and Notes No class text book; instead a set of class notes (see syllabus page) Each lecture has a corresponding set of notes PowerPoint for lecture overheads will be available prior to lectures

Late & Partnership Policy No late hand ins unless approved in advance. Reasonable excuses will be allowed. Programs may be done individually, or in two person teams (your choice). All other work must be done individually.

Key Dates September 18Homework 1 September 28Project 1 October 2Homework 2 October 9Homework 3 October 19Project 2 October 29Midterm, 1 st sitting October 23Homework 4 October 30Homework 5 November 5Midterm, 2 nd sitting

November 9Project 3 November 13Homework 6 November 20Homework 7 November 30Project 4 December 4Homework 8 December 11Homework 9 December 17Final, 1 st sitting December 18Homework 10 December 21Project 5 December 22Final, 2 nd sitting

Piazza Piazza is an interactive online platform used to share class- related information Use it to ask questions and track course- related information Students may answer questions posed by others

Goals of Class Survive 15 lectures! Study fundamental data structures, including lists, trees, stacks, queues, graphs, and hash tables Study important algorithms involving, sorting, lookup, and complexity Learn advanced Java concepts, including interfaces, generics, iterators and exceptions

What makes software good? Efficient (fast) Reliable Robust --- Handles errors well Easy to use Useful --- does important work Lots of features Friendly interface Affordable Easy to maintain & update Scalable Secure

What makes code reusable? Well structured; follows coding rules Good documentation Modular --- Independent components Public/private distinction Compatible with other systems Uses objects (abstract data types) Generic code (type parameters) Flexible user interface Exceptions handle special cases

Abstract Data Types Separate needed operations from actual implementation. User determines what’s needed in an Interface. Implementer programs operations in a Class. Can use any class that meets the interface requirements.

Interface says what is need Class says what is implemented

Remove Add BAG Abstract Data Type

Interfaces in Java Interfaces specify the operations an AST will provide. This is independent of implementation !

Interface for a Bag public interface BagADT { void add(Object item); Object remove () throws NoSuchElementException ; boolean isEmpty(); }

Interface issues  Why use class Object ?  Why is an exception thrown?  Should insert of null be allowed?

Useful methods can be defined in terms of Interfaces These methods can be used by any implementation of the Interface!

public static void printBag(BagADT myBag){ /* Body here */ }

public static void printBag(BagADT myBag){ while(! bag.isEmpty()) { /* Loop Body here */ } }

public static void printBag(BagADT myBag){ while(! bag.isEmpty()) { System.out.println(bag.remove()); } }

But printBag has a major flaw! It has a side-effect (it is destructive!) The following doesn’t work as expected: printBag(myBag); printBag(myBag);

The following simple fix doesn’t work: public static void printBag(BagADT myBag){ BagADT temp = myBag; while(! temp.isEmpty()) { System.out.println(temp.remove()); } }

A simple assignment simply adds a new reference to an existing object. After temp = myBag; we have two references to one object.

If we require that the interface implements: BagADT clone(); We can use this: public static void printBag(BagADT myBag){ BagADT temp = myBag.clone(); while(! temp.isEmpty()) { System.out.println(temp.remove()); } }

Let’s implement a BagADT We need to define: Local Data Structures Constructors Implementations of all interface methods

Let’s build a BagAST using an array of Objects. Arrays are simple to use but also have a fixed size.

public class ArrayBag implements BagADT { /* Local data to implement a Bag */ /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { itemCount = 0; INIT_SIZE = 100; items = new Object[INIT_SIZE]; } /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { return (itemCount == 0); } }

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) { if (item == null) throw new NullPointerException(); if (itemCount >= INIT_SIZE) throw new Error(); items[itemCount] = item; itemCount++; }}

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException { if (itemCount == 0) throw new NoSuchElementException(); else { itemCount--; return items[itemCount] ; } }

public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException {…} public ArrayBag clone() { ArrayBag copy = new ArrayBag(); copy.itemCount = itemCount; copy.items = items.clone(); return copy; } }

Examples of using ArrayBag: ArrayBag bag = new ArrayBag(); bag.add(1); bag.add(2); bag.add(3); bag.add(1); bag.add(2); bag.add(3); printBag(bag); printBag(bag); int item = (int) bag.remove(); int item = (int) bag.remove(); System.out.println(item); System.out.println(item); Output is:3213

Using the Object class in BagADT can be problematic You have to type-cast all objects returned by remove () (Why?) It is hard to enforce a uniform type in a bag. Bag declarations are uninformative. (All bags are essentially the same)

Java Generics Generics allow you to add a type parameter to an interface or class: BagADT or ArrayBag

When a type is declared, a class name replaces the type parameter: ArrayBag or ArrayBag Only the declared type can be inserted. Removed items need not be type-cast.

public interface BagADT { void add(E item); E remove () throws NoSuchElementException ; boolean isEmpty(); BagADT clone(); }

public class ArrayBag implements BagADT { /* Local data to implement a Bag */ /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { itemCount = 0; INIT_SIZE = 100; // Kludge alert! items = (E[]) new Object[INIT_SIZE]; } /* Implementations for add, remove, isEmpty and clone */ }

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { return (itemCount == 0); } }

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(E item) { if (item == null) throw new NullPointerException(); if (itemCount >= INIT_SIZE) throw new Error(); items[itemCount] = item; itemCount++; }}

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public E remove() throws NoSuchElementException { if (itemCount == 0) throw new NoSuchElementException(); else { itemCount--; return items[itemCount] ; } }

public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException {…} public ArrayBag clone() { ArrayBag copy = new ArrayBag (); copy.itemCount = itemCount; copy.items = items.clone(); return copy; } }

printBag becomes: public void printBag(BagADT myBag){ BagADT temp = myBag.clone(); while(! temp.isEmpty()) { System.out.println(temp.remove()); } }

Examples of using ArrayBag in Generic form: ArrayBag bag = new ArrayBag (); bag.add(1); bag.add(2); bag.add(33); bag.printBag(bag); int item = bag.remove(); // No casting! System.out.println(item); Output is:332133