10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the...

Post on 18-Mar-2020

0 views 0 download

Transcript of 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the...

10/10/2018

Data Structure & Algorithm

1

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

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

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

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

Three examples

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

5

Break

6

Review - Iterable

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

7

Three java files for demo

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

8

Break

9

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

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

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

Last class: Interface Map and Abstract Class AbstractMap

13

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

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

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

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

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

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