2015 DS Labs by: Prof. J. Chen 2015.09.14 LabArrayList

advertisement
2015 DS Labs
by: Prof. J. Chen
2015.09.14
LabArrayList
LabArrayList&LinkedList
LabRedBlackTree
LabTreeSet
LabGraph (To be done)
LabCompare4DS
Instructions on the Labs:
The grading is based on READABILITY. You need good header, pseudo-code,
and names.
LabArrayList
Insert ("add" in ArrayList) one million randomly-generated grades from 0 to 100 to anArrayList.
Then, search for the max grade in the list, and record the search time.
If there are multiple answers, return one at the highest index.
You can handle exceptions to get bonus point.
Use the following data names:
aGrade
- a grade to be inserted to an ArrayList.
anArrayList - an ArrayList object to hold the inserted grades.
maxGrade - the max grade in the ArrayList.
endTime - ending time of the search.
searchTime - endTime minus startTime.
startTime - start time of the search.
/*------------------------------------------------------------------------------prepareGrades () puts all the grades in anArrayList
returns: anArrayList
-----------------------------------------------------------------------------------*/
1. randomly generate aGrade (0-100)
2. insert aGrade into anArrayList
3. do 1,2 for one million times
4. return anArrayList
/*-----------------------------------------------------------------------------------------searchMaxGrade () looks for max grade in anArrayList
Example result:
The max grade is 100 and the search time is 12 milliseconds for ArrayList.
-------------------------------------------------------------------------------------------*/
1. record startTime.
2. let the first grade be maxGrade
3. if the next grade >= maxGrade then let it be maxGrade
4. do 2,3 for one million times
5. record endTime.
6. get searchTime.
7. Print maxGrade and searchTime
LabArrayList&LinkedList
Add LinkedList to the previous lab.
/*------------------------------------------------------------------------------prepareGradesInArrayList () puts all the grades in anArrayList
returns: anArrayList, which is an object of ArrayList class
-----------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------prepareGradesInLinkedList () puts all the grades in aLinkedList
returns: aLinkedList, which is an object of LinkedList class
-----------------------------------------------------------------------------------*/
/*------------------------------------------------------------------searchMaxGrade (whichDS) looks for the max grade in anArrayList or aLinkedList
parameter: whichDS means which data structure (DS), either “anArrayList” or
“aLinkedList”
Example result:
The max grade is 100 and the search time is 12 milliseconds for ArrayList.
OR
The max grade is 100 and the search time is 19 milliseconds for LinkedList.
----------------------------------------------------------------------*/
LabRedBlackTree
Study the class notes of Chap. 12, pp. 19-25 to develop the red-black tree insert
method specified below:
/*--------------------------------------------------------------------red black trees (RBT) insert() puts anElement into the tree
----------------------------------------------------------------------*/
RBT.Entry RBTinsert (anElement)
In that method, you need binary search tree (BST) “add” method, BSTinsert() (see
class note Chap. 10, p.35). You also need “rotation” method (see p.80, pp. 67-81 of
Chap. 10).
Turn in 2 classes:
1) a Main class with main (), and
2) a RBT class with 7 methods:
RBT() constructor,
RBTinsert (),
BSTinsert(),
rotateRight(),
rotateLeft(),
recolor(x)
inOrder () (See class note Chap. 9, p.71).
Be sure to have readable headers for class and method.
You can paste design sketches in class notes to improve its readability.
Most importantly, give Big O worst time estimate for each method.
Instructions on LabRedBlackTree
Strictly follow the design below:
main ()
1. call RBT() to construct an empty red-black tree called “t”
2. call the above “RBTinsert” 4 times to insert 3, 1, 4, and 2 to “t”,
respectively
3. call a recursive “inOrder” method to traverse “t” to print out the
followings:
1 black 2 red 3 black 4 black
public class RBT
//red-black trees (RBT)
private static final boolean RED
= false;
private static final boolean BLACK =true;
public class Entry <E> { fill in here the code of class Entry shown below}
public inOrder ()
public RBT () {Entry <Integer> t=null; return t;}
public RBTinsert ()
private recolor(x) {if color of x is red, recolor it to black; else to red end if}
private rotateRight();
private rotateLeft();
private BSTinsert(); //binary search trees (BST) insert
end of class RBT
protected static class Entry<E>
{
protected E
element;
protected Entry<E> left
= null,
right = null,
parent;
boolean color;
protected Entry (E element, Entry<E> parent)
{this.element = element; this.parent = parent; color=RED;}
} // end of class Entry
LabTreeSet
First, you import the two utilities below:
import java.util.Iterator;
import java.util.TreeSet;
and you construct an empty TreeSet called “t”. Then, you call “add” of TreeSet class 4
times to insert 3, 1, 4, and 2 to “t”, respectively. Last, you construct an iterator to
traverse “t” to print out the followings:
1 2 3 4
LabGraph
For the graph below, use 1) TreeMap and 2) HashMap, respectively, to model it.
Scenario 1: From Karen to Don is 7.4 km
Scenario 2: From Karen to Tara is 18.3 km
Scenario 3: From Mark to Karen Sorry, you cannot get there.
LabCompare4DS
Use 4 data structures (4DS) below:
0)
1)
2)
3)
“ArrayList”,
“LinkedList”,
“TreeMap”,
“HashMap”
to store a big data set of one million elements with ID from 0 to 999,999.
In a list, an element contains a grade.
In a map, a mapping contains both ID (as key) and grade (as value).
Use Java Random class to obtain a grade so you do not need to prepare a big input
text file. Then, do run-time analysis on the data structures.
You will see some data structure is slower than others!
/****************************************************
LAB Compare 4 DS
By 張一二 2015.09.13
隨機產生一百萬筆 aGrade 資料 每筆資料分別存入下列四種資料結構:
0) anArrayList,
1)
aLinkedList,
aTreeMap,
3) aHashMap
2)
Example result:
請輸入ID 400000 (red means user input)
ID: 400000
Grade: 91
各資料結構搜尋時間:
ArrayList
80 milliseconds
LinkedList
92 milliseconds
TreeMap
8 milliseconds
HashMap
2 milliseconds
*****************************************************/
class Main
static int[4]
grades
// hold aGrade of ID for 4 data structures.
static double[4] searchTimes // hold searchTime of 4 data structures
main() {
1.
construct 4 empty data structures: 0) anArrayList, 1) aLinkedList, 2) aTreeMap, and
3) aHashMap with n= 1000000, m=1250000 (load factor: 1000000/1250000 = 0.8)
2.
for ID from 0 upto 999,999
1.
randomly generate a grade (aGrade) with range 0..100
2.
store aGrade into 0) anArrayList and 1) aLinkedList
3.
store ID (as key) and aGrade (as value) into 2) aTreeMap and 3) aHashMap
end for
3.
for DSindex from 0 upto 3 (for each of 4 DS, data structures)
call search (DSindex, 400000)
end for
4.
prompt for ID and print result
} // end of main()
/*--------------------------------------------------------------------search() looks for the grade of the given ID stored in 4 data structures.
It also records the search time for each.
Parameters: DSindex
ID
specifies which of the 4 data structures (0 to 3)
specifies the student ID to search
--------------------------------------------------------------------------------*/
private void search (int DSindex, int ID)
1.get startTime by system clock
2.case (DSindex)
0: from anArrayList get aGrade of ID
1: from aLinkedList
do the same
2: from aTreeMap
do the same
3: from aHashMap
do the same
3.get endTime by system clock
4.searchTime is endTime minus startTime
5.store searchTime into searchTimes[DSindex]
6.store aGrade into grades[DSindex]
end of search()
end of class Main
Download