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

Abstract Data Types

This document discusses abstract data types (ADTs). It defines an ADT as specifying the data stored, operations on the data, and error conditions of operations. An example ADT for a stock trading system is provided. The document also discusses common patterns in storing objects and relationships in containers that lead to the definition of ADTs. Specific ADTs like arrays, vectors, lists, and stacks are introduced along with their basic operations and implementations using data structures like arrays and linked lists.

Uploaded by

Zarin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Abstract Data Types

This document discusses abstract data types (ADTs). It defines an ADT as specifying the data stored, operations on the data, and error conditions of operations. An example ADT for a stock trading system is provided. The document also discusses common patterns in storing objects and relationships in containers that lead to the definition of ADTs. Specific ADTs like arrays, vectors, lists, and stacks are introduced along with their basic operations and implementations using data structures like arrays and linked lists.

Uploaded by

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

Abstract Data Types

Reading: Chapter 1

 http://csce.uark.edu  +1 (479) 575-6043  [email protected]


2.1.8
Abstract Data Types
❑ We model such containers by Abstract Data Types or ADTs
● In engineering, we tend to see certain patterns that occur over and over
in applications
● In these circumstances, we first name these patterns and then proceed
to define certain standard solutions or implementations
● In software in storing objects and relationships in containers, there are
reoccurring containers of objects and associated relationships where
the actual queries and operations are restricted

2019/8/30 CSCE 4133/5133: Algorithms 2


Abstract Data Types
❑ An abstract data type (ADT) is an abstraction of a data structure
❑ An ADT specifies:
● Data stored
● Operations on the data
● Error conditions associated with operations
❑ Example: ADT modeling a simple stock trading system
● The data stored are buy/sell orders
● The operations supported are
▪ order buy(stock, shares, price)
▪ order sell(stock, shares, price)
▪ void cancel(order)
● Error conditions:
▪ Buy/sell a nonexistent stock
▪ Cancel a nonexistent order
2019/8/30 CSCE 4133/5133: Algorithms 3
2.1.8
Abstract Data Types
❑ Any time you are intending to store objects, you must ask:
● What are the relationships on the objects?
● What queries will be made about the objects in the container?
● What operations will be performed on the objects in the container?
● What operations may be performed on the container as a whole?
● What queries will be made about the relationships between the objects
in the container?
● What operations may be made on the relationships themselves between
the objects in the container?

2019/8/30 CSCE 4133/5133: Algorithms 4


2.2.1.1
2.2.1
Array ADT
❑ A Fixed-Size Contiguous Allocation of Objects is an Array
● An array stores n objects in a single contiguous space of memory
● Unfortunately, if more memory is required, a request for new memory
usually requires copying all information into the new memory
● In general, you cannot request for the operating
system to allocate to you the next n memory
locations

2019/8/30 CSCE 4133/5133: Algorithms 5


Vector ADT
❑ The Vector ADT extends the notion of array by storing an
arbitrary size of objects sequence
● An element can be accessed, inserted or removed by specifying its
index/rank (number of elements preceding it)
● An exception is thrown if an incorrect rank is specified
❑ Main vector operations:
● object elemAt(integer r): returns the element at index r without
removing it
● object replaceAt(integer r, object o): replace the element at rank with o
and return the old element
● insertAt (integer r, object o): insert a new element o to have index r
● object removeAt(integer r): removes and returns the element at index r
● Additional operations size() and isEmpty()

2019/8/30 CSCE 4133/5133: Algorithms 6


Array-based Vector
❑ Use an array V of size N
● A variable n keeps track of the size of the vector (number of elements
stored)
● Operation elemAt (r) is implemented in O(1) time by returning V[r]

V
0 1 2 r n

2019/8/30 CSCE 4133/5133: Algorithms 7


Insertion
❑ In operation insertAt(r, o), we need to make room for the new
element by shifting forward the n - r elements V[r], …, V[n - 1]
❑ In the worst case (r = 0), this takes O(n) time

V
0 1 2 r n
V
0 1 2 r n
V o
0 1 2 r n

2019/8/30 CSCE 4133/5133: Algorithms 8


Deletion
❑ In operation removeAt(r), we need to fill the hole left by the
removed element by shifting backward the n - r - 1 elements V[r +
1], …, V[n - 1]
❑ In the worst case (r = 0), this takes O(n) time

V o
0 1 2 r n
V
0 1 2 r n
V
0 1 2 r n

2019/8/30 CSCE 4133/5133: Algorithms 9


Performance
❑ In the array based implementation of a Vector
● The space used by the data structure is O(n)
● size, isEmpty, elemAt and replaceAt run in O(1) time
● insertAt and removeAt run in O(n) time
❑ Note
● If we use the array in a circular fashion, insertAt (0) and removeAt (0)
run in O(1) time
● In an insertAt operation, when the array is full, instead of throwing an
exception, we can replace the array with a larger one

2019/8/30 CSCE 4133/5133: Algorithms 10


List ADT
❑ The List ADT models a ❑ Update methods:
sequence of arbitrary objects ● replaceElement(p, o),
● It establishes a before/after swapElements(p, q)
relation between positions ● insertBefore(p, o), insertAfter(p,
❑ Generic methods: o),
● size(), isEmpty() ● insertFirst(o), insertLast(o)
● remove(p)
❑ Query methods:
● isFirst(p), isLast(p)
❑ Accessor methods:
● first(), last()
● before(p), after(p)

2019/8/30 CSCE 4133/5133: Algorithms 11


Singly Linked List
❑ A singly linked list is a concrete data structure consisting of a
sequence of nodes
next
❑ Each node stores
● element
● link to the next node
elem node

A B C D

2019/8/30 CSCE 4133/5133: Algorithms 12


Doubly Linked List
❑ A doubly linked list provides a natural implementation of the
List ADT
prev next
❑ Nodes implement Position and store:
● element
● link to the previous node
● link to the next node elem node
❑ Special trailer and header nodes
header nodes/positions trailer

elements

2019/8/30 CSCE 4133/5133: Algorithms 13


Insertion
❑ We visualize operation insertAfter(p, X), which returns position q
p

A B C
p

A B q C

X
p q

A B X C
2019/8/30 CSCE 4133/5133: Algorithms 14
Deletion
❑ We visualize remove(p), where p = last()
p

A B C D

A B C p

A B C
2019/8/30 CSCE 4133/5133: Algorithms 15
Performance
❑ In the implementation of the List ADT by means of a doubly
linked list
● The space used by a list with n elements is O(n)
● The space used by each position of the list is O(1)
● All the operations of the List ADT run in O(1) time
● Operation element() of the
Position ADT runs in O(1) time

2019/8/30 CSCE 4133/5133: Algorithms 16


The Stack ADT
❑ The Stack ADT stores ❑ Auxiliary stack operations:
arbitrary objects ● object top(): returns the last
❑ Insertions and deletions inserted element without
removing it
follow the last-in first-out
scheme ● integer size(): returns the
number of elements stored
❑ Think of a spring-loaded ● boolean isEmpty(): indicates
plate dispenser whether no elements are stored
❑ Main stack operations:
● push(object): inserts an element
● object pop(): removes and
returns the last inserted element

2019/8/30 CSCE 4133/5133: Algorithms 17


Exceptions
❑ Attempting the execution of ❑ In the Stack ADT, operations
an operation of ADT may pop and top cannot be
sometimes cause an error performed if the stack is
condition, called an exception empty
❑ Exceptions are said to be ❑ Attempting the execution of
“thrown” by an operation that pop or top on an empty stack
cannot be executed throws an
EmptyStackException

2019/8/30 CSCE 4133/5133: Algorithms 18


Array-based Stack
❑ A simple way of Algorithm size()
implementing the Stack ADT return t + 1
uses an array
❑ We add elements from left to Algorithm pop()
right if isEmpty() then
❑ A variable keeps track of the throw EmptyStackException
index of the top element else
tt−1
return S[t + 1]


S
0 1 2 t

2019/8/30 CSCE 4133/5133: Algorithms 19


Array-based Stack (cont.)
❑ The array storing the stack elements may become full
❑ A push operation will then throw a FullStackException
● Limitation of the array-based implementation
● Not intrinsic to the Stack ADT
Algorithm push(o)
if t = S.length − 1 then
throw FullStackException
else
tt+1
S[t]  o


S
0 1 2 t
2019/8/30 CSCE 4133/5133: Algorithms 20
Stack with a Singly Linked List
❑ We can implement a stack with a singly linked list
● The top element is stored at the first node of the list
● The space used is O(n) and each operation of the Stack ADT takes O(1)
time

nodes

t 

elements

2019/8/30 CSCE 4133/5133: Algorithms 21


Performance and Limitations
❑ Performance
● Let n be the number of elements in the stack
● The space used is O(n)
● Each operation runs in time O(1)
❑ Limitations
● The maximum size of the stack must be defined a priori and cannot be
changed
● Trying to push a new element into a full stack causes an
implementation-specific exception

2019/8/30 CSCE 4133/5133: Algorithms 22


The Queue ADT
❑ The Queue ADT stores ❑ Auxiliary queue operations:
arbitrary objects ● object front(): returns the
❑ Insertions and deletions element at the front without
removing it
follow the first-in first-out
scheme ● integer size(): returns the
number of elements stored
❑ Insertions are at the rear of ● boolean isEmpty(): indicates
the queue and removals are at whether no elements are stored
the front of the queue
❑ Exceptions
❑ Main queue operations: ● Attempting the execution of pop
● push(object): inserts an element or front on an empty queue
at the end of the queue throws an
● object pop(): removes and EmptyQueueException
returns the element at the front
of the queue
2019/8/30 CSCE 4133/5133: Algorithms 23
Queue Operations
❑ We use the modulo operator (remainder of division)
Algorithm size()
return (N − f + r) mod N

Algorithm isEmpty()
return (f = r)

Q
0 1 2 f r
Q
0 1 2 r f

2019/8/30 CSCE 4133/5133: Algorithms 24


Queue Operations (cont.)
❑ Operation push throws an exception if the array is full
❑ This exception is implementation-dependent
Algorithm push(o)
if size() = N − 1 then
throw FullQueueException
else
Q[r]  o
r  (r + 1) mod N
Q
0 1 2 f r
Q
0 1 2 r f
2019/8/30 CSCE 4133/5133: Algorithms 25
Queue Operations (cont.)
❑ Operation pop throws an exception if the queue is empty
❑ This exception is specified in the queue ADT
Algorithm pop()
if isEmpty() then
throw EmptyQueueException
else
o  Q[f]
f  (f + 1) mod N
return o
Q
0 1 2 f r
Q
0 1 2 r f
2019/8/30 CSCE 4133/5133: Algorithms 26
Array-based Queue
❑ Use an array of size N in a circular fashion
❑ Two variables keep track of the front and rear
● f index of the front element
● r index immediately past the rear element
❑ Array location r is kept empty

normal configuration
Q
0 1 2 f r

wrapped-around configuration
Q
0 1 2 r f
2019/8/30 CSCE 4133/5133: Algorithms 27
Growable Array-based Queue
❑ In an push operation, when the array is full, instead of throwing
an exception, we can replace the array with a larger one
● Similar to what we did for an array-based stack
❑ The push operation has amortized running time
● O(n) with the incremental strategy
● O(1) with the doubling strategy

2019/8/30 CSCE 4133/5133: Algorithms 28


Queue with a Singly Linked List
❑ We can implement a queue with a singly linked list
● The front element is stored at the first node
● The rear element is stored at the last node
❑ The space used is O(n) and each operation of the Queue ADT
takes O(1) time
r
nodes

f 

elements
2019/8/30 CSCE 4133/5133: Algorithms 29

You might also like