+ All Categories
Home > Documents > Abstract Data Types - University of...

Abstract Data Types - University of...

Date post: 15-Jun-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
15
Abstract Data Types Definitions, Implementations, and Uses
Transcript
Page 1: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Abstract Data Types

Definitions, Implementations, and Uses

Page 2: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

ADT

• An abstract data representation without reference to a specific implementation – TABLE

– QUEUE

– STACK

• The “primitive” operations on the ADT elements – store, retrieve, lookup

– Generically called getters and setters

• toString provides a generic way to display the contents of a complex data element

Page 3: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Ord

er

TABLE ADT

Name & Address Telephone Number

Unique key Data needed

Search (lookup)

Store (insert) Retrieve

(get_by_key)

Page 4: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

TABLE Primitive Operations • Insert (for ordered tables)

• Delete

• Find (search by key)

• Retrieve (data by key) = get(find()) – Operations ordinarily dominated by retrieval

– Requires a find (by key) and a get (return of value)

– Does not modify the table data

• Traverse (all of the above require an ability to “walk” the links

Page 5: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

TABLE Composite Operations

• Edit (general)

– Retrieve (to get location)

– Delete old data

– Insert new data

– Depending on how the table is implemented this could take up to three taversrals

Page 6: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

QUEUE ADT

Head Tail

Retrieve (dequeue)

Store (enqueue)

First-In-First-Out

Page 7: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

QUEUE Primitive Operations

• isEmpty – returns true if there is nothing in the queue

• isFull – returns true if this is a fixed size and is full of elements

• Enqueue – adds an element to the tail of the queue

• Dequeue – removes an element from the head

• Peek – looks at the head element without removing it

Page 8: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

STACK ADT

Retrieve (pop)

Store (push)

Last-In-First-Out

Top_of_stack

Page 9: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

STACK Primitive Operations

• isEmpty – returns true if there are no elements in the stack

• isFull – returns true if the stack is of fixed size and it is full of elements

• Push – puts a new element at the top of the stack

• Pop – removes an element from the top of the stack

• Peek – looks at the element at the top of the stack without removing it

Page 10: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Array Implementations • Table – multiple arrays or multi-dimensional

– Key element and value element must be kept in order – Sorting two arrays difficult and time inefficient – Searching can be implemented as a binary search –

much faster

• Queue – Single dimensioned array of data type – Problem with moving data toward index 0 – Circular queue is very efficient – Fixed size

• Stack – Single dimensioned array of data type – Fixed size

Page 11: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Linked List Implementation

• Linked lists are extensible (can grow as needed)

• Search operations take time proportional to the size of the list, n

• Good performance for queues and stacks but not so good for tables

Page 12: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Linked List Details

node

data next_p data next_p

first node

next_p

last

next_p NULL

data

data

data

head

count first

heap variables

headPtr variable

last

data

In program heap stack (function

stack frame) Could be in program heap

or function stack frame

typedef struct headStr {

int count;

NodePtr first;

NodePtr last;

} HeadStr;

typedef HeadStr * HeadPtr;

typedef struct nodeStr {

Data data;

nodeStr next_p;

} NodeStr;

typedef NodeStr * NodePtr;

To Add data to unordered list just make the last element point

to the new data node.

Page 13: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Linked List Insert

node

next_p

node

next_p

newNode

next_p

temp_p

dataA

dataB

dataC

dataB > dataA Go to next node

dataC > dataB Insert before

Insert dataB behind dataA and before dataC to establish order.

Doubly linked list

next_p dataB prior_p next_p dataA prior_p

Page 14: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

BST

root node

root pointer

George

name

left right

name

left right name

left right

Dennis

Greg

name

left right

name

left right

Bill Donald

name

left right

name

left right

Granford Jim

name

left right

Arron

name

left right

Kelly

Less than More than

Stack

Heap

0

1 2

3 4 5 6

7 8

David name

left right

New Node

Page 15: Abstract Data Types - University of Washingtonfaculty.washington.edu/gmobus/Academics/TCES203/ADTs.pdf · Abstract Data Types Definitions, Implementations, and Uses . ADT •An abstract

Hash Table/Map Implementation of Fast Table

node

next_p

name

salary

y_t_d

node

next_p

name

salary

y_t_d

“Wayvern” %

“Wayvern”

“Wayne” hash

function

modulo the array size = 3

Table Array

0

1

2

size

class HashMap { int size; head hashMap[size]; int hash(string); void insert(node entry); node find(string name); float getSalary(string name); double getYearToDate(string name); etc. }

class Head { int count; node first; }

class Node { string name; float salary; double y_t_d; Node * next_p; }


Recommended