10/10/2018 data structure & algorithm · please read the above page and watch the videos on the...

18
10/10/2018 Data Structure & Algorithm 1 Review: iterable/iterator Review: array-implementation of a tree Map, Hash table

Upload: others

Post on 18-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

10/10/2018

Data Structure & Algorithm

1

• Review: iterable/iterator• Review: array-implementation of a tree• Map, Hash table

Page 2: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Warning!!! Error in the assignment 06

2

/* todo: fill this method

* private utility:

* find the entry in the table with equal key,

* or, null if none found */

private int findIndex(Integer k){

return -1; // replace this line with your

implementation

}

should be -1 INSTEAD OF null

Page 3: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Review: Iterator/Iterable

Reference: http://www.cs.cornell.edu/courses/JavaAndDS/iteratorIterable/iterator.html

Please read the above page and watch the videos on the page. A noteabout enumeration on the reference page:

enumeration ≈ iteration. A technical detail (not important for this course): in iteration, you can remove an element from the collection; in enumeration, you cannot. This course doesn’t cover the “remove” operation of iteration, so we can say iteration is “kind of” enumeration.

3

click this on the reference page to watch a video

Page 4: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Review: Iterator

Iterator

An iterator over some collection provides methods that help you write a loop to enumerate and process the elements in that collection. An iterator has two methods:

1. hasNext(): is there another element to enumerate?

2. next(): return the next element to enumerate – return null if there is no more (in our course)

4

Page 5: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Three examples

https://sjiang1.github.io/classes/algorithms/18fall/09/IteratorDemos.zip

5

Page 6: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Break

6

Page 7: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Review - Iterable

Benefit: implementing this interface allows an object to be the target of the “for-each loop” statement.

7

Page 8: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Three java files for demo

https://sjiang1.github.io/classes/algorithms/18fall/09/IterableDemos.zip

8

Page 9: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Break

9

Page 10: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Quiz Example Question (Array-implementation)

Given the following tree, suppose this tree is stored in an array.

Q1: What is the minimum length of the array for storing the tree?

Q2: What is the index of the element (in this array) stores Node X?

10https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html

X

Q1: 22Q2: 9

Page 11: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Quiz Example Question (Array-implementation)

Given the following array, what will the tree look like?

11

a c NULL d k NULL NULL NULL z m

Page 12: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Last class: Map

A map is an abstract data type for storing and retrieving values based upon a uniquely identifying search key for each.

An entry is a pair of (key, value)

12

Page 13: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Last class: Interface Map and Abstract Class AbstractMap

13

https://sjiang1.github.io/classes/algorithms/18fall/09/Map_ClassDiagram.pdf

Page 14: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Intro to Hash Tables

• Besides UnsortedTableMap, and SortedTableMap, we will discuss another additional implementations:• lookup table

• hash table (* important)

• A lookup table: an array, in which every element maps to a value. The key for the value is index of the element.

• Hash table: a bucket array

14

Page 15: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

The disadvantages of a lookup table

• Problem 1: if we have keys ranging from 0 to 1000000000, and we only have 10 keys at a time

• Problem 2: if the keys are not integers

15

Page 16: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Assignment 06 – assignment part 1 (2 points)due Monday, Oct. 15, Noonimplement a class SortedTableMap<K, V> extends AbstractMap<K,V> so that (1) its storage for entries is an ArrayList; (2) the ArrayList (its variable name is table) is always sorted after an invocation of V put(K key, V value)

Github link: https://classroom.github.com/a/mRZk3J_Y

22

Page 17: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

Assignment 06 – assignment part 1 (2 points)Grading rules, due Monday, Oct. 15, NoonAt least 0.5: if you have filled all the methods in SortedTableMap

At most 1.5: if the output of main method is not correct

-0.1~-0.3: if there is a bug depending on the severity.

23

Page 18: 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the page. A note about enumeration on the reference page: enumeration ≈iteration. A technical

There is only one part for this week’s assignmentThe second part is removed so that you can spend most of your time in learning Iterator/Iterable.

24