Lectures 9-1, 9-2. Collections. Introduction to Computer Science, Shimon Schocken slide 1

Similar documents
Lectures 11-1, Polymorphism. Introduction to Computer Science, Shimon Schocken slide 1

Multi HLR Architecture for Improving Location Management in PCS Network

12d Solutions Pty Ltd CIVIL AND SURVEYING SOFTWARE

KCFitnessLink School of Yoga & Holistic Health Yoga Hybrid Bridge Program Supplemental Application

Patient Information Sheet

Advertising packages 2018

Decision of the FIBAA Certification Committee for Continuing Education Courses

Equity in Athletics 2017 Institution Information

Proposal application form

Weber State University Annual Assessment of Evidence of Learning

H0006 Case Management $ Minute Increment 1-28 Units per month

AETC Philosophy Future Requirements Combat Systems Officer Training Remotely Piloted Aircraft Training Future of Pilot Training

CITY OF SAINT PAUL FUNDING & TECHNICAL RESOURCES FOR BUSINESSES

Analysis of Results of the Rating of Volunteer Distributed Computing Projects

The Air Force was born of

Arthropod. Fecal-oral. Exposure/exertion. Hepatitis Hepatitis B. Sexually transmitted. Page 24. MSMR Vol. 19 No. 4 April Lyme disease.

Commissioning for Value Where to Look pack

National Committee for Quality Assurance

ASSOCIATION OF SENIOR LIVING INDIA CODE OF PRACTICE

TABC TODAY. New web page lists election status by county

YOUTH SUCCESS EARLY LITERACY JULY 2016 DOING GOOD IS GOOD BUSINESS. Serving DeSoto, Hillsborough, Pinellas and Sarasota counties FINANCIAL STABILITY

Undergraduate Student Workbook

HRHCare Family Nurse Practitioner Residency Training Program Application

WORLD CUSTOMS ORGANIZATION ORGANISATION MONDIALE DES DOUANES 11FL-00289E/S.R. Brussels, 7 October Dear Director General,

Counterdrug(CD) Information Brief LTC TACKETT

The provision of same-day care in general practice: an observational study

LIBRARY AMENITIES QUICK REFERENCE DUPONT - BALL LIBRARY STETSON.EDU /LIBRARY. dupont-ball Library 421 N. Woodland Blvd., Unit 8418 DeLand, FL 32723

Reimbursement for Blood Products and Related Services in 2017

UC MERCED EXIT DISCOVERING YOUR JOURNEY TO SUCCESS NEXT EXIT

CITY OF PEMBROKE COMMUNITY IMPROVEMENT PLAN FINAL

Schedule A Monetary Contributions Received

PER DIEM, TRAVEL AND TRANSPORTATION ALLOWANCE COMMITTEE 4800 Mark Center Drive, Suite 04J2501 Alexandria, VA

colorado fresh food financing fund CO4F Executive Summary

Rapportens tittel. Norway Rapport xx Rapport

Space Object Re entry. State Leadership Briefing DHS/FEMA Region IX

THE REPEATER. Warrensburg Area Amateur Radio Club, Inc. Volume 18, Issue 2 March / April 2013

NAVAL MINES INTRODUCTION: EOD CONSIDERATIONS CONTACT MINES: SAFETY FOR OFFICIAL USE ONLY FOR OFFICIAL USE ONLY

Table of Contents. What is Branding and Why is it Important? The Shield of the College Selecting the Correct File Format...

Maneuver Center of Excellence (MCoE) Libraries HQ Donovan Research Library Armor Research Library Fort Benning, Georgia

National Disaster Medical System (NDMS) Department of Health and Human Services

Award Cash Management $ervice (ACM$) National Science Foundation Regional Grants Conference. June 23 24, 2014

i' ~t to CIJ~ ~lb ">9""

UNCLASSIFIED. FY 2014 FY 2014 OCO ## Total FY 2015 FY 2016 FY 2017 FY 2018

THE CITY COUNCIL'S RULES OF DECORUM WILL BE ENFORCED. Name of City Agency, Department, Committee or Council -

FY15 Rural Health Care Services Outreach Funding Opportunity Announcement (FOA) HRSA Technical Assistance Webinar for SORHs

Current and Emerging Rural Issues in Medicare

A HISTORY OF RADIO A RADIO INTERVIEW. Teacher s notes 1 ARTS AND MEDIA

As organizations strive to improve

MJMS at the Dawn of Its Electronic Era

Office of the Inspector General. 20th & Constitution Avenue, NW Washington, DC Fax: (202) Online FOIA Request Form

State of Florida. Department of Economic Opportunity. One Stop Management Information System (OSMIS) Regional Financial Management User Manual

CITIZENS SERVING COMMUNITIES

Policy Research Corporation

MEDICAL SURVEILLANCE MONTHLY REPORT

Graduate Division Award Entry

JMNO GOSC Overview. 13 July Col Kavin Kowis, USMC Joint Test Director

Department of Homeland Security

Bylaw No OFF-SITE LEVY BYLAW

course booklet-mumbai

Innovation valorisation Linking Research and Business the AFI MAC & MMDIP experiences

Reports. Options for Joint Stiffness By Dr Greg Pursley INSIDE NOV - DEC Carbon Monoxide

MARKET INSIGHT INDIANAPOLIS MULTIFAMILY REPORT FOURTH QUARTER 2017

Army Aviation and Missile Command (AMCOM) Corrosion Program Update. Steven F. Carr Corrosion Program Manager

Subcontracting Tools. First Wednesday Virtual Learning Series 2018

Excellence in Energy for the Tourism Industry Accommodation Sector: SME Hotels

F. No. 6-4-Al2012-EE-6(MDM-3-1) Government of India. Ministry of Human Resource Development,

The For-Profitization of Affordable Housing Development and the de Blasio Plan

Quincy Riley Assessment Manager, HUD-REAC. Brian Edwards Auditor/Financial Analyst, HUD-REAC

Postage Chargeback Report

Review of the 2016 Annual Quality and Resource Use Reports. October 19, 2017

General Services Administration

Explanatory document for the Nordic synchronous area proposal for coordination actions aiming to reduce FRCE as defined in Article 152(14) and

American Health Lawyers Association Continuing Education Request Form Annual Meeting June 25-27, 2018

PER DIEM, TRAVEL AND TRANSPORTATION ALLOWANCE COMMITTEE 4800 Mark Center Drive, Suite 04J2501 Alexandria, VA

Developmental screening, referral and linkage to services: Lessons from ABCD

NCHIP and NICS Act Grants Overview and Current Status

National School Safety Conference Reno, Nevada / June 24 29, 2018

NCQA PCMH Recognition: 2017 Standards Preview. Tricia Barrett Vice President, Product Design and Support January 25, 2017

THANK YOU FOR YOUR LOYAL SERVICE!

2017 Keyworker Training Guide Combined Federal Campaign

Building Blocks to Health Workforce Planning: Data Collection and Analysis

Governor s Office of Electronic Health Information (GOEHI) The National Council for Community Behavioral Healthcare

Graduate Division Award Entry

40-Hour Hazardous Waste Worker PARTICIPANT MANUAL

State Title V Health Care Transition Performance Objectives and Strategies: Current Snapshot and Suggestions

OKLAHOMA IMPORTANT NOTICE THIS IS AN IMPORTANT NOTICE TO JOHN D. SAMPLE OF OKLAHOMA. YOU ARE CURRENTLY NOT RECEIVING BENEFITS YOU MAY BE ELIGIBLE FOR.

UNIVERSITY UNDERGRADUATE CURRICULUM COMMISSION MINUTES December 1, 2010

Nurse Advocacy /15/16. Who watches out for your profession, while you take care of patients? Why should you care?

PER DIEM, TRAVEL AND TRANSPORTATION ALLOWANCE COMMITTEE 4800 Mark Center Drive, Suite 04J2501 Alexandria, VA

Cesarean Delivery Model Meeting the challenge to reduce rates of Cesarean delivery

Medicare & Medicaid EHR Incentive Programs Robert Tagalicod, Robert Anthony, and Jessica Kahn HIT Policy Committee January 10, 2012

Sources of funding for A&A education to deliver the vision of Europe 2020

Beau Rivage Resort & Casino June 5-7, 2018 Exhibitor Information

February 26, Dear State Health Official:

Care Provider Demographic Information Update

MEMORANDUM Texas Department of Human Services * Long Term Care/Policy

Commissioning a Community IV service

PDTATAC/dlw 14 December UTD/CTD for MAP (E)/CAP (E) -- Storage of POV During Contingency Operations

Initiative for State Infrastructure Protection (ISIP) Vulnerability Alert Process (VAP) Concept of Operations

Impacts of Evidence-Based Quality Improvement on Depression in Primary Care

Transcription:

Lecture 9-1, 9-2 Collection Introduction to Comuter Science, Shimon Schocken lide 1 Dt tructure / collection In comuter cience, dt tructure i collection chrcterized by n rchitecture nd behvior. Some common dt tructure: 0 1 2 4 Arry Lit out Stck out Queue Tree Grh Or dt tructure In Jv termology, dt tructure re ometime clled collection The Jv collection frmework: librry cle tht imlement commonly ued dt tructure. Introduction to Comuter Science, Shimon Schocken lide 2 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

The collection cn be object ny tye collection Arry 0 1 2 4 Integer Peron Stck Grh etc. Mtri etc. The cn be object me tye ( homogenou collection ) or multile tye ( heterogeneou collection ) But, when we tlk bout collection we don t cre much bout dividul ; rr, we focu on collection whole Poible ource confuion: The re object The collection itelf i n object. Introduction to Comuter Science, Shimon Schocken lide Tyicl oertion Collection n btrction tht uort cert oertion: Fd n Add n Delete n Iterte through Etc. Dferent collection erform dferent ub e oertion, with dferent degree efficiency nd cot Thu, dferent collection re uitble for dferent liction need Collection come mny vrt: Arry: 1-,2-, n-dimenionl, Lked lit: gly-connected, doubly connected, Tree: bry, n-ry, Grh: directionl, undirectionl, 0 1 2 4 Introduction to Comuter Science, Shimon Schocken lide 4 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

The btrction / imlementtion erly A uul, we ditguih between btrction nd imlementtion 0 1 2 4 Abtrction: vriou oertion tht collection uort ( functionl, client-oriented view) Imlementtion: how collection uort e oertion ( behd cene, develoer-oriented view) Collection re imlemented ug two m techniue: Arry Lked lit. Introduction to Comuter Science, Shimon Schocken lide Outle Dt tructure / collection Stck Lit Hhg Methodology Decribe collection (btrction) Illutrte liction tht ue it Show how collection cn be relized (imlementtion). Introduction to Comuter Science, Shimon Schocken lide 6 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Rule gme: The order i ignicnt: 2,, 4 4, 2, Dulicte re not llowed The oertion: Crete n emty Inert n Delete n Check given eit Check i emty Iterte through Perform -oriented oertion: Interection Union Dference etc. For imlicity, we focu on eger. Client code ; ;.ert(2);.ert(2);.ert(1);.ert(1);.ert();.ert();.ert(1);.ert(1);.ert();.ert();.ert(4);.ert(4); Sytem.out.rln; Sytem.out.rln;.delete();.delete();.delete(7);.delete(7);.delete(2);.delete(2); Sytem.out.rln; Sytem.out.rln; Introduction to Comuter Science, Shimon Schocken lide 7 btrction cl cl Contruct Contruct Check Check cont cont boolen boolen cont cont ( ( ) ) Inert Inert to to ert ert ( ( ) ) Delete Delete from from delete delete ( ( ) ) Inert Inert nor nor o o ert ert ( ( ) ) Return Return erection erection nd nd nor nor erection erection ( ( ) ) Return Return union union nd nd nor nor union union ( ( ) ) Return Return tetul tetul re. re. to to Client code ; ;.ert(2);.ert(2);.ert(1);.ert(1);.ert();.ert();.ert(1);.ert(1);.ert();.ert();.ert(4);.ert(4); Sytem.out.rln; Sytem.out.rln;.delete();.delete();.delete(7);.delete(7);.delete(2);.delete(2); Sytem.out.rln; Sytem.out.rln; Introduction to Comuter Science, Shimon Schocken lide 8 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

imlementtion, ug n rry cl cl [] [] element; element; The The element element,, no no rticulr rticulr order order ize; ize; ctul ctul number number rry rry ued ued by by ttic ttic fl fl DEFAULT_SIZE DEFAULT_SIZE 10 10 The The defult defult mimum mimum ize ize Contructor Contructor ( ( msize) msize) element element [msize]; [msize]; ize ize (DEFAULT_SIZE); (DEFAULT_SIZE); More More method method follow. follow. Introduction to Comuter Science, Shimon Schocken lide 9 imlementtion (cont.) cl cl [] [] element; element; ize; ize; boolen boolen cont cont ( ( ) ) for for ( ( e e : : element) element) (e (e ) ) true; true; fle; fle; An efficient imlementtion We ll imrove lter. ert ert ( ( ) ) (!cont) (!cont) element[ize] element[ize] ; ; delete delete ( ( ) ) for for ( ( e e : : element) element) (e (e ) ) element[j] element[j] element[--ize]; element[--ize]; ; ; Introduction to Comuter Science, Shimon Schocken lide 10 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

imlementtion (cont.) cl cl [] [] element; element; ize; ize; ert ert ( ( ) ) ( (!cont!cont ) ) (ize (ize element.length) element.length) reize; reize; element[ize] element[ize] ; ; reize reize [] [] tem tem [2 [2 * * element.length]; element.length]; Sytem.rrycoy(element, Sytem.rrycoy(element, 0, 0, tem, tem, 0, 0, element.length); element.length); element element tem; tem; Thi verion ert hndle overflow It mke grow much needed to ccommodte. Introduction to Comuter Science, Shimon Schocken lide 11 imlementtion (cont.) cl cl [] [] element; element; ize; ize; erection( ) erection (ize); for ( e : element) (.cont(e)) erection.ert(e); erection; ert ( ) for ( e : ) ert(e); union ( ) union (ize.ize); union.ert; union.ert; union; ttic union(, t).union(t); Client code 1 1 ; ; 1.ert(2); 1.ert(2); 1.ert(1); 1.ert(1); 1.ert(); 1.ert(); Sytem.out.rln(1 Sytem.out.rln(1 1); 1); 2 2 ; ; 2.ert(1); 2.ert(1); 2.ert(); 2.ert(); 2.ert(); 2.ert(); 2.ert(7); 2.ert(7); Sytem.out.rln(2 Sytem.out.rln(2 2); 2); Sytem.out.rln(1 Sytem.out.rln(1 erect erect 2 2 1.erection(2)); 1.erection(2)); Sytem.out.rln(1 Sytem.out.rln(1 union union 2 2 1.union(2)); 1.union(2)); Sytem.out.rln(1 Sytem.out.rln(1 union union 2 2.union(1,.union(1, 2)); 2)); Introduction to Comuter Science, Shimon Schocken lide 12 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Immutble We now dicu n immutble vrt tht uort only two oertion: cont nd to The cont imlementtion will run O(log 2 N) time, N beg ize. cl cl Immutble Immutble Contructor Contructor Immutble Immutble ([] ([] element) element) Check Check cont cont boolen boolen cont cont ( ( ) ) Return Return tetul tetul re. re. Client code to to [] [] dt dt 9, 9, 7, 7,,, 4, 4, 2, 2, 1, 1, ; ; Immutble Immutble Immutble(dt); Immutble(dt); Sytem.out.rln; Sytem.out.rln; Introduction to Comuter Science, Shimon Schocken lide 1 Immutble imlementtion An An immutble immutble rereenttion rereenttion cl cl Immutble Immutble The The element element immutble immutble,, ordered ordered from from low low to to high high [] [] element; element; Contruct Contruct n n immutble immutble from from given given rry. rry. The The rry rry doe doe not not hve hve to to be be ordered; ordered; It It i i u u tht tht it it cont cont no no dulicte. dulicte. Immutble([] Immutble([] element) element).element.element [element.length]; [element.length]; Sytem.rrycoy(element, Sytem.rrycoy(element, 0, 0,.element,.element, 0, 0, element.length); element.length); Arry.ort(.element); Arry.ort(.element); Client code [] [] dt dt 9, 9, 7, 7,,, 4, 4, 2, 2, 1, 1, ; ; Immutble Immutble Immutble(dt); Immutble(dt); Sytem.out.rln; Sytem.out.rln; Introduction to Comuter Science, Shimon Schocken lide 14 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Immutble imlementtion: ft fd ug bry erch cl cl Immutble Immutble Check Check i i.. boolen boolen cont cont ( ( ) ) cont(, cont(, 0, 0, element.length element.length - - 1); 1); Fd Fd ug ug bry bry erch erch boolen boolen cont cont ( (,, low, low, high) high) (low (low > > high) high) Client code fle; fle; [] (low (low high) high) / / 2; [] dt dt 9, 9, 7, 7,,, 4, 4, 2, 2, 1, 1, ; ; 2; ( ( element[]) element[]) true; true; Immutble Immutble Immutble(dt); Immutble(dt); ele ele ( ( < < element[]) element[]) Sytem.out.rln; cont(, cont(, low, low, - - 1); Sytem.out.rln; 1); ele ele cont(, cont(, 1, 1, high); Sytem.out.rln(7 high); Sytem.out.rln(7 i i : :.cont(7));.cont(7)); Sytem.out.rln(11 Sytem.out.rln(11 i i : :.cont(11));.cont(11)); Introduction to Comuter Science, Shimon Schocken lide 1 Aide: recurive v. itertive imlementtion cl cl Immutble Immutble Check Check i i.. boolen boolen cont cont ( ( ) ) cont(, cont(, 0, 0, element.length element.length - - 1); 1); Recurive Fd Fd rry rry imlementg imlementg ug ug bry bry erch erch boolen boolen cont cont ( (,, low, low, high) high) (low (low > > high) high) fle; fle; (low (low high) high) / / 2; 2; boolen boolen cont cont ( ( ) ) ( ( element[]) element[]) low low true; true; high high element.length element.length - - 1; 1; ele ele ( ( < < element[]) element[]) while while (low (low < < high) high) cont(, cont(, low, low, - - 1); 1); (low (low high) high) / / 2; 2; ele ele ( ( element[]) element[]) cont(, cont(, 1, 1, high); high); true; true; ( ( < < element[]) element[]) high high - - 1; 1; ele ele low low 1; 1; fle; fle; Itertive Introduction to Comuter Science, Shimon Schocken lide 16 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Outle Dt tructure / collection Stck Lit Hhg Introduction to Comuter Science, Shimon Schocken lide 17 Stck A tck hold n ordered collection with gle entry / eit o re uhed (erted) onto tck to re oed (removed) from tck to crete n emty tck uh uh 7 7 uh 7 uh 8 7 8 uh 2 7 8 2 o ( 2) 7 8 o ( 8) 7 uh 4 7 4 o ( 4) 7 A LIFO (Lt In, Firt Out) tg Stck clicl dt tructure with mny liction comuter cience. Introduction to Comuter Science, Shimon Schocken lide 18 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Stck btrction Stck Stck eger eger cl cl Stck Stck Crete Crete n n emty emty tck tck Stck Stck Inert Inert n n to to tck tck to to uh uh ( ( ) ) Remove Remove n n to to from from tck tck to to o o Check Check i i tck tck i i emty emty boolen boolen iemty iemty Client code Stck Stck tck tck Stck; Stck; tck.uh(10); tck.uh(10); tck.uh(20); tck.uh(20); tck.uh(0); tck.uh(0); tck.uh(40); tck.uh(40); tck.uh(0); tck.uh(0); tck.uh(60); tck.uh(60); Sytem.out.rln(tck); Sytem.out.rln(tck); tck.o; tck.o; Sytem.out.rln(Poed: Sytem.out.rln(Poed: ); ); tck.o; tck.o; Sytem.out.rln(Poed: Sytem.out.rln(Poed: ); ); Sytem.out.rln(tck); Sytem.out.rln(tck); tck.uh(tck.o tck.uh(tck.o tck.o); tck.o); Sytem.out.rln(tck); Sytem.out.rln(tck); Tetul Tetul tck tck decrition decrition to to Introduction to Comuter Science, Shimon Schocken lide 19 Stck imlementtion cl cl Stck Stck [] [] element; element; to; to; fl fl ttic ttic DEFAULT_MAX_SIZE DEFAULT_MAX_SIZE 1 1 Stck Stck ( ( msize) msize) element element [msize]; [msize]; to to Stck Stck (DEFAULT_MAX_SIZE); (DEFAULT_MAX_SIZE); o o element[--to]; element[--to]; Client code Stck Stck tck tck Stck; Stck; tck.uh(10); tck.uh(10); tck.uh(20); tck.uh(20); tck.uh(0); tck.uh(0); tck.uh(40); tck.uh(40); tck.uh(0); tck.uh(0); tck.uh(60); tck.uh(60); Sytem.out.rln(tck); Sytem.out.rln(tck); tck.o; tck.o; Sytem.out.rln(Poed: Sytem.out.rln(Poed: ); ); tck.o; tck.o; Sytem.out.rln(Poed: Sytem.out.rln(Poed: ); ); Sytem.out.rln(tck); Sytem.out.rln(tck); tck.uh(tck.o tck.uh(tck.o tck.o); tck.o); Sytem.out.rln(tck); Sytem.out.rln(tck); uh uh ( ( ) ) element[to]; element[to]; boolen boolen iemty iemty to to to to ; ; for for ( ( j j j j < < to; to; j) j) element[j] element[j] ; ; ; ; Overflow / underflow re not hndled All oertion run O(1) time. Introduction to Comuter Science, Shimon Schocken lide 20 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Aliction emle: tck rithmetic crete n emty tck uh 8 8 uh 8 uh 2 8 2 dd 8 mult 40 o ( 40) uh 10 10 uh 4 10 4 div 2 neg -2 Etc. Stck rithmetic: rule gme Ech rithmetic oertion o it oernd from tck, comute function on m, nd uhe reult onto tck. Introduction to Comuter Science, Shimon Schocken lide 21 Aliction emle: comilg rithmetic ereion Arithmetic nottion: Infi: ( ) * 8 Prefi: * 8 Potfi: 8 * The tck btrction rovide nturl wy to imlement otfi rithmetic. To evlute given rithmetic ereion, we: 1. Rewrite ereion otfi nottion 2. Ue tck to comute it vlue. Introduction to Comuter Science, Shimon Schocken lide 22 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Aliction emle: comilg rithmetic ereion High level code 7 7 * * (4 (4 2) 2) / / ( ( ) ) rer Pre tree 7 * 4 2 / - Code genertor Stck mche code (~Bytecode) tck.uh(7); tck.uh(7); tck.uh(4); tck.uh(4); tck.uh(2) tck.uh(2) tck.dd; tck.dd; tck.mult; tck.mult; tck.uh(); tck.uh(); tck.uh(); tck.uh(); tck.ub; tck.ub; tck.div; tck.div; VM imlementtion How Jv comiler hndle rithmetic ereion: 1. Prer: tke high-level ereion (ut) nd crete re tree (outut) 2. Code genertor: tke re tree (ut) nd crete otfi tck code (outut) Techniue: deth-firt trverl re tree, genertg code on fly. Introduction to Comuter Science, Shimon Schocken lide 2 Emle VM imlementtion Pong gme ction Introduction to Comuter Science, Shimon Schocken lide 24 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Outle Dt tructure / collection Stck Lit Hhg Introduction to Comuter Science, Shimon Schocken lide 2 Lit Architecture A lit i euence, e.g. (12, 4,, 61, 17, ) Ech h oition, trtg from 1. So, i rd lit lit re id to be ucceor nd redeceor ech or Oertion: Fd loction n : fd(61) 4 Inert n : ert(,) turn lit o (12, 4,,, 61, 17, ) Remove n : remove(2) turn lit o (12,,, 61, 17, ) PrLit MkeEmty Or imilr oertion, itution reuire; The ect nture nd nmg e oertion vrie from one liction to nor. Introduction to Comuter Science, Shimon Schocken lide 26 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Imlementtion A lit cn be imlemented two m wy: Arry Lked lit Arry imlementtion Simle: lit i tored ufficiently lrge rry Scnng lit cn be done time O(N) which i good cn be eected Fdg n tke O(N) ce lit i not orted Inertion / deletion re eenive, ce ce mut be rrnged to mke room or cloe g to ccommodte chnge. Thi reuire coyg, which i O(N). Lked lit imlementtion Inted torg lit contiguou rry, we ut dferent lce memory nd fcilitte direct cce from ech to it ucceor The reult: more efficient nd elegnt imlementtion. Introduction to Comuter Science, Shimon Schocken lide 27 Lked lit Quite ten, n object h to o to or object me tye: A web ge og to web ge tht it mention A eron og to eron who re her friend A country og to or countrie with which it border An tom og to or tom me molecule A word og to net word tet An emloyee og to hi bo Etc. Such reltionhi cn be mnged effectively ug dynmic dt tructure uch : Lit Tree Grh Introduction to Comuter Science, Shimon Schocken lide 28 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Lked lit liction emle: word roceg t It w bet time Editor wdow: It It w w bet bet time time Delete firt two word: t It w bet time Editor wdow: bet bet time time Relce bet with wort : t wort It w bet time Editor wdow: wort wort time time Introduction to Comuter Science, Shimon Schocken lide 29 Lked lit liction emle: word roceg Seciction: A word conit dt ( word itelf) nd reference to net word. It b e w c f d bet btrction / API cl cl Contruct Contruct word word ( ( dt) dt) Aign Aign net net word word to to word word Net Net ( ( w) w) Return Return vlue vlue word word getdt getdt Return Return net net word word net net Client code (It); (It); b b (w); (w); c c ; ; d d (bet); (bet);.net(b);.net(b); b.net(c); b.net(c); c.net(d); c.net(d); d.net(null); d.net(null); e e.net;.net; f f.net.net;.net.net; tt tt b.net.getdt b.net.getdt Comment: A we ll ee hortly, re re more elegnt nd owerful wy to crete nd mnge lit. Thi i jut relimry emle ue reference. Introduction to Comuter Science, Shimon Schocken lide 0 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

cl Imlementtion Seciction: A word conit dt ( word itelf) nd reference to net word It b e c f d w bet cl imlementtion cl cl dt; dt; net; net; ( ( dt) dt).dt.dt dt; dt; Net Net ( ( w) w) net net w; w; getdt getdt dt; dt; net net net; net; Client code (It); (It); b b (w); (w); c c ; ; d d (bet); (bet);.net(b);.net(b); b.net(c); b.net(c); c.net(d); c.net(d); d.net(null); d.net(null); e e.net;.net; f f.net.net;.net.net; tt tt b.net.getdt b.net.getdt Introduction to Comuter Science, Shimon Schocken lide 1 A lked lit eger We now turn to decribe comlete lked lit emle: lit eger. Our lked lit btrction clude everl cle: : rereent dividul Lit: rereent lit itelf LitItertor: hel roce lit 0 7 Intnce Lit Intnce LitItertor (lter) Introduction to Comuter Science, Shimon Schocken lide 2 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

The cl ckge ckge LitStructure; LitStructure; Rereent Rereent gle gle lit lit cl cl Friendly Friendly (ckge-) (ckge-) field field dt; dt; No viibility modier net; net; ( ( dt, dt, net) net).dt.dt dt; dt;.net.net net; net; ( ( dt) dt) (dt, (dt, null); null); Two otion for rereentg dt: Enculte dt by mkg it field nd writg get/ method to hndle m Put lked lit cle ckge, nd mke field viible throughout ckge (tht wht we do emle). Pckge viibility: Achieved by mkg field ckge- In Jv, ckge- i defult viibility otion. Introduction to Comuter Science, Shimon Schocken lide Ug object to form lked lit ckge ckge LitStructure; LitStructure; Rereent Rereent gle gle lit lit cl cl Friendly Friendly (ckge-) (ckge-) field field dt; dt; net; net; ( ( dt, dt, net) net).dt.dt dt; dt;.net.net net; net; ttic ttic m m ( ( rg[]) rg[]) ; ; (7, (7, null); null); ( ( 7 7 ) ) (, (, ); ); ( ( (7 (7 ) ) ) ) (, (, ); ); ( ( ( ( (7 (7 ) ) ) ) ) ) The code bove form lit, but reultg dt tructure i dficult to mnge We how it yet nor emle how reference cn be ued ( ( dt) dt) (dt, (dt, null); null); Note tht lit conit two rt: Hed: (k node or tom ) Til: lit. Introduction to Comuter Science, Shimon Schocken lide 4 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

The Lit cl ckge ckge LitStructure; LitStructure; Rereent Rereent lit lit cl cl Lit Lit heder; heder; Client code Lit Lit Lit; Lit;.ert(.ert( (7)); (7));.ert(.ert( ()); ());.ert(.ert( ()); ()); Sytem.out.rln(); Sytem.out.rln(); Pr Pr 7 7 Contruct Contruct lit lit begng begng with with dummy dummy.. Lit Lit heder heder (0); (0); Before ertg : 0 7 Inert Inert n n t t begng begng ert ert ( ( ) ).net.net heder.net; heder.net; heder.net heder.net ; ; to to lter lter More More method method lter lter After ertg : 0 The lit hndler i it heder 7 In imlementtion, every lit beg with dummy ; Thi mke imlementtion lit method eier Quetion: Suoe we wnt to ert t end lit. How cn we do it? Introduction to Comuter Science, Shimon Schocken lide Proceg lit ckge ckge LitStructure; LitStructure; cl cl Lit Lit heder; heder; Return Return contg contg fd fd ( ( ) ) heder.net; heder.net; while while (!( (!( null)) null)) (.dt (.dt ) ) ; ;.net;.net; null; null; Client code Lit Lit Lit; Lit;.ert(.ert( (7)); (7));.ert(.ert( ()); ());.ert(.ert( ()); ()); Sytem.out.rln(.fd().dt); Sytem.out.rln(.fd().dt); r r Sytem.out.rln(.fd(8).dt); Sytem.out.rln(.fd(8).dt); ecetion ecetion Sytem.out.rln(); Sytem.out.rln(); r r 7 7 0 7 Return Return tetul tetul re. re. lit lit to to To roce lit: ; ; heder.net; reference vrible (y ) to lit heder heder.net; while while (!( (!( null)) null)) Proce.dt.dt.dt ; ;.net;.net.net; Do until you ve reched lit end. ; ; More More method method lter lter Introduction to Comuter Science, Shimon Schocken lide 6 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Removg n ckge ckge LitStructure; LitStructure; cl cl Lit Lit heder; heder; Contructor Contructor nd nd or or method method Remove Remove contg contg remove remove ( ( ) ) rev rev heder; heder; heder.net; heder.net; while while (!( (!( null)) null)) (.dt (.dt ) ) rev.net rev.net rev.net.net; rev.net.net; rev rev ; ;.net;.net; Client code Lit Lit Lit; Lit;.ert(.ert( (7)); (7));.ert(.ert( ()); ());.ert(.ert( (9)); (9));.ert(.ert( ()); ()); Sytem.out.rln(); Sytem.out.rln(); r r 9 9 7 7.remove(9);.remove(9); Sytem.out.rln(); Sytem.out.rln(); r r 7 7 rev 0 9 7 More More method method lter lter The removed object will be recycled by grbge collector. Introduction to Comuter Science, Shimon Schocken lide 7 Or common ertion vrt ckge ckge LitStructure; LitStructure; cl cl Lit Lit heder; heder; Lit Lit contructor contructor nd nd reviouly reviouly hown hown method method Inert Inert n n t t begng begng lit lit (me (me before) before) ert ert ( ( ) ) Inert Inert n n t t ecied ecied loction loction ertat ertat ( (,, n) n) Inert Inert n n t t end end lit lit ertatend ertatend ( ( ) ) More More / / or or method. method. Q: Wht bout ertg From n rry? From file? From nor lit? Anwer: KISS (Kee it ( API) Simle, Stuid) Introduction to Comuter Science, Shimon Schocken lide 8 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Or common method eret ckge ckge LitStructure; LitStructure; cl cl Lit Lit heder; heder; Lit Lit contructor contructor nd nd reviouly reviouly hown hown method method Return Return contg contg (me (me before) before) fd fd ( ( ) ) Return Return n th n th fdnth fdnth ( ( n) n) Check Check lit lit cont cont boolen boolen cont cont ( ( ) ) Check Check lit lit emty emty boolen boolen iemty iemty Emtie Emtie lit lit mkeemty mkeemty But (rememberg KISS), rr ckg ll e oertion o Lit btrction nd API, better deign my be to let client deign m, needed. More More / / or or method. method. Introduction to Comuter Science, Shimon Schocken lide 9 reviited: lked lit imlementtion cl cl Lit Lit element; element; element element Lit; Lit; boolen boolen cont cont ( ( ) ) ((element.fd ((element.fd null) null)?? fle fle : : true); true); ert ert ( ( ) ) (!cont) (!cont) element.ert( element.ert( ); ); to to element.to element.to ; ; ; ;.ert(2);.ert(2);.ert(7);.ert(7);.ert();.ert();.ert(7);.ert(7); Sytem.out.rln; Sytem.out.rln; r r 7 7 2 2 Introduction to Comuter Science, Shimon Schocken lide 40 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Itertor ckge ckge LitStructure; LitStructure; cl cl Lit Lit heder; heder; Sme Sme before before to1 to1 ; ; heder.net; heder.net; while while (!( (!( null)) null)).dt.dt ; ;.net;.net; ; ; An itertor i n object tht rovide mrchg ervice through collection The tyicl Itertor API clude: hnet: Return true itertion h more net: Return net remove: Remove from collection lt element ed by itertor (otionl oertion). to to ; ; for for (LitItertor (LitItertor itr itr LitItertor(heder.net); LitItertor(heder.net); itr.hnet;) itr.hnet;) itr.net.dt itr.net.dt ; ; ; ; Sme effect to1 bove, ug n itertor Introduction to Comuter Science, Shimon Schocken lide 41 Itertor imlementtion ckge ckge LitStructure; LitStructure; cl cl LitItertor LitItertor current current oition oition lit lit current; current; Ech collection tyiclly fer n itertor tht llow mrchg through collection There re more wy to imlement itertor, we ll ee lter. LitItertor LitItertor ( ( ) ) current current ; ; boolen boolen hnet hnet!(current!(current null); null); net net current; current; current current current.net; current.net; ; ; Introduction to Comuter Science, Shimon Schocken lide 42 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Generic lit Emle: Ech eron h 0 or more friend, who re eron. We wih to be ble to dd nd delete friend eily nd efficiently. Rereent Rereent eron eron cl cl Peron Peron Contruct Contruct eron eron Peron Peron ( ( nme, nme, emil) emil) Return Return tetul tetul re. re. eron eron to to Or Or Peron Peron method method follow. follow. Deired btrction: friend Peron object Peron object Peron object Problem: The Peron object rovide no mechnim to lk to or Peron How cn we evolve our Lit btrction, which oerte on lit eger, o generic lit cl tht oerte on or object tye? Thi etenion need not be dficult, ce lit i not relly lit eger, but rr lit object tht clude n field. Introduction to Comuter Science, Shimon Schocken lide 4 Generic lit friend Lit object object Ron D Net Ben Peron object cl cl Peron Peron dt; dt; net; net; (Peron (Peron,, net) net).dt.dt ; ;.net.net net; net; We lo hve to rewrite our Lit cl, which i deigned to oerte on eger, not on Peron object Cn we crete generic Lit cl tht oerted on ny object? Ye i clled generic. (Peron (Peron ) ) (, (, null); null); Introduction to Comuter Science, Shimon Schocken lide 44 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Outle Dt tructure / collection Stck lit Hhg Introduction to Comuter Science, Shimon Schocken lide 4 Hhg Arry rovide reonble olution for fdg vlue but roblemtic olution for ertg nd deletg vlue. Hhg: techniue ued to erform fdg, ertion, nd deletion, vlue contnt verge time (deendent number vlue) Two centrl lyer: Hh tble ADT Hhg function. Introduction to Comuter Science, Shimon Schocken lide 46 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Hhg: bic ide Hh tble 0 10, 1 Hh function h % h 1 2 4 6, 11, 716 97 9, 10124 Efficient fdg, ertion, deletion Idelly, ize tble hould be number key; tyiclly, not rcticl Idelly, key hould ditribute evenly cro tble; tyiclly, cn be chieved A rime tble ize hel romote n even ditribution Iue: how to hndle colliion. Introduction to Comuter Science, Shimon Schocken lide 47 Hh tble with erte chg Ech cell hh tble o to lked lit: The wort oible hh function: h 17 To fd / delete / ert key : Comute h nd erform fd/delete/ert oertion on reective lit Efficiency: The h comuttion tke contnt time; roceg lit tke ler time; lit re not long, we ll be OK. Oertion like fdm, fdm, or cnng vlue order: not uorted. Introduction to Comuter Science, Shimon Schocken lide 48 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

Hh function Hh function for eger key: imle. h % tblesize Tyicl hh function for key Public Public ttic ttic hh hh ( ( key, key, tblesize) tblesize) hvlue hvlue for for ( ( i i i i < < key.length; key.length; i) i) hhvlue hhvlue key.chrat(i); key.chrat(i); hhvlue hhvlue % % tblesize; tblesize; Introduction to Comuter Science, Shimon Schocken lide 49 Hh tble imlementtion cl cl HhTble HhTble ttic ttic fl fl DEFAULT_TABLE_SIZE DEFAULT_TABLE_SIZE 101; 101; Lit[] Lit[] tble; tble; tblesize; tblesize; hhtble hhtble hhtble hhtble ( ( ize) ize) mkeemty mkeemty ert ert (Object (Object ) ) remove remove (Object (Object ) ) Object Object fd fd (Object (Object ) ) ttic ttic hh hh (Object (Object key, key, tblesize) tblesize) ttic ttic netprime netprime ( ( n) n) Introduction to Comuter Science, Shimon Schocken lide 0 Intorduction to Comuter Science IDC Herzliy Shimon Schocken

The role hhg mngg Jv object When object i creted, Jv comute hh code for it. The hh code become uniue identier object, nd cn be cceed vi ttement like hhcode omeobject.hhcode; The hhcode method i rt Jv Object API Often, cl writer override tndrd hhcode nd write ir own hh code function Return Return hh hh code code for for Frction. Frction. @Override @Override hhcode hhcode fl fl rime rime 1; 1; reult reult 1; 1; reult reult rime rime * * reult reult denomtor; denomtor; reult reult rime rime * * reult reult numertor; numertor; reult; reult; / / IDE (like Eclie) rovide tndrd hhcode method More bout, lter coure. Introduction to Comuter Science, Shimon Schocken lide 1 Intorduction to Comuter Science IDC Herzliy Shimon Schocken