Upload
fay-rose
View
212
Download
0
Embed Size (px)
Citation preview
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, linked, indirect addressing, and simulated-pointer representations
– Chains, circular lists, and doubly linked lists
• Applications
– Bin sort, radix sort
– Equivalence class
– Convex hull
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, …}
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
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
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
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
ADT specification
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
An example
11
Class Definition
12
Class Definition (Continue)
13
What if new fails?
14
Constructor
15
Find and Search
16
Delete
17
Insert
18
Output
19
A client program
20
A client program (continue)
21
Output of the client program
22
Evaluation
• Merits– Fast: Search, Delete, and Insert functions have
a worst complexity that is linear
• Shortcomings– Inefficient use of space
23
Enhancement:Represent multiple lists in a single array
24
Insertion
25
Insertion (continue)Cost: a single insertion may require as many as
MaxSize-1 moves
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
Chain
28
Chain (continue)
29
Destructor - Θ(n)
30
Length - Θ(n)
31
Find - O(k)
32
Find - O(n)
33
Output - Θ(n)
34
Delete the fourth node
• Locate the third and fourth nodes
• Link the third node to the fifth
• Free the fourth node
35
Delete
36
Delete (continue)
37
InsertionTwo cases: k=0 and k<>0
38
Insert function
39
Insert function (continue) - O(k)
40
Extensions to the Class ChainErase: delete the nodes in the chain
41
Chain
last = 0; }
42
Chain (continue)
, *last ;
43
Append: add an element to the end of a chain - Θ(1)
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
45
Chain Iterator Class
Location
Initialize()
next()
ChainIterator
Chain
first
46
Chain Iterator Class
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}
48
Circular List• singly linked circular list
• add the head node at the front of the list
49
Search a circular list - O(n)
50
Comparison with formula-based representation
51
Doubly linked list
52
Class definition
53
Class definition (continue)
54
The end of Chapter 3Part 1