Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan - - PowerPoint PPT Presentation

compsc sci 201 201 maps ps a and nd link nked l lists
SMART_READER_LITE
LIVE PREVIEW

Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan - - PowerPoint PPT Presentation

Compsc sci 201 201 Maps ps a and nd Link nked L Lists Susan Rodger February 19, 2020 2/19/2020 CompSci 201, Spring 2020 1 K is for Kernel Low-level foundation for an operating system Key Pairs Public & private


slide-1
SLIDE 1

Compsc sci 201 201 Maps ps a and nd Link nked L Lists

2/19/2020 CompSci 201, Spring 2020 1

Susan Rodger February 19, 2020

slide-2
SLIDE 2

K is for …

  • Kernel
  • Low-level foundation for an operating system
  • Key Pairs
  • Public & private key make encryption happening, from

Git to SSL

2/19/2020 CompSci 201, Spring 2020 2

slide-3
SLIDE 3

Announcements

  • Exa

Exam 1 1 – Do no not d dis iscuss unt until w wit ith any h anyone unt until hand handed b bac ack

  • APT

APT Quiz 1 1 out t toda day

  • Do by yourself
  • Assig

ignm nment nt P P3 out F Frida day – due 2/27 ue 2/27

  • Builds on P2 Markov

2/19/2020 CompSci 201, Spring 2020 3

slide-4
SLIDE 4

PFWAE1

  • Qui

uick Revi view of

  • f Maps
  • Link

nked ed List st f from h

  • m high-le

level to to l low-leve vel

  • Similar to how we viewed ArrayList/array
  • Low-level linked lists have history and current

pedigree

  • Iterators, I

Inter erfac aces es, I Idioms

  • From design patterns to APIs
  • APT Qui

uiz ready today, E Exam 1 1 no not g graded y yet

2/19/2020 CompSci 201, Spring 2020 4

slide-5
SLIDE 5

The java.util.Map interface, concepts

  • Has

ashM hMap < <Key,Val alue ue> o

  • r <K,V

Method return purpose map.size() int # keys map.get(K) V get value map.keySet() Set<K> Set of keys map.values() Collection<V> All values map.containsKey(K) boolean Is key in Map? map.put(K,V) V (ignored) Insert (K,V) map.entrySet() Set<Map.Entry> Get (K,V) pairs map.clear() void Remove all keys map.putIfAbsent(K,V) V (ignored) Insert if not there

2/19/2020 CompSci 201, Spring 2020 5

slide-6
SLIDE 6

2/19/2020 CompSci 201, Spring 2020 6

slide-7
SLIDE 7

Investigate Map Solution

  • One p

ne pass o

  • ver

er t the he d data ins instead o

  • f many

any p passes

  • .Understand all map methods
  • Why is line 39 never executed? Still needed?

2/19/2020 CompSci 201, Spring 2020 7

slide-8
SLIDE 8

What is a java.util.List in Java?

  • Interface

ace for c collec lectio ion o n of element nts

  • Add, remove, traverse, …
  • What can a list do to itself?
  • What can we do to a list?
  • Why

hy more t than han one ne kin ind o

  • f lis

list: A Array and and L Link inked?

  • Useful in different applications
  • How do we analyze differences?
  • How do we use them in code?

2/19/2020 CompSci 201, Spring 2020 9

slide-9
SLIDE 9

Remember? list.remove(0)

2/19/2020 CompSci 201, Spring 2020 10

  • Wha

hat is is “faster”? L Link inkedList o

  • r Arra

rrayList

y = -4E-05x + 0.0009 y = 0.0064x2 - 0.0156x + 0.0238 R² = 0.9984 0.2 0.4 0.6 0.8 1 1.2 1.4

RemoveFirst

linked array Linear (linked)

  • Poly. (array)
slide-10
SLIDE 10

ArrayList remove(0) is O(N)

  • Mus

ust s shif hift N N-1 e elem ement ents

  • Details in code below? Some matter, some …
  • Shif

hifting N N elem elements is is O(N2): wh why?

2/19/2020 CompSci 201, Spring 2020 11

slide-11
SLIDE 11

Random Access v Splicing…

  • How does

es f find ind-a-tr track w k work? k? F Fast f st forward?

  • Quick survey of linked list code
  • http://bit.ly/201playRecord

2/19/2020 CompSci 201, Spring 2020 12

slide-12
SLIDE 12

Conceptual: array[] to Node

  • How do

w do w we impl plement Arra rrayList? U ? Use a arra ray[]

  • list.get(n) is O(1), BUT
  • list.remove(0) is O(N)
  • How do we

e im imple lement L Link inkedList? U Use N e Node

  • list.get(n) is O(n), BUT
  • list.remove(0) is O(1)
  • Trad

adeo eoffs: w what d does s sequenc uence o

  • f nodes

es p provid ide? e?

2/19/2020 CompSci 201, Spring 2020 13

slide-13
SLIDE 13

What’s in a Node?

  • Some i

e info forma rmation

  • Pla

lace t to snap nap ano another no node

  • In

n Java w we’ll s ll see ee

  • String reference: info
  • Node reference: next

2/19/2020 CompSci 201, Spring 2020 14

slide-14
SLIDE 14

Visualizing/Understanding Nodes

  • https://co

://coursework rk.cs cs.d .duke.ed edu/r /rodg dger/ er/di diyad ad-new

  • diyad.linkedlist.SimpleLinkedList
  • Like pair, note: this not needed below
  • Instance variables for String and "next node"

2/19/2020 CompSci 201, Spring 2020 15

slide-15
SLIDE 15

remove(0) for linked list?

  • Looking

ng at at remove(0) not

  • t remove(n) now
  • Instance variables myFirst and myLast
  • Initially null, but we'll see what .add does

2/19/2020 CompSci 201, Spring 2020 16

slide-16
SLIDE 16

Adding nodes to end:.add(..)

  • Class i

invar aria iant nt: : myLast refer erenc ences es l las ast n node

  • Symmetry: myFirst references first node
  • When

hen t the he lis list is is em empty, s spec ecial l case?

  • Always add node to end of list

2/19/2020 CompSci 201, Spring 2020 17

slide-17
SLIDE 17

Adding New Nodes

  • To ad

add to the he end end o

  • f a

a link linked lis list

  • Maintain reference to first node
  • only through first node can we access entire list
  • Need reference to last node
  • To add a new last node
  • Often

n need i initiali ializatio ion c n code

  • First node anchors list
  • Must do before

re loop

  • Loop will add over and over to end

2/19/2020 CompSci 201, Spring 2020 18

slide-18
SLIDE 18

Visualizing, Thinking, Understanding

  • How to p

pict ctur ure e myLas ast and and my myLast.ne .next

  • Both are Node pointers aka Node references
  • Like all Java Object variables: memory location
  • Conc

nceptuall lly? A An n ar arrow, a a point inter

  • References a Node: label for memory location

2/19/2020 CompSci 201, Spring 2020 19

myFirst myLast

slide-19
SLIDE 19

Only one node in the list? myFirst? myLast?

  • a

2/19/2020 CompSci 201, Spring 2020 20

myFirst myLast

slide-20
SLIDE 20

Another program for understanding

  • Not m

model elin ing a a Lis ist c cla lass, j jus ust p plai lain N Nodes es

  • https://coursework.cs.duke.edu/201spring20/classcode/blob/master/src/LowLevelLinkDemo.java
  • LowLevelLinkDemo: add to back, keep front
  • Local variable last: always point to last node
  • Each time through loop? True, thus an invari

rian ant

2/19/2020 CompSci 201, Spring 2020 22

slide-21
SLIDE 21

Main in LowLevelLinkDemo.java

2/19/2020 CompSci 201, Spring 2020 23

slide-22
SLIDE 22

Visualizing Code – CreateList

Add nodes to end end of list

  • Using J

Java va T Tutor:

  • See

e fir irst and and la last: b both N h Node v e var ariables

2/19/2020 CompSci 201, Spring 2020 24

slide-23
SLIDE 23

Adding first node to linked list

  • Repe

peatedl dly a add dd fir irst elem element, init initially null null

  • New first node points at previous first node
  • first references/points to new first node
  • Can use first = new Node(vg[k],first)

2/19/2020 CompSci 201, Spring 2020 25

slide-24
SLIDE 24

Visualizing Code – CreateListFront

Add nodes to fro ront of list

  • Using J

Java va T Tutor:

  • See

e fir irst and and la last: b both N h Node v e var ariables

2/19/2020 CompSci 201, Spring 2020 26

slide-25
SLIDE 25

Reference: Array Traversal

  • Visit

iting ing ( (print nting ing) e ever ery v value i ue in an a array

  • Initialize index, print w/index, increment index
  • Elements of array are adjacent in memory

2/19/2020 CompSci 201, Spring 2020 27

slide-26
SLIDE 26

List Traversal

  • Visit

iting ing ( (print nting ing) e ever ery v value i ue in an a array

  • Start with first node, print .info, advance .next
  • Done when current node is null

2/19/2020 CompSci 201, Spring 2020 28

slide-27
SLIDE 27

WOTO (correctness counts)

http://bit.ly/201spring20-0219-1

You can i n install J all Java T Tutor i in IntelliJ lliJ – see c course e websit ite R e Resour urces ces t tab

2/19/2020 CompSci 201, Spring 2020 29

slide-28
SLIDE 28

John Tukey: 1915-2000

  • Cool
  • oley-Tukey F

FFT

  • Bit

it is is a a bina inary d dig igit it

  • Box o
  • r Box and

and W Whis hiskers P Plo lots

2/19/2020 CompSci 201, Spring 2020 30

Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise. The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.

slide-29
SLIDE 29

Removing some values: filter

  • Rem

emove all o all occ ccurrences o

  • f X, o

, or …

  • When we remove in array, we shift. Trouble?
  • ListRemoveAndCount: exception thrown!
  • ConcurrentModificationException

2/19/2020 CompSci 201, Spring 2020 31

slide-30
SLIDE 30

Incorrect Results

  • See

e ListRemoveAnd ndCount unt.j .java

  • You shouldn’t do this: results in errors
  • Remove the kth element (think 0)

2/19/2020 CompSci 201, Spring 2020 32

slide-31
SLIDE 31

Iterators to the Rescue

  • Itera

rators rs ar are e soooo

  • ooo nic
  • nice. B

But ut t tim iming?

  • Why O(N) linked list and O(N2) array?

2/19/2020 CompSci 201, Spring 2020 33

slide-32
SLIDE 32

From Iterator to Iterable

  • Enhance

nced f for: for(String s : list) { …

  • Underneath, uses iterator
  • Code below O(N) for both lists!

2/19/2020 CompSci 201, Spring 2020 34

slide-33
SLIDE 33

From Iterator to Iterable

  • Wha

hat if if ind indexing lo loop us used?,

  • e.g., list.get(k)
  • Code below is ?

2/19/2020 CompSci 201, Spring 2020 35

slide-34
SLIDE 34

Compare the two

  • ListSplice

licer.j .java

2/19/2020 CompSci 201, Spring 2020 36

slide-35
SLIDE 35

WOTO

http:// //bi bit.ly/2 /201spr pring20-02 0219 19-2

2/19/2020 CompSci 201, Spring 2020 38

slide-36
SLIDE 36

APT Practice Quiz

  • APT Pract

ctic ice Q Quiz iz i is o

  • n Sakai n

ai now

  • NOT F

FOR CRE R CREDIT, Just for practice

  • You can see how an APT quiz works
  • Only Available through Sunday 11:59pm
  • RECO

COMM MMEND trying ing A APT Pract ctice ice Q Quiz b before e taking ing t the A APT Quiz iz1

2/19/2020 CompSci 201, Spring 2020 39

slide-37
SLIDE 37

APT Quiz Details

  • APT Q

Quiz 1 1 availabl able o e on Sakai ai

  • Wed. Feb 19 at 8pm – Mon. Feb 24 11:59pm
  • Once y

you st start, y you get 2 2.5 ho hour urs

  • You cannot stop and restart it.
  • More time if you get accommodations
  • Must

st start by y 9:29pm M Mond nday ni y night ht!

  • Recomme

mmend y you take it B BEFORE M E Monday

  • You can se

see t the t timer r in Sa Sakai

  • We will not grade anything you submit after time runs
  • ut
  • You C

CANNO NNOT, C CANNO NNOT, C CANNO NNOT co collaborate o

  • n the

e qu quiz. . We run re reas asonabl ably s sophisticat ated s ed similari arity det etec ection softwa ware re

2/19/2020 CompSci 201, Spring 2020 40

slide-38
SLIDE 38

APT Quiz

  • We expect

ct t that e ever eryone ne w will g get et t the f e first p problem lem

  • Sometimes we are wrong. But it’s designed to

be straightforward. If you’ve done the APTs? You’ll succeed

  • We

e ex expect e everyone w will k ill kno now ho how to solv lve t the he

  • ther proble

lems, b but s sometim imes c coding ing a and deb ebug ugging is is no not ea easy

  • There is a time limit, if stuck? Try next problem

2/19/2020 CompSci 201, Spring 2020 41