Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Advertisements

Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Hash Tables1  
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture4: Arrays Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Ch7. List and Iterator ADTs
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
05 Array-Based Sequences
Array Lists, Node Lists & Sequences
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Stacks.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Copyright © Aiman Hanna All rights reserved
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Array Lists1 © 2010 Goodrich, Tamassia

Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary objects  An element can be accessed, inserted or removed by specifying its index (number of elements preceding it)  An exception is thrown if an incorrect index is given (e.g., a negative index)  Main methods: get(integer i): returns the element at index i without removing it set(integer i, object o): replace the element at index i with o and return the old element add(integer i, object o): insert a new element o to have index i remove(integer i): removes and returns the element at index i  Additional methods: size() isEmpty() © 2010 Goodrich, Tamassia

Array Lists3 Applications of Array Lists  Direct applications Sorted collection of objects (elementary database)  Indirect applications Auxiliary data structure for algorithms Component of other data structures © 2010 Goodrich, Tamassia

Array Lists4 Array-based Implementation  Use an array A of size N  A variable n keeps track of the size of the array list (number of elements stored)  Operation get ( i ) is implemented in O(1) time by returning A[i]  Operation set ( i,o ) is implemented in O(1) time by performing t = A[i], A[i] = o, and returning t. A 012n i © 2010 Goodrich, Tamassia

Array Lists5 Insertion  In operation add ( i, o ), we need to make room for the new element by shifting forward the n  i elements A[i], …, A[n  1]  In the worst case ( i  0 ), this takes O(n) time A 012n i A 012n i A 012n o i © 2010 Goodrich, Tamassia

Array Lists6 Element Removal  In operation remove (i), we need to fill the hole left by the removed element by shifting backward the n  i  1 elements A[i  1], …, A[n  1]  In the worst case ( i  0 ), this takes O(n) time A 012n i A 012n o i A 012n i © 2010 Goodrich, Tamassia

Array Lists7 Performance  In the array based implementation of an array list: The space used by the data structure is O(n) size, isEmpty, get and set run in O(1) time add and remove run in O(n) time in worst case  If we use the array in a circular fashion, operations add(0, x) and remove(0, x) run in O(1) time  In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one © 2010 Goodrich, Tamassia

Array Lists8 Growable Array-based Array List  In an add(o) operation (without an index), we always add at the end  When the array is full, we replace the array with a larger one  How large should the new array be? Incremental strategy: increase the size by a constant c Doubling strategy: double the size Algorithm add(o) if t = S.length  1 then A  new array of size … for i  0 to n  1 do A[i]  S[i] S  A n  n + 1 S[n  1]  o © 2010 Goodrich, Tamassia

Array Lists9 Comparison of the Strategies  We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n add(o) operations  We assume that we start with an empty stack represented by an array of size 1  We call amortized time of an add operation the average time taken by an add over the series of operations, i.e., T(n)/n © 2010 Goodrich, Tamassia

Array Lists10 Incremental Strategy Analysis  We replace the array k = n / c times  The total time T(n) of a series of n add operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c( … + k) = n + ck(k + 1)/2  Since c is a constant, T(n) is O(n + k 2 ), i.e., O(n 2 )  The amortized time of an add operation is O(n) © 2010 Goodrich, Tamassia

Array Lists11 Doubling Strategy Analysis  We replace the array k = log 2 n times  The total time T(n) of a series of n add operations is proportional to n …+ 2 k = n  2 k + 1  1 = 3n  1  T(n) is O(n)  The amortized time of an add operation is O(1) geometric series © 2010 Goodrich, Tamassia