Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS2230 CSII:Datastructures Meeting28:SortedListMap BrandonMyers UniversityofIowa Today’sbigideas • SortedMap isanextensionofMapthatincludesadditional methodsthatdependonkeyshavinganordering • TwoapproachestoimplementingSortedMap arewith sortedlistsorsearchtrees;we’lllookatSortedListMap Helpfultohavethecodeinfrontofyoutoday:Goto4/3lecture materialsonthewebsitetoaccesstheJavacode DictionaryApp.java Input:Textfilewith”WORD”:”definition” {"DIPLOBLASTIC":"Characterizing theovumwhenithastwoprimary germinallayers.", "DEFIGURE":"To delineate.[Obs.]Thesetwostonesastheyareheredefigured. Weever.", "LOMBARD":"Of orpertainingtoLombardy,ortheinhabitantsofLombardy.", "BAHAISM":"The religioustenetsorpracticesoftheBahais.", "FUMERELL":"See Femerell.", "ROYALET":"Apettyorpowerlessking.[R.]therewereatthistimetwoother royalets,asonlykingsbyhisleave.Fuller.", "TROPHIED":"Adorned withtrophies.The trophied arches,storiedhalls,invade. Pope.",...} illustrationofaSortedMap put(word,def) into SortedMap querythe SortedMap,e.g., firstEntry() A Thefirstletterofthe English... key value A ThefirstletteroftheEnglish... ACHEVAL Astride;withapartoneachside... AFORTIORI Withstrongerreason. ... ... ZYTHEPSARY Abrewery. SortedMap.java Thisinterfaceextends Map,soaSortedMap cando everythingaMapcando get, put, remove, ... inadditiontomethodsthattakeadvantageofsome orderingontheKeys SortedMap classhierarchy Map SortedMap AbstractSort AbstractMap edMap HashMap BSTMap aka,AVLTreeMap UnsortedListMap SortedListMap AbstractSortedMap.java Providesmethods/constructorsthatareprobably usefulformanyimplementationsofSortedMap protected AbstractSortedMap(Comparator<K> c) { comp = c; TheComparator<K>interface } providesthe int compare(K o1, K o2) method /** Initializes the map with a default comparator. */ protected AbstractSortedMap() { // default comparator uses natural ordering this(new DefaultComparator<K>()); } Anorderingonthekeysneedstobedefined.AbstractSortedMap providestwoconstructorsthatallowyoutodefinethatordering. SortedMap classhierarchy Map SortedMap AbstractSort AbstractMap edMap HashMap BSTMap aka,AVLTreeMap UnsortedListMap SortedListMap SortedListMap<Integer,String> entries ArrayList<MapEntry<Integer,String>> elements count 2 MapEntry<Integer, String> k "A" v "The first letter ..." MapEntry<Integer, String> k "A CHEVAL" v "Astride; with a..." Peerinstruction https://b.socrative.com/login/student/ CS2230Aids1000-4999 CS2230Bids5000+ Whatrunningtimeshouldweaimforfor SortedListMap.get()? a) b) c) d) e) O(1) O(logn) O(n) O(nlogn) O(n^2) BONUS:runningtimefor... put ceiling(key) floor(key) higherEntry(key) lastEntry() IteratingthroughtheresultofsubMap SortedListMap.java • put(k,v) • step1:findtheindex,j,wherekwouldgoinsorted order • step2: • ifentries[j]alreadyholdsthekeyk,thenoverwritethevalueof thatentrywithv • otherwise,addanewentry(k,v)atindexj.TheArrayList.add methodmustcopyeverythingaftertheinsertionpoint! • runningtime • step1:O(logn)withbinarysearch • step2:O(1)inthefirstcase,O(n)todothecopyinthesecond case;thereforeworstcaseO(n) • thereforeputisworstcaseO(n) SortedListMap.java • higherEntry(k) • step1:findtheindex,j,wherekwouldgoinsorted order • step2: • ifentries[j]holdsthekeyk,thenreturnentries[j+1]ifitexists • otherwise,returnentries[j],whichmusthavealargerkeythan k • runningtime • step1:O(logn)withbinarysearch • step2:O(1)inbothcases • thereforehigherEntryisO(logn) SortedListMap.java subMap(“Do”,“Egg”)returnsasnapshotiterable MapEntry<Integer, String> "A" MapEntry<Integer, String> "The first letter ..." k v Currentindex buffer 0 "Door" "A covering for..." MapEntry<Integer, String> k v "Dog" "A life form..." MapEntry<Integer, String> MapEntry<Integer, String> k "Turtle" k v "A shelled..." v "Zebra" "Known to have..." Copytheportionofthearraywherethekeys fallinrange“Do”to“Egg”into abuffer(the“snapshot”) Why?ourcopywillbesafe.Removesremovesonthe map thatoccurbeforewereturnallelementsfromthe iteratorwon’taffecttheiterator (however,sincewecopiedonlyreferences,puts that overwriteentrieswill beseen) SortedListMap.java subMap runtime? Let’sassumen=numberofentriesands=numberof entriesreturnedinthesubMap. • 𝑂(log𝑛)tofindthestartkeyusingbinarysearch • 𝑂(𝑠)toiteratefromthestartkeytothefirstkey thatistoobig therefore,subMap is𝑂(𝑠 + 𝑙𝑜𝑔𝑛) Administrivia HW7out,dueon4/13 SeeannouncementonICONforsigningupHW8 partners(pairprogrammingproject) UsingversioncontrolinHW8 Git Youwillgetexperienceinmanagingversionsofyour codeandsharingyourcodewithapartnerinHW8. Nextweek’sdiscussion(4/10-4/12)isacrashcourse ongit.Bringyourlaptopwithgit readytogo! (instructionsforthcomingonICON) Today’sbigideas • SortedMap isanextensionofMapthatincludesadditional methodsthatdependonkeyshavinganordering • TwoapproachestoimplementingSortedMap arewith sortedlistsorsearchtrees;we’lllookatSortedListMap Helpfultohavethecodeinfrontofyoutoday:Goto4/3lecture materialsonthewebsitetoaccesstheJavacode resources • Forananimationofbinarysearchsee http://www.cs.usfca.edu/~galles/visualization/Sear ch.html