+ All Categories
Home > Documents > Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low...

Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low...

Date post: 26-Mar-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
38
Compsci 201 Maps and Linked Lists 2/19/2020 CompSci 201, Spring 2020 1 Susan Rodger February 19, 2020
Transcript
Page 1: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Compsci 201Maps and Linked Lists

2/19/2020 CompSci 201, Spring 2020 1

Susan Rodger

February 19, 2020

Page 2: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

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

Page 3: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Announcements• Exam 1 – Do not discuss until with anyone until

handed back

• APT Quiz 1 out today

• Do by yourself

• Assignment P3 out Friday – due 2/27

• Builds on P2 Markov

2/19/2020 CompSci 201, Spring 2020 3

Page 4: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

PFWAE1

• Quick Review of Maps

• Linked List from high-level to low-level• Similar to how we viewed ArrayList/array• Low-level linked lists have history and current

pedigree

• Iterators, Interfaces, Idioms• From design patterns to APIs

• APT Quiz ready today, Exam 1 not graded yet

2/19/2020 CompSci 201, Spring 2020 4

Page 5: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

The java.util.Map interface, concepts

• HashMap <Key,Value> or <K,V

Method return purpose

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

2/19/2020 CompSci 201, Spring 2020 5

Page 6: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

2/19/2020 CompSci 201, Spring 2020 6

Page 7: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Investigate Map Solution• One pass over the data instead of many passes

• .Understand all map methods

• Why is line 39 never executed? Still needed?

2/19/2020 CompSci 201, Spring 2020 7

Page 8: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

What is a java.util.List in Java?

• Interface for collection of elements• Add, remove, traverse, …• What can a list do to itself?• What can we do to a list?

• Why more than one kind of list: Array and Linked?• Useful in different applications• How do we analyze differences?• How do we use them in code?

2/19/2020 CompSci 201, Spring 2020 9

Page 9: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Remember? list.remove(0)

2/19/2020 CompSci 201, Spring 2020 10

• What is “faster”? LinkedList or ArrayList

y = -4E-05x + 0.0009

y = 0.0064x2 - 0.0156x + 0.0238R² = 0.9984

0

0.2

0.4

0.6

0.8

1

1.2

1.4

RemoveFirst

linked array Linear (linked) Poly. (array)

Page 10: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

ArrayList remove(0) is O(N)

• Must shift N-1 elements

• Details in code below? Some matter, some …• Shifting N elements is O(N2): why?

2/19/2020 CompSci 201, Spring 2020 11

Page 11: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Random Access v Splicing…

• How does find-a-track work? Fast forward?

• Quick survey of linked list code

• http://bit.ly/201playRecord

2/19/2020 CompSci 201, Spring 2020 12

Page 12: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Conceptual: array[] to Node

• How do we implement ArrayList? Use array[]• list.get(n) is O(1), BUT• list.remove(0) is O(N)

• How do we implement LinkedList? Use Node• list.get(n) is O(n), BUT• list.remove(0) is O(1)

• Tradeoffs: what does sequence of nodes provide?

2/19/2020 CompSci 201, Spring 2020 13

Page 13: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

What’s in a Node?

• Some information

• Place to snap another node

• In Java we’ll see

• String reference: info

• Node reference: next

2/19/2020 CompSci 201, Spring 2020 14

Page 14: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Visualizing/Understanding Nodes

• https://coursework.cs.duke.edu/rodger/diyad-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

Page 15: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

remove(0) for linked list?

• Looking at remove(0) not 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

Page 16: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Adding nodes to end:.add(..)

• Class invariant: myLast references last node

• Symmetry: myFirst references first node

• When the list is empty, special case?

• Always add node to end of list

2/19/2020 CompSci 201, Spring 2020 17

Page 17: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Adding New Nodes

• To add to the end of a linked 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 need initialization code

• First node anchors list• Must do before loop

• Loop will add over and over to end

2/19/2020 CompSci 201, Spring 2020 18

Page 18: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Visualizing, Thinking, Understanding

• How to picture myLast and myLast.next

• Both are Node pointers aka Node references

• Like all Java Object variables: memory location

• Conceptually? An arrow, a pointer

• References a Node: label for memory location

2/19/2020 CompSci 201, Spring 2020 19

myFirst myLast

Page 19: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Only one node in the list? myFirst? myLast?

• a

2/19/2020 CompSci 201, Spring 2020 20

myFirstmyLast

Page 20: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Another program for understanding• Not modeling a List class, just plain Nodes

• 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 invariant

2/19/2020 CompSci 201, Spring 2020 22

Page 21: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Main in LowLevelLinkDemo.java

2/19/2020 CompSci 201, Spring 2020 23

Page 22: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Visualizing Code – CreateListAdd nodes to end of list

• Using Java Tutor:

• See first and last: both Node variables

2/19/2020 CompSci 201, Spring 2020 24

Page 23: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Adding first node to linked list

• Repeatedly add first element, initially 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

Page 24: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Visualizing Code – CreateListFrontAdd nodes to front of list

• Using Java Tutor:

• See first and last: both Node variables

2/19/2020 CompSci 201, Spring 2020 26

Page 25: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Reference: Array Traversal

• Visiting (printing) every value in an array

• Initialize index, print w/index, increment index

• Elements of array are adjacent in memory

2/19/2020 CompSci 201, Spring 2020 27

Page 26: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

List Traversal

• Visiting (printing) every value in an array

• Start with first node, print .info, advance .next

• Done when current node is null

2/19/2020 CompSci 201, Spring 2020 28

Page 27: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

WOTO (correctness counts)

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

You can install Java Tutor in IntelliJ – see course website Resources tab

2/19/2020 CompSci 201, Spring 2020 29

Page 28: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

John Tukey: 1915-2000• Cooley-Tukey FFT

• Bit is a binary digit• Box or Box and Whiskers Plots

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.

Page 29: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Removing some values: filter

• Remove all occurrences of X, or …

• When we remove in array, we shift. Trouble?

• ListRemoveAndCount: exception thrown!• ConcurrentModificationException

2/19/2020 CompSci 201, Spring 2020 31

Page 30: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Incorrect Results

• See ListRemoveAndCount.java

• You shouldn’t do this: results in errors

• Remove the kth element (think 0)

2/19/2020 CompSci 201, Spring 2020 32

Page 31: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Iterators to the Rescue

• Iterators are soooo nice. But timing?• Why O(N) linked list and O(N2) array?

2/19/2020 CompSci 201, Spring 2020 33

Page 32: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

From Iterator to Iterable

• Enhanced for: for(String s : list) { …

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

2/19/2020 CompSci 201, Spring 2020 34

Page 33: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

From Iterator to Iterable

• What if indexing loop used?, • e.g., list.get(k)

• Code below is ?

2/19/2020 CompSci 201, Spring 2020 35

Page 34: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

Compare the two

• ListSplicer.java

2/19/2020 CompSci 201, Spring 2020 36

Page 35: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

WOTO

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

2/19/2020 CompSci 201, Spring 2020 38

Page 36: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

APT Practice Quiz

• APT Practice Quiz is on Sakai now

• NOT FOR CREDIT, Just for practice

• You can see how an APT quiz works

• Only Available through Sunday 11:59pm

• RECOMMEND trying APT Practice Quiz before taking the APT Quiz1

2/19/2020 CompSci 201, Spring 2020 39

Page 37: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

APT Quiz Details

• APT Quiz 1 available on Sakai• Wed. Feb 19 at 8pm – Mon. Feb 24 11:59pm

• Once you start, you get 2.5 hours• You cannot stop and restart it. • More time if you get accommodations

• Must start by 9:29pm Monday night!• Recommend you take it BEFORE Monday

• You can see the timer in Sakai• We will not grade anything you submit after time runs

out

• You CANNOT, CANNOT, CANNOT collaborate on the quiz. We run reasonably sophisticated similarity detection software

2/19/2020 CompSci 201, Spring 2020 40

Page 38: Compsci 201 Maps and Linked Lists - Duke University · • Linked List from high -level to low -level • Similar to how we viewed ArrayList/array • Low-level linked lists have

APT Quiz

• We expect that everyone will get the first problem

• Sometimes we are wrong. But it’s designed to be straightforward. If you’ve done the APTs? You’ll succeed

• We expect everyone will know how to solve the other problems, but sometimes coding and debugging is not easy

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

2/19/2020 CompSci 201, Spring 2020 41


Recommended