10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the...
Transcript of 10/10/2018 Data Structure & Algorithm · Please read the above page and watch the videos on the...
![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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042021/5e78bb51f774b03d935019a8/html5/thumbnails/18.jpg)
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