1 chapter 3 data representation part 1. 2 goals introduce the different ways in which data may be...
TRANSCRIPT
![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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/1.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/2.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/3.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/4.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/5.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/6.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/7.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/8.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/9.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/10.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/11.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/12.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/13.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/14.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/15.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/16.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/17.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/18.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/19.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/20.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/21.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/22.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/23.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/24.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/25.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/26.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/27.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/28.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/29.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/30.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/31.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/32.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/33.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/34.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/35.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/36.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/37.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/38.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/39.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/40.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/41.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/42.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/43.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/44.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/45.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/46.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/47.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/48.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/49.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/50.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/51.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/52.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/53.jpg)
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,](https://reader035.vdocument.in/reader035/viewer/2022070408/56649e555503460f94b4c77a/html5/thumbnails/54.jpg)
54
The end of Chapter 3Part 1