54
1 Chapter 3 Data Representation Part 1

1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

Embed Size (px)

Citation preview

Page 1: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

1

Chapter 3

Data Representation

Part 1

Page 2: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

2

Goals• Introduce the different ways in which data may be represented

• Concepts

– Abstract data types

– Formula-based, linked, indirect addressing, and simulated-pointer representations

– Chains, circular lists, and doubly linked lists

• Applications

– Bin sort, radix sort

– Equivalence class

– Convex hull

Page 3: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

3

Data objectsA set of instances or values

• Boolean = {false, true}

• Digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

• Letter = {A, B, C, …, Z, a, b, … z}

• Natural Number = {0, 1, 2, …}

• Integer = {0, ±1, ±2, ±3, …}

• String = {a, b, …, aa, ab, ac, …}

Page 4: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

4

Primitive and composed data object

• Primitive (or atomic) data objects

• Composed data objects

• Relationships among instances, e.g., order

• Functions associated with data objects

Page 5: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

5

Data structures• Data structure: a data object together with the

relationships that exist among the instances, usually expressed by functions/operations

• Data structure = data + functions• Standard data types - int, float, bool• User defined data types - using enumeration,

grouping facility such as class, array, and pointers, e.g., char s[MaxSize];

Page 6: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

6

Linear Lists

• Data object in the form (e1, e2, …, en) where n is a finite natural number

• elements - ei’s

• length - n

• empty list - when n = 0

• Order - e1 precedes e2, e2 precedes e3, and so on

Page 7: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

7

Examples of linear lists

• An alphabetized list of students in a class

• a list of exam scores in nondecreasing order

• an alphabetized list of members of Congress

• a list of gold-medal winners in the Olympics men’s basketball event

Page 8: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

8

ADT specification

Page 9: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

9

Formula-based representation

• Use an array to represent the instances of a linear list

• Cell (or node) holds an instance of the data object

• Locations of each element is given by a mathematical formula, e.g.,location(i) = i-1

Page 10: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

10

An example

Page 11: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

11

Class Definition

Page 12: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

12

Class Definition (Continue)

Page 13: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

13

What if new fails?

Page 14: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

14

Constructor

Page 15: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

15

Find and Search

Page 16: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

16

Delete

Page 17: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

17

Insert

Page 18: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

18

Output

Page 19: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

19

A client program

Page 20: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

20

A client program (continue)

Page 21: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

21

Output of the client program

Page 22: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

22

Evaluation

• Merits– Fast: Search, Delete, and Insert functions have

a worst complexity that is linear

• Shortcomings– Inefficient use of space

Page 23: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

23

Enhancement:Represent multiple lists in a single array

Page 24: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

24

Insertion

Page 25: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

25

Insertion (continue)Cost: a single insertion may require as many as

MaxSize-1 moves

Page 26: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

26

Linked representation• Each node keeps an explicit information (link or

pointer) about the the location of other relevant nodes

• Singly linked list, also called chain

Page 27: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

27

Chain

Page 28: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

28

Chain (continue)

Page 29: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

29

Destructor - Θ(n)

Page 30: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

30

Length - Θ(n)

Page 31: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

31

Find - O(k)

Page 32: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

32

Find - O(n)

Page 33: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

33

Output - Θ(n)

Page 34: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

34

Delete the fourth node

• Locate the third and fourth nodes

• Link the third node to the fifth

• Free the fourth node

Page 35: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

35

Delete

Page 36: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

36

Delete (continue)

Page 37: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

37

InsertionTwo cases: k=0 and k<>0

Page 38: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

38

Insert function

Page 39: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

39

Insert function (continue) - O(k)

Page 40: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

40

Extensions to the Class ChainErase: delete the nodes in the chain

Page 41: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

41

Chain

last = 0; }

Page 42: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

42

Chain (continue)

, *last ;

Page 43: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

43

Append: add an element to the end of a chain - Θ(1)

Page 44: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

44

In case we need to visit elements of the whole chain

Chain<int> L; ……..

int len = L.Length();

int x, sum =0;

for (int i = 1; i <= len; i++) {

L.Find(i, x);

sum = sum + x;

}• The complexity of this code is Θ(n2)

• It is necessary to define a function which traverse the chain in Θ(n) time

Page 45: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

45

Chain Iterator Class

Location

Initialize()

next()

ChainIterator

Chain

first

Page 46: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

46

Chain Iterator Class

Page 47: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

47

Traverse the chain using IteratorChain<int> L; …….…..int *p;ChainIterator<int> c; p = c.Initialize(L); // c is iterator of chain Lwhile (p != NULL){

sum = sum + (*p);p = c.Next(); // advance p to point to next

// node in the chain}

Page 48: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

48

Circular List• singly linked circular list

• add the head node at the front of the list

Page 49: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

49

Search a circular list - O(n)

Page 50: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

50

Comparison with formula-based representation

Page 51: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

51

Doubly linked list

Page 52: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

52

Class definition

Page 53: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

53

Class definition (continue)

Page 54: 1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,

54

The end of Chapter 3Part 1