59
CS 106 INTRODUCTION TO DATA STRUCTURES SPRING 2020 PROF. SARA MATHIESON HAVERFORD COLLEGE

CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

CS 106INTRODUCTION TO DATA STRUCTURES

SPRING 2020

PROF. SARA MATHIESONHAVERFORD COLLEGE

Page 2: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ADMIN• Midterm 1 in-class on Thursday

• Create one-page (front & back) “cheat-sheet”

• Office Hours TODAY! 4:30-6pm (H110)

• Remind me to hand back Handout 12 and Lab 2

• We DO have lab this week and attendance is still required. You do NOT need to do anything in advance though, you may begin Lab 4 during lab.

• Lab 4 will be posted Wed or Thurs

Page 3: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

• Queues (theory and implementation)

• Review arrays and ArrayLists

• Review Nodes and Linked Lists

• Practice Problems

MAR 3 OUTLINE

Page 4: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

• Queues (theory and implementation)

• Review arrays and ArrayLists

• Review Nodes and Linked Lists

• Practice Problems

MAR 3 OUTLINE

Page 5: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QUEUES

How would you want a data structure to work for waiting in line at a store?

What is the rate of input is different than the rate of output?Example: people show up to the DMV at random times, but processing takes about the same time for each person

Define an abstract data type (ADT).

Page 6: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

THE QUEUE ADT

Insertions and deletions are First In First Out (FIFO)-Insert at the back-Delete from the front

Operations:• enqueue(Object)• Object dequeue()• Object first()• int size()• boolean isEmpty()

Page 7: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

IMPLEMENTING A QUEUEBrainstorm: using the data structures we know about, how could we implement this ADT?

Page 8: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

IMPLEMENTING A QUEUEBrainstorm: using the data structures we know about, how could we implement this ADT?

Many ways to implement a Queue! Underneath, we can use:* Arrays* Lists* Stacks

Page 9: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAY-BASED QUEUE IMPLEMENTATIONAn array of size n in a circular fashionTwo ints to track front and size

• f: index of the front element• size: number of stored elements

Q

0 1 2 rf

normal configuration

Q

0 1 2 fr

wrap-around configuration (circular)

Page 10: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

0 1 2 3 4

Size: 0

Arrow is front

Page 11: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

30 1 2 3 4

Size: 1

Arrow is front

Page 12: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

3 40 1 2 3 4

Size: 2

Arrow is front

Page 13: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

3 4 50 1 2 3 4

Size: 3

Arrow is front

Page 14: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

3 4 5 20 1 2 3 4

Size: 4

Arrow is front

Page 15: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

3 4 5 2 10 1 2 3 4

Size: 5

Arrow is front

Page 16: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

4 5 2 10 1 2 3 4

Size: 4

Arrow is front

Page 17: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

5 2 10 1 2 3 4

Size: 3

Arrow is front

Page 18: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

-9 5 2 10 1 2 3 4

Size: 4

Arrow is front

Page 19: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

-9 2 10 1 2 3 4

Size: 3

Arrow is front

Page 20: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

-9 10 1 2 3 4

Size: 2

Arrow is front

Page 21: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

-90 1 2 3 4

Size: 1

Arrow is front

Page 22: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

0 1 2 3 4

Size: 0

Arrow is front

Page 23: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

EXAMPLE

-80 1 2 3 4

Size: 1

Arrow is front

Page 24: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 25: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AYTwo constructors (allow

user to select capacity, or use default.

Page 26: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 27: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 28: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 29: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 30: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 31: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 32: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QU

EU

E W

ITH

C

IRC

ULA

R A

RR

AY

Page 33: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

DESIGNING DATA STRUCTURES1. Make a Course object that can store a name and list of

students. Include relevant constructors, getters, and setters.

2. Make a LimitedEnrollmentCourse that has a cap on the number of students who can enroll. Have it inherit from Course.

3. Make addStudent, removeStudent, and getEnrolledmethods that correctly handle limited versus unlimited enrollment.

Extra practice!

Page 34: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &
Page 35: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

• Queues (theory and implementation)

• Review arrays and ArrayLists

• Review Nodes and Linked Lists

• Practice Problems

MAR 3 OUTLINE

Page 36: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAYS• Fixed length

• Pro: all operations O(1)• Con: cannot resize or move around elements easily

• Declare

• Initialize (allocate)

Page 37: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAYS• Fixed length

• Pro: all operations O(1)• Con: cannot resize or move around elements easily

• Declare

• Initialize (allocate)

• “set”:

• “get”:

• length

Page 38: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAYS• Fixed length

• Pro: all operations O(1)• Con: cannot resize or move around elements easily

• Declare

• Initialize (allocate)

• “set”:

• “get”:

• length

Q: what is happening here??

Page 39: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAY LISTS• We allow the size to change, but we don’t copy over elements

every time a new element doesn’t fit

• Use the idea of doubling the size of the array to get an average creation time of O(n) for length n array

• Declare/Initialize

• add:

Page 40: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

ARRAY LISTS• We allow the size to change, but we don’t copy over elements

every time a new element doesn’t fit

• Use the idea of doubling the size of the array to get an average creation time of O(n) for length n array

• Declare/Initialize

• add:

• get:

• set:

• size:

Page 41: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

• Queues (theory and implementation)

• Review arrays and ArrayLists

• Review Nodes and Linked Lists

• Practice Problems

MAR 3 OUTLINE

Page 42: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

NODE OBJECTS

• Doubly linked list

• Singly linked list

Match the constructor to the type of list that would contain such Nodes

Page 43: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

NODE OBJECTS

• Doubly linked list

• Singly linked list

Match the constructor to the type of list that would contain such Nodes

Page 44: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

LINKED LISTS

• Singly linked list

• Singly linked list with tail pointer

• Doubly linked list

• Doubly linked list with tail pointer

• Doubly linked with sentinels

Match the constructor to the appropriate type(s) of list.

Page 45: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

LINKED LISTS

• Singly linked list

• Singly linked list with tail pointer

• Doubly linked list

• Doubly linked list with tail pointer

• Doubly linked with sentinels

Match the constructor to the appropriate type(s) of list.

Page 46: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

LINKED LISTS

• Singly linked list

• Singly linked list with tail pointer

• Doubly linked list

• Doubly linked list with tail pointer

• Doubly linked with sentinels

Match the constructor to the appropriate type(s) of list.

Page 47: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

LINKED LISTS

• Singly linked list

• Singly linked list with tail pointer

• Doubly linked list

• Doubly linked list with tail pointer

• Doubly linked with sentinels

Match the constructor to the appropriate type(s) of list.

Page 48: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

What is the issue with the following code?

What is printed?

Page 49: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

What is the issue with the following code?

What is printed?

Only “107”!

Page 50: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &
Page 51: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

• Queues (theory and implementation)

• Review arrays and ArrayLists

• Review Nodes and Linked Lists

• Practice Problems

MAR 3 OUTLINE

Page 52: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

WORK WITH A PARTNER!

• Question 1: focus on understanding the code and thinking about what type of loops to use (don’t rewrite the code now)

• Question 2: skip (about Queues)

Page 53: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QUESTION 1

Page 54: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &
Page 55: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &
Page 56: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &
Page 57: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QUESTION 3There are many ways to do this! Two are shown below. What

are the runtimes of these two algorithms?

1)

2)

Page 58: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QUESTION 4a) get & set (only public methods)

b) Could be a LinkedList or an ArrayList

c) Less efficient than an ArrayList, same efficiency as a LinkedList

d) No! the details are abstracted away

Visualization:

Assume that we have stack A with elements 5, 11, -1, 3 (where 3 is on the top). What happens when we call get(2)? Draw the stacks A and B and see what happens.

Page 59: CS 106 INTRODUCTION TO DATA STRUCTUREScs.haverford.edu/faculty/smathieson/teaching/s20/lecs/lec13/lec13.pdf · ADMIN •Midterm 1 in-class on Thursday •Create one-page (front &

QUESTION 5

a) Pseudocode: tail = tail.prevtail.next = null

b) Hints: there are 6 pointers that need to be rearranged (2 for A, 2 for B, and head/tail)

d) See notes from Lecture 11