abstract data types: lists 1

Post on 18-Dec-2021

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Structured Programming 1110/1140/6710

ADTs

The List ADT

A List interface and its implementation: Array List

Abstract Data Types:

Lists 1

Structured Programming 1110/1140/6710

Abstract Data Types (ADTs)

Abstract data types describe the behaviour (semantics) of a data type

without specifying its implementation. An ADT is thus abstract, not

concrete.

A container is a very general ADT, serving as a holder of objects. A

list is an example of a specific container ADT.

An ADT is described in terms of the semantics of the operations that

may be performed over it.

6

Abstract Data Types: Lists A1

Structured Programming 1110/1140/6710

The List ADT

The list ADT is a container known mathematically as a finite

sequence of elements. A list has these fundamental properties:

• duplicates are allowed

• order is preserved

A list may support operations such as these:

• create: construct an empty list

• add: add an element to the list

• is empty: test whether the list is empty

7

Abstract Data Types: Lists A1

Structured Programming 1110/1140/6710

Our List Interface

We will explore lists using a simple interface:

public interface List<T> {

void add(T value);

T get(int index);

int size();

T remove(int index);

void reverse();

}

8

Abstract Data Types: Lists A1

Structured Programming 1110/1140/6710

A B C D

A B CD

A B C D

void add(T value);

T get(int index);

int size();

T remove(int index);

void reverse();

String toString();

9

Abstract Data Types: Lists

A B C D

A B C D

A B C D

C

4

A B CD

D B A

D B A D B A

D

2

2

A1

C

Structured Programming 1110/1140/6710 10

Abstract Data Types: Lists A1

List Implementation

• Arrays

– Fast lookup of any element

– A little messy to grow and contract

• Linked list

– Logical fit to a list, easy to grow, contract

– Need to traverse list to find arbitrary element

Structured Programming 1110/1140/6710

A List interface and its implementation: Linked List

Abstract Data Types:

Lists 2

Structured Programming 1110/1140/6710

List Implementation: Linked Lists

• Arrays

– Fast lookup of any element

– A little messy to grow and contract

• Linked list

– Logical fit to a list, easy to grow, contract

– Need to traverse list to find arbitrary element

16

Abstract Data Types: Lists A2

A B C D

start end

public class LinkedList<T> {

private class LLNode<T> {

T value;

LLNode<T> next;

}

LLNode<T> start;

LLNode<T> end;

}

Structured Programming 1110/1140/6710

Linked List Reversal

New list

17

Abstract Data Types: Lists

A B C D

start end

D C B A

start end

A

endstart

A2

B A

endstart

C B A

endstart

Structured Programming 1110/1140/6710

Linked List Reversal

18

Abstract Data Types: Lists

A B C D

startend

A B C D

end start

Pointer reversal

A2

A B C D

start end

A B C D

startend

A B C D

startend

reverse

reverse

reverse

reverse

Structured Programming 1110/1140/6710

The Set ADT

A Set Interface

Abstract Data Types:

Sets

Structured Programming 1110/1140/6710

The Set ADT

23

The set ADT corresponds to a mathematical set. A set has these

fundamental properties:

• duplicates are not allowed

• order is not preserved

A set may support operations such as these:

• create: construct an empty set

• add: add an element to the set

• contains: does the set contain a given element

• remove: remove an element from the set

A3Abstract Data Types: Sets

Structured Programming 1110/1140/6710

Our Set Interface

We will explore sets using a simple interface :

public interface Set<T> {

boolean add(T value);

boolean contains(T value);

int size();

boolean remove(T value);

}

24

Abstract Data Types: Sets A3

Structured Programming 1110/1140/6710

Hash Table

Implementation of a Set 1

Abstract Data Types:

Hash Tables

Structured Programming 1110/1140/6710

Hash Tables

28

A hash table stores (key, value) pairs, using a hash function to map a

key into a table. Key challenges are: a) dealing with hash collisions,

and b) dealing with load (how big to make the table).

Two broad approaches:

• Separate chaining

– Hash table entries are lists. (key, values) are in lists.

• Open addressing

– Hash table entries are (key, value) pairs.

– Collisions resolved by probing – e.g. find next empty slot

A4Abstract Data Types: Hash Table

Structured Programming 1110/1140/6710

apple

orange

banana

pearapricot

peachmango

plumcherry

grape

Abstract Data Types: Hash Table A4

orangeapple banana pear apricot peach mango plum cherry grape

fruit

Structured Programming 1110/1140/6710

Abstract Data Types: Hash Table A4

orangeapple banana pear apricot peach mango plum cherry grape

fruit.add(“apple”)

fruit

fruit.add(“orange”)fruit.add(“banana”)fruit.add(“pear”)fruit.add(“orange”)fruit.add(“apricot”)

Structured Programming 1110/1140/6710

Abstract Data Types: Hash Table A4

orangeapple banana pear apricot peach mango plum cherry grape

fruit

fruit.contains(“orange”)

fruit.contains(“mango”)fruit.contains(“fig”)

✗✓

Structured Programming 1110/1140/6710 32

Abstract Data Types: Hash Table A4

orange

apple banana

pear

apricot

peach

mango

plum

grape

cherry

a-f

g-m

n-t

u-z

fruit

apple

orange

banana

pearapricot

peachmango

plumcherry

grape

Structured Programming 1110/1140/6710

fruit

33

Abstract Data Types: Hash Table A4

orange

apple banana

pear

apricot

peach

mango

plum

grape

cherry

a-f

g-m

n-t

u-z

appleorangebananapearapricotpeachmangoplumgrapecherry

Structured Programming 1110/1140/6710 34

Abstract Data Types: Hash Table A4

orange

apple banana

pear

apricot

peach

mango

plum

grape

cherry

a-f

g-m

n-t

u-z

fruit.contains(“orange”)

orangefiggrape

✗fruit.contains(“grape”)fruit.contains(“fig”)

fruit

Introduction to Software Systems 1110/1140/6710

The Tree ADT

Implementation of a Set 2

Abstract Data Types:

Trees

Introduction to Software Systems 1110/1140/6710

The Tree ADT

14

The tree ADT corresponds to a mathematical tree. A tree is defined

recursively in terms of nodes:

• A tree is a node

• A node contains a value and a list of trees.

• No node is duplicated.

A5Abstract Data Types: Trees

Introduction to Software Systems 1110/1140/6710

Binary Search Tree

A binary search tree is a tree with the following additional properties:

• Each node has at most two sub-trees

• Nodes may contain (key, value) pairs (or just keys)

• Keys are ordered within the tree:

– The left sub-tree only contains keys less than the node’s key

– The right sub-tree only contains keys greater than the node’s key

15

A5Abstract Data Types: Trees

Introduction to Software Systems 1110/1140/6710 16

Abstract Data Types: Trees A5

orange

apple

pear

apricot peachmango plum

grape

cherry

banana

fruit

apple

orange

banana

pearapricot

peachmango

plumcherry

grape

Introduction to Software Systems 1110/1140/6710 17

Abstract Data Types: Trees A5

orange

apple

pear

plum

grape

cherry

mango peachapricot

banana

fruit

appleorangebananapearapricotpeachmangoplumgrapecherry

Introduction to Software Systems 1110/1140/6710 18

Abstract Data Types: Trees A5

orange

apple

pear

plum

grape

cherry

mango peachapricot

banana

fruit

fruit.contains(“orange”)fruit.contains(“grape”)fruit.contains(“fig”) ✓

✓✗

Structured Programming 1110/1140/6710

The Map ADT

A Map interface and its implementation

ADT Recap

Abstract Data Types:

Maps

Structured Programming 1110/1140/6710

ADT Recap

12

Abstract Data Types: Maps

First-principles implementation of three Java container types:

• List

– ArrayList, LinkedList implementations (A1, A2)

• Set

– HashSet, BSTSet implementations (A3, A4, A5)

• Map

– HashMap, BSTMap implementations (A6)

Introduced hash tables, trees (A4, A5)

A6

Structured Programming 1110/1140/6710

The Map ADT (A.K.A. Associative Array)

13

Abstract Data Types: Maps

A map consists of (key, value) pairs

• Each key may occur only once in the map

• Values are retrieved from the map via the key

• Values may be modified

• Key, value pairs may be removed

A6

Structured Programming 1110/1140/6710

Our Map Interface

14

Abstract Data Types: Maps

We will explore maps using a simple interface:

public interface Map<K,V> {

V put(K key, V value);

V get(K key);

V remove (K key);

int size();

}

A6

Structured Programming 1110/1140/6710

fruit

15

Abstract Data Types: Maps A6

orange

apple banana

pear

apricot

peach

mango

plum

grape

cherry

a-f

g-m

n-t

u-z

3.50 2.50 5.50 12.00

11.25 7.00

4.00 4.00 6.00 4.50

fruit.get(“apricot”)fruit.put(“grape”, 7.00)fruit.put(“orange”, 3.50)

5.50orangeapricotgrape

3.50

Structured Programming 1110/1140/6710 16

Abstract Data Types: Maps A6

orange

apple

pear

plum

grape

cherry

mango peachapricot

banana

fruit

3.50

2.50

5.50

12.00

11.25

7.00

4.00

4.00

4.506.00

fruit.get(“apricot”)fruit.put(“grape”, 7.00)fruit.put(“orange”, 3.50)

5.50

3.50

top related