Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 1 Chapter 3 Data Representation Part 1

2 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

3 3 Data objects A 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, …}

4 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

5 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];

6 6 Linear Lists Data object in the form (e 1, e 2, …, e n ) where n is a finite natural number elements - e i ’s length - n empty list - when n = 0 Order - e 1 precedes e 2, e 2 precedes e 3, and so on

7 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

8 8 ADT specification

9 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

10 10 An example

11 11 Class Definition

12 12 Class Definition (Continue)

13 13 What if new fails?

14 14 Constructor

15 15 Find and Search

16 16 Delete

17 17 Insert

18 18 Output

19 19 A client program

20 20 A client program (continue)

21 21 Output of the client program

22 22 Evaluation Merits –Fast: Search, Delete, and Insert functions have a worst complexity that is linear Shortcomings –Inefficient use of space

23 23 Enhancement: Represent multiple lists in a single array

24 24 Insertion

25 25 Insertion (continue) Cost: a single insertion may require as many as MaxSize-1 moves

26 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

27 27 Chain

28 28 Chain (continue)

29 29 Destructor - Θ(n)

30 30 Length - Θ(n)

31 31 Find - O(k)

32 32 Find - O(n)

33 33 Output - Θ(n)

34 34 Delete the fourth node Locate the third and fourth nodes Link the third node to the fifth Free the fourth node

35 35 Delete

36 36 Delete (continue)

37 37 Insertion Two cases: k=0 and k<>0

38 38 Insert function

39 39 Insert function (continue) - O(k)

40 40 Extensions to the Class Chain Erase : delete the nodes in the chain

41 41 Chain last = 0; }

42 42 Chain (continue), *last ;

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

44 44 In case we need to visit elements of the whole chain Chain 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 Θ(n 2 ) It is necessary to define a function which traverse the chain in Θ(n) time

45 45 Chain Iterator Class Location Initialize() next() ChainIterator Chain first

46 46 Chain Iterator Class

47 47 Traverse the chain using Iterator Chain L; ……. ….. int *p; ChainIterator c; p = c.Initialize(L);// c is iterator of chain L while (p != NULL) { sum = sum + (*p); p = c.Next();// advance p to point to next // node in the chain }

48 48 Circular List singly linked circular list add the head node at the front of the list

49 49 Search a circular list - O(n)

50 50 Comparison with formula-based representation

51 51 Doubly linked list

52 52 Class definition

53 53 Class definition (continue)

54 54 The end of Chapter 3 Part 1


Download ppt "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,"

Similar presentations


Ads by Google