Data Structures and Algorithms



Similar documents
Data Structures and Algorithms Lists

Data Structures and Algorithms

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Class 32: The Java Collections Framework

D06 PROGRAMMING with JAVA

Data Structures in the Java API

Ordered Lists and Binary Trees

recursion, O(n), linked lists 6/14

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Analysis of a Search Algorithm

The Java Collections Framework

Chapter 8: Bags and Sets

CMSC 202H. ArrayList, Multidimensional Arrays

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Circular Linked List. Algorithms and Data Structures

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

DATA STRUCTURE - STACK

LINKED DATA STRUCTURES

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures and Algorithms Stacks and Queues

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Unordered Linked Lists

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Abstract Data Types. Chapter 2

Abstraction and Abstract Data Types

Practical Session 4 Java Collections

Linked List as an ADT (cont d.)

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Introduction to Object-Oriented Programming

Working with Java Collections

Binary Heap Algorithms

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Project 4 DB A Simple database program

Lecture 12 Doubly Linked Lists (with Recursion)

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Software Development with UML and Java 2 SDJ I2, Spring 2010

DATA STRUCTURE - QUEUE

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Introduction to Stacks

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Linked Lists Linked Lists, Queues, and Stacks

Node-Based Structures Linked Lists: Implementation

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Per imparare a programmare bisogna programmare

Programming with Data Structures

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

JAVA COLLECTIONS FRAMEWORK

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Cpt S 223. School of EECS, WSU

Data Structures Using C++

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Chapter 13: Query Processing. Basic Steps in Query Processing

Dynamic Programming Problem Set Partial Solution CMPSC 465

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Section IV.1: Recursive Algorithms and Recursion Trees

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

7.1 Our Current Model

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

CompSci-61B, Data Structures Final Exam

Assignment 3 Version 2.0 Reactive NoSQL Due April 13

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

STACKS,QUEUES, AND LINKED LISTS

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Sequential Data Structures

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Heaps & Priority Queues in the C++ STL 2-3 Trees

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Stacks. Linear data structures

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Sequences in the C++ STL

CSE 211: Data Structures Lecture Notes VII

Algorithms and Data Structures

The Interface Concept

Lecture Notes on Binary Search Trees

Data Structures Fibonacci Heaps, Amortized Analysis

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

>

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Transcription:

Data Structures and Algorithms CS245-2016S-05 Abstract Data Types and Lists David Galles Department of Computer Science University of San Francisco

05-0: Abstract Data Types Recall that an Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface Data in an ADT cannot be manipulated directly only through operations defined in the interface

05-1: List ADT A List is an ordered collection of elements Each element in the list has a position Element 0, Element 1, Element 2,... We can access elements in the list through an iterator

05-2: List ADT Operations Create an empty list Add (append) an element to the end of the list Add (insert) an element at a spectified index Get the size (length) of the list Remove an element at a specific index Remove the first occurence of an element Get an element at a specific index Get an iterator to traverse the list

05-3: Iterators Think of an iterator as a smart bookmark that is associated with a specific data structure Often used to examine every element in a data structure

05-4: Iterators Some operation on iterators: Retrieve the current element Move the iterator forward, to the next element in the data structure C++ has two different operations: Get current and Move forward Java has a single operation: Get current and move forward Move the iterator backwards, to the previous element in the data structure Not all iteratrs can go backwards Java also combines going backwards as Get previous element and move iterator backwards

05-5: Iterators Some operation on iterators: Delete element at current location (not always allowed) Insert an element at the current location (not always allowed) Operations specific to the particular data structure

05-6: List Iterator (first pass) Get the next element (moving the iterator one forwad) Check if their is a next element Remove the object at the current position (current position == last element that was returned from a next ) Insert an element at the current position (right before the next element)

05-7: Java Interfaces A Java interface is a set of methods. Any class that implements an interface must implement all of these methods

05-8: Java List Interface public interface List { public void clear(); public void add(object o); public void add(int index, Object o); public void remove(int index); public void remove(object o); public int size(); public Object get(int index); public ListIterator listiterator(); public ListIterator listiterator(int index); }

05-9: Java List Iterator Interface public interface ListIterator { public void add(object o); public boolean hasnext(); public Object next(); public void remove(); public void set(object o); }

05-10: Using Iterators Print out a list L: List L;... ListIterator it = L.listIterator(); for (it.hasnext()) { System.out.println(it.next()); }

05-11: Array Implementation Data is stored in an array Iterator stores index of next location To add an element to the current position: Shift all elements with index >= current one to right To remove and element from the middle of the array: Shift all elements with index >= current to the right List has a maximum size (unless we use growable arrays)

05-12: Array Implementation Θ() Running Time for each operation: List Operations Iterator Operations add(append) next add(insert) hasnext remove add listiterator() remove listiterator(n) set size get

05-13: Array Implementation Θ() Running Time for each operation: List Operations Iterator Operations add(append) Θ(1) next Θ(1) add(insert) Θ(n) hasnext Θ(1) remove Θ(n) add Θ(n) listiterator() Θ(1) remove Θ(n) listiterator(n) Θ(1) set Θ(1) size Θ(1) get Θ(1)

05-14: Linked-List Implementation Data is stored in a linked list Maintain a pointer to first element in list Iterator maintains a pointer to the next element To find the ith element: Start at the front of the list Skip past i elements How do we insert an element before the next element? How do we remove the current element?

05-15: Linked-List Implementation Data is stored in a linked list Maintain a pointer to first element in list Iterator maintains a pointer to the element before the next element ( current element) and a pointer to the element before the current element. To find the ith element: Start at the front of the list Skip past i elements What should current pointer be when the next element is the first element in the list?

05-16: Linked-List Implementation Data is stored in a linked list with a dummy first element Maintain a pointer to first (dummy) element in list Iterator maintains a pointer to the element before the next element ( current element) and the previous element (what should previous be when the first element of the list is the next element in the list?) To find the ith element: Start at the front of the list Skip past (i+1) elements

05-17: Linked-List Implementation Θ() Running Time for each operation: List Operations Iterator Operations add(append) next add(insert) hasnext remove add listiterator() remove listiterator(n) set size get

05-18: Linked-List Implementation Θ() Running Time for each operation: List Operations Iterator Operations add(append) Θ(1) next Θ(1) add(insert) Θ(n) hasnext Θ(1) remove Θ(n) add Θ(1) listiterator() Θ(1) remove Θ(1) listiterator(n) Θ(n) set Θ(1) size Θ(1) get Θ(n)

05-19: Adding Previous Add a new operation to the iterator: previous Move the iterator back one element, return the previous elememt next() followed by previous(), both return same element How would we implement previous for an array implementation

05-20: Adding Previous Add a new operation to the iterator: previous Move the iterator back one element, return the previous elememt next() followed by previous(), both return same element How would we implement previous for an array implementation Subtract one from the index of the current location

05-21: Adding Previous Add a new operation to the iterator: previous Move the iterator back one element How would we implement previous for a linked list implementation

05-22: Adding Previous Add a new operation to the iterator: previous Move the iterator back one element How would we implement previous for a linked list implementation Start a temp pointer at the front of the list, advance it until temp.next = current pointer How can we improve the running time of previous for the linked list version?

05-23: Doubly-Linked Lists Each element in the list has two pointers next and previous Can locate the previous element of any element in the list in time O(1), instead of time O(n) More space is required (two pointers for each element, instead of one) Do we still need a dummy element?

05-24: Multiple Iterators We can have more than one iterator going in the same list Handy for comparing every element in the list to every other element in the list Can have a problem when one iterator modifies the list while another iterator is active Examples

05-25: Multiple Iterators We can have more than one iterator going in the same list Can have a problem when one iterator modifies the list while another iterator is active Solutions: Throw exception (how java libraries do it) Inform the other iterators List maintains a pointer to each active iterators When a change is made, each active iterator needs to be updated, too