0% found this document useful (0 votes)
3 views

DSA-Class 04-ADTs_ArrayList

The document discusses Abstract Data Types (ADTs), focusing on their definition, common types, and operations. It explains the implementation of ADTs, particularly the Array List, detailing operations such as insertion, deletion, and searching, along with their time complexities. Additionally, it highlights the application of lists in polynomial manipulation and addresses issues related to sparse polynomials.

Uploaded by

nhuquan.dev.py
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA-Class 04-ADTs_ArrayList

The document discusses Abstract Data Types (ADTs), focusing on their definition, common types, and operations. It explains the implementation of ADTs, particularly the Array List, detailing operations such as insertion, deletion, and searching, along with their time complexities. Additionally, it highlights the application of lists in polynomial manipulation and addresses issues related to sparse polynomials.

Uploaded by

nhuquan.dev.py
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

ABSTRACT DATA TYPE

ARRAY LIST
Data Structures and Algorithm
2

Outline
• Abstract Data Type (ADT)
• Common ADTs
• Array List
3

Abstract Data Type (ADT)


• An Abstract Data Type (ADT) is a data structure
with a set of operations
• Operations specify how the ADT behaves, but does not
reveal how they are implemented
• In many cases, there are more than one way to implement
an ADT

• In this course we will cover lots of ADTs


• Lists
• Stacks
• Queues
• Trees & Search Trees
• Hash Tables & Tries
4

Abstract Data Type (ADT) - more


• An Abstract Data Type (ADT) is a data
structure with a set of operations
Attributes (ADT State)

Operations (ADT Methods)

Operation1(…)
Operation2(…)
Operation3(…)



5

Why Abstract?
• Specify the operations of the data structure
and leave implementation details to later
• many, many different ADTs
• picking the right one for the job is an important step
in design
• "Get your data structures correct first, and the rest
of the program will write itself."
- Davids S. Johnson
• High level languages often provide built in
ADTs,
• the C++ STL, the Java standard library
6

The Core Operations


• Every Collection ADT should provide a way to:
• add an item
• remove an item
• find, retrieve, or access an item
• Many, many more possibilities
• is the collection empty
• make the collection empty
• give me a sub set of the collection
• and on and on and on…
• Many different ways to implement these items
each with associated costs and benefits
7

Implementing ADTs
• When implementing an ADT, the operations
and behaviors are already specified
• Implementer’s first choice is what to use as
the internal storage container for the
concrete data type
• the internal storage container is used to hold the
items in the collection
• often an implementation of an ADT
• initially slim pickings for choice of storage
containers: arrays anyone?
8

The Grand Tour


• Why study ADTs?
• Why reimplement some of them?
• How many of you will actually go out and create
your own linked list ADT from scratch?
• Remember, the primary goal is to learn how to
learn how to use and create ADTs
• also learn the behavior of some of the more
conventional ADTs
9

Bags and Sets


• Simplest ADT is a Bag
• items can be added, removed, accessed
• no implied order to the items
• duplicates allowed
• Set
• same as a bag, except duplicate elements not allowed
• union, intersection, difference, subset
10

Lists
• Items have a position in this Collection
• Random access or not?
• Array Lists
• internal storage container is native array
• Linked Lists

last
first
11

Stacks
• Collection with access only to the last element
inserted
• Last in first out Data4 Top
• insert/push Data3
• remove/pop
Data2
• top
Data1
• make empty
12

Queues
• Collection with access only to the item that
has been present the longest
• Last in last out or first in first out
• enqueue, dequeue, front
• priority queues and dequeues
Front
Back

Data1 Data2 Data3 Data4


13

Trees
• Similar to a linked list:
Root
14

Other Types of Trees


• Binary Search Trees
• sorted values
• Heaps
• sorted via a different algorithm
• AVL and Red-Black Trees
• binary search trees that stay balanced
• Splay Trees
• B Trees
15

HashTables
• Take a key, apply function
• f(key) = hash value
• store data or object based on hash value
• Sorting O(N), access O(1) if a perfect hash
function and enough memory for table
• how deal with collisions?
16

Other ADTs
• Maps
• a.k.a. Dictionary
• Collection of items with a key and associated values
• similar to hash tables, and hash tables often used to
implement Maps
• Graphs
• Nodes with unlimited connections between other
nodes
• Sparse vectors and sparse matrices
17

List ADT
• What is a list?
• An ordered sequence of elements A1, A2, …, AN
• Elements may be of arbitrary, but the same type (i.e., all
ints, all doubles etc.)
• Common List operations are:
• Insert(ElementType E, Position P)
• Delete(Position P)
• Find(ElementType E)
• IsEmpty() – Returns true if the list is empty
• First() – Returns the first element
• Last() – Returns the last element
• Kth(Position K) – Returns the Kth element
• Length() – Returns the length of the list
18

Lists: Implementation
• Two types of implementation:
• Array-Based - Today
• Linked List – Next Week

• We will compare worst case running time of


ADT operations with different
implementations
19

Lists: Array-Based Implementation


• Basic Idea:
• Pre-allocate a big array of size MAX_SIZE
• Keep track of first free slot using a variable N
• Empty list has N = 0
• Shift elements when you have to insert or delete

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N-1
20

Array List ADT – C++ Declerations


ArrayList ADT
class ArrayList { // Constructor:
private: // Make empty list
#define MAX_SIZE 100 ArrayList::ArrayList(){
int elements[MAX_SIZE]; noOfElements = 0;
int noOfElements; } // end-ArrayList

public:
ArrayList (); // Constructor
~ArrayList ();// Destructor

void Insert(int e, int pos);


void Delete(int pos);
int Find(int e);
bool IsEmpty();
int First();
int Last();
int Kth(int pos);
int Length();
};
21

Lists Operations: Insert


• Insert(ElementType E, Position P)
• Example: Insert(X, 2): Insert X at position 2
• Basic Idea: Shift existing elements to the
right by one slot and insert new element

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N

• Here is the final list. Running time: O(N)


0 1 2 3 ……… N-1 N MAX_SIZE
A_1 A_2 X A_3 ……… A_N-1 A_N
22

Lists Insert – Full Array


• What if the array is already full?
• Can return an error
• Not preferred

• Typically though, you would do the following:


(1) Allocate a bigger array (usually double the capacity)
(2) Copy all elements from the old array to the new one
(3) Free up the space used by the old array
(4) Start using the new array

• With this dynamic array implementation, you


would also need to keep track of the capacity of
the array
23

Lists Operations: Delete


• Delete(Position P)
• Example: Delete(1): Delete element at position 1
• Basic Idea: Delete element and shift existing
elements to the left by one

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N

• Here is the final list. Running time: O(N)


0 1 2 3 ……… N-2 N-1 MAX_SIZE
A_1 A_3 A_4 A_5 ……… A_N
24

Lists Operations: Find


• Find(ElementType E)
• Example: Find(X): Search X in the list

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N

• Must do a linear search


• Running time: O(N)
25

Lists Operations: isEmpty


• IsEmpty()
• Returns true if the list is empty

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N

• Trivial – Return true if N == 0


• Running time: O(1)
26

Lists Operations: First, Last, Kth


• First(): Return A[0]
• Last(): Return A[N-1]
• Kth(Position K): Return A[K]

0 1 2 3 ……… N-1 MAX_SIZE


A_1 A_2 A_3 A_4 ……… A_N

• First – Running time: O(1)


• Last – Running time: O(1)
• Kth – Running time: O(1)
27

Application of Lists
• Polynomial ADT: store and manipulate single
variable polynomials with non-negative exponents
• 10X^3 + 4X^2 + 7 = 10X^3 + 4X^2 + 0X^1 + 7X^0
• Data structure: stores coefficients Ci and exponents i

• Array Implementation: C[i] = Ci


• E.g. C[3] = 10, C[2] = 4, C[1] = 0, C[0] = 7

• ADT operations: Input polynomials in arrays A and


B, result in C
• Addition: C[i] = ?
• Multiplication: ?
28

Application of Lists: Polynomials


• Add(Poly A, Poly B, Poly C):
• C[i] = A[i] + B[i] for 0<=i<=N

• Multiply(Poly A, Poly B, Poly C):


• Set C[i] = 0 for 0<=i<=N
• For each pair (i, j)
• C[i+j] = C[i+j] + A[i]*B[j]

• Divide(Poly A, Poly B, Poly C):


• You do it…
29

Application of Lists: Polynomials


• Problem with Array Implementation: Sparse
Polynomials
• E.g. 10X^3000+ 4X^2+ 7
• Waste of space and time (Ci are mostly 0s)
• Solution?
• Store each element as exponent and coefficient and
sort in decreasing order of exponents
(10, 3000) (4,2) (7,0)

• More flexible solution: Use singly linked list


head (10, 3000) (4, 2) (7, 0)

You might also like