Ordered Structures Wellesley College CS230 Lecture 10

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Advertisements

1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
COMP 103 Linked Stack and Linked Queue.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
TCSS 342, Winter 2005 Lecture Notes
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
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.
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Linked Data Structures
Ordered Structures Wellesley College CS230 Lecture 11
Review Array Array Elements Accessing array elements
The List ADT.
Week 4 - Friday CS221.
Queues Rem Collier Room A1.02
Heaps And Priority Queues
CSE 373: Data Structures and Algorithms
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Top Ten Words that Almost Rhyme with “Peas”
CMSC 341 Lecture 5 Stacks, Queues
Iteration Abstraction
A Data Structure Bestiary
Stacks and Queues.
A Data Structure Bestiary
Lecture 7: Linked List Basics reading: 16.2
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Iteration Abstraction
Example: LinkedSet<T>
slides adapted from Marty Stepp and Hélène Martin
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
slides created by Marty Stepp
ITI Introduction to Computing II Lab-12
Queues Definition of a Queue Examples of Queues
Building Java Programs
Stacks, Queues, and Deques
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
slides adapted from Marty Stepp
Chapter 9 The Priority Queue ADT
slides created by Marty Stepp
Stacks and Queues.
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Ordered Structures Wellesley College CS230 Lecture 10 Thursday, March 8 Handout #21 Exam 1 due Friday, March 16

Overview of Today’s Lecture Insertion sort on integer arrays. Using Comparable<T> and Comparator<T> for more general orderings. Some other approaches to sorting Priority Queues = ordered queues in which maximal element is dequeued.

Insertion Sort on Integer Arrays 7 1 6 2 3 3 9 4 public static void insertionSort (int[] a) { for (int i = 1; i < a.length; i++) { insert(a, i); //Assume a[0..(i-1)] is sorted // Insert a[i] so that a[0..i] is sorted } 6 1 7 2 3 3 9 4 3 1 6 2 7 3 9 4 3 1 6 2 7 3 9 4 3 1 4 2 6 3 7 4 9

Inserting One Element 3 6 7 9 val 4 //Assume a[0..(i-1)] is sorted 3 1 6 2 7 3 9 4 val 4 //Assume a[0..(i-1)] is sorted // Insert a[i] so that a[0..i] is sorted public static void insertion (int[] a, int i) { } j 3 1 6 2 7 3 9 4 9 val 4 j 3 1 6 2 7 3 7 4 9 val 4 j 3 1 6 2 6 3 7 4 9 val 4 j 3 1 4 2 6 3 7 4 9 val 4

Insertion Sort in One Method public static <T> void insertionSort (int [] a) { for (int i = 1; i < a.length; i++) { int val = a[i]; int j = i; while ((j > 0) && (val < (a[j-1]))) { // Order of tests is crucial! a[j] = a[j-1]; // Shift value right = shift hole left. j = j-1; } // At this point, either (j = 0) or ((j > 1) && (val >= a[j-1])) a[j] = val;

Generalizing to an Array of Comparables public static <T> void insertionSort (Comparable<T> [] a) { for (int i = 1; i < a.length; i++) { Comparable<T> val = a[i]; int j = i; while ((j > 0) && (val.compareTo(a[j-1]) < 0)) { // Order of tests is crucial! a[j] = a[j-1]; // Shift value right = shift hole left. j = j-1; } // At this point, either (j = 0) or ((j > 1) && (val.compareTo(a[j-1]) >= 0)) a[j] = val;

Using an Explicit Comparator public static <T> void insertionSort (Comparator<T> comp, T[] a) { for (int i = 1; i < a.length; i++) { T val = a[i]; int j = i; while ((j > 0) && (comp.compare(val, a[j-1]) < 0)) { // Order of tests is crucial! a[j] = a[j-1]; // Shift value right = shift hole left. j = j-1; } // At this point, either (j = 0) or ((j > 1) && (val.compareTo(a[j-1]) >= 0)) a[j] = val;

A Queue is a FIFO Collection A queue is like a tube. Different ends are used to enqueue (insert) and dequeue (remove) elements. back end front end dequeue elements here enqueue elements here The enq() method enqueues an element and deq() dequeues an element. front() returns the element at the front of the queue without removing it Suppose q is an empty queue q.deq() → “bat” “dog” “ant” q.enq(“bag”) “bat” q.deq() → “ant” “dog” q.enq(“ant”) “ant” “bat” q.deq() → “dog” q.enq(“dog”) “dog” “ant” “bat”

Queue Interface import java.util.*; // imports Iterator and Iterable /** Interface to mutable queues with elements of type T. A queue is a first-in-first-out collection. WARNING: the CS230 Queue interface is different than the java.util.Queue interface. */ public interface Queue<T> extends Iterable<T> { public void enq(T elt); // Add elt to back of queue. public T deq(); // Remove and return element from front of queue. public T front(); // Return front element of queue without removing it. public boolean isEmpty(); // Return true if this queue is empty; else false public int size(); // Return the number of elements in this queue public void clear(); // Remove all elements from this queue public Queue<T> copy(); // Return a shallow copy of this queue public Iterator<T> iterator(); // Return an iterator that yields // this queue's elements from front to back. }

Vector Implementations of Stacks

Bad Vector Implementations of Queues

A Good Vector Implementation of Queues

A Good Linked List Implementation of Queues QueueTwoEndedLinkedList back q front “bat” “ant” “dog” q.enq(“cat”) QueueTwoEndedLinkedList back q front “bat” “ant” “dog” “cat” q.deq() → “bat” QueueTwoEndedLinkedList back q front “ant” “dog” “cat”

What is an Empty Queue? We define a queue to be empty if front contains the empty list; what back contains is irrelevant. E.g., both of the following are empty: QueueTwoEndedLinkedList back front QueueTwoEndedLinkedList back front “dog” This simplifies the implementation of clear(), which can just set front to the empty list.

Equeuing into an Empty Queue Enqueuing an element into an empty queue is a special case: QueueTwoEndedLinkedList back ??? (can be anything) front q.enq(“bat”); QueueTwoEndedLinkedList back front “bat”

Representing Linked Lists as “Raw” Nodes Abstract View: “bat” “ant” “dog” Concrete View: ListNode ListNode ListNode value “bat” value “ant” value “dog” next next next

A ListNode<T> Class class ListNode<T> { // Simplify things by using public instance variables. public T value; // Like head in CS111 list public ListNode<T> next; // Like tail in CS111 list public ListNode (T value, ListNode<T> next) { this.value = value; this.next = next; }

Linked-List Queue Code: Skeleton public class QueueTwoEndedLinkedList<T> implements Queue<T> { // Instance Variables: private ListNode<T> front, back; // Initially contain null by default // Constructor Methods: public QueueTwoEndedLinkedList() { // front is null by default, and back is irrelevant at beginning, so do nothing. } // Instance Methods public boolean isEmpty () { return front == null; // By design, queue is empty if front is empty public void clear() { front = null; // by design, don't need to modify back. // Remaining instance methods go here

Linked-List Queue Code: enq() public void enq (T elt) { if (isEmpty()) { // Enqueuing onto the empty list is a special case: front = new ListNode<T>(elt, null); back = front; // initializes back for empty front } else { back.next = new ListNode<T>(elt, null); back = back.next; }

Linked-List Queue Code: deq() and front() public T deq () { if (isEmpty()) { throw new RuntimeException("Attempt to deq() an empty queue"); } else { T result = front.value; } public T front () { throw new RuntimeException("Attempt to find front() of an empty queue"); return front.value;

Linked-List Queue Code: size() and copy() public int size() { // Calculate the length of the linked list in front. int len = 0; for (ListNode<T> current = front; current != null; current = current.next) { len++; } return len; public Queue<T> copy() { QueueTwoEndedLinkedList<T> qCopy = new QueueTwoEndedLinkedList<T>(); if (front != null) { qCopy.front = new ListNode<T>(front.value, null); ListNode<T> prevCopy = qCopy.front; for (ListNode<T> current = front.next; current != null; current = current.next) { prevCopy.next = new ListNode<T>(current.value, null); prevCopy = prevCopy.next; return qCopy;

Linked-List Queue Code: toString() public String toString() { StringBuffer buff = new StringBuffer(); buff.append("QueueTwoEndedLinkedList["); if (front != null) { buff.append(front.value); // Treat first element specially – no comma in front for (ListNode<T> current = front.next; current != null; current = current.next) { buff.append("," + current.value); } buff.append("]"); return buff.toString();

Linked-List Queue Code: iterator() public Iterator<T> iterator() { return new QueueIterator<T>(this); } where QueueIterator is defined as: public class QueueIterator<T> implements Iterator<T> { private Queue<T> q; // holds a copy of the queue being iterated over public QueueIterator (Queue<T> q) { this.q = q.copy(); } // copy the given queue public boolean hasNext() { return ! q.isEmpty(); } public T next() { return q.deq(); } // Remove and return the next elt from copy public void remove() { // Don’t handle this method throw new UnsupportedOperationException( "QueueIterator<T> does not support the remove() operation.");

Linked-List Queue Code: main() public static void main (String[] args) { // QueueTester.test performs simple tests on any queue implementation: QueueTester.test(new QueueTwoEndedLinkedList<String>()); } q = QueueTwoEndedLinkedList[]; q.size() = 0; q.front() = Attempt to find front() of an empty queue q.enq(B); q = QueueTwoEndedLinkedList[B]; q.size() = 1; q.front() = B q.enq(C); q = QueueTwoEndedLinkedList[B,C]; q.size() = 2; q.front() = B q.enq(B); q = QueueTwoEndedLinkedList[B,C,B]; q.size() = 3; q.front() = B q.enq(A); q = QueueTwoEndedLinkedList[B,C,B,A]; q.size() = 4; q.front() = B q.deq() = B; q = QueueTwoEndedLinkedList[C,B,A]; q.size() = 3; q.front() = C q.deq() = C; q = QueueTwoEndedLinkedList[B,A]; q.size() = 2; q.front() = B q.deq() = B; q = QueueTwoEndedLinkedList[A]; q.size() = 1; q.front() = A q.enq(D); q = QueueTwoEndedLinkedList[A,D]; q.size() = 2; q.front() = A q.enq(A); q = QueueTwoEndedLinkedList[A,D,A]; q.size() = 3; q.front() = A q.enq(C); q = QueueTwoEndedLinkedList[A,D,A,C]; q.size() = 4; q.front() = A q2 = (Queue<String>) q.copy(); q2 = QueueTwoEndedLinkedList[A,D,A,C]; q2.size() = 4; q2.front() = A q.clear(); q = QueueTwoEndedLinkedList[]; q.size() = 0; q.front() = Attempt to find front() … q2 = QueueTwoEndedLinkedList[A,D,A,C]; q2.size() = 4; q2.front() = A Iterating the elements of q2: 0:A 1:D 2:A 3:C

An Improved Queue: Caching the Size Can change size() from a linear-time to a constant time operation by storing it in an instance variable. For example: QueueTwoEndedLinkedList back q front size 3 “bat” “ant” “dog” q.enq(“cat”) QueueTwoEndedLinkedList back q front size 4 “bat” “ant” “dog” “cat”

Code Modifications for Queue with Cached Sizes public class QueueTwoEndedLinkedListBetter<T> implements Queue<T> { // Instance Variables: private ListNode<T> front, back; // Initially contain null by default private int size = 0; public int size() { return size; } // Constant time, not linear time in # of elts public void clear() { front = null; size = 0; } public void enq (T elt) { … usual enq() code goes here … size++; } public void deq (T elt) { … put size--; before return result … } public Queue<T> copy() { … put qCopy.size = size; before return qCopy … } // All other methods are unchanged. }

public void enq (T elt) { … usual enq() code goes here … size++; }