Data Structure

Download Report

Transcript Data Structure

Data Structure (Part I)
Chapter 2 – Arrays
2.1.2 Data Abstraction and
Encapsulation in C++
• Section 1.3
– Data Encapsulation
• Also called information hiding
• The concealing of the implementation details of a
data object from the outside world.
– Data Abstraction
• The separation between the specification of a data
object and its implementation.
2.1.2 Data Abstraction and
Encapsulation in C++
• Data Type
– A collection of objects and a set of operations
that act on those objects.
• Data Encapsulation
– In C++, data encapsulation is enforced
Declaring all data members of a class to be
private or protected.
• External access to data members can be achieved
by defining public member functions that get and
set data members.
2.1.2 Data Abstraction and
Encapsulation in C++
• Data Abstraction
– Abstract Data Type (ADT)
• A data type in which the specification of objects
and operations on the objects is separated from
the representation of and the implementation the
objects.
• Implementation-independent.
Abstract Data Type
ADT
public:
public:
int ReadData(int i)
int ReadData(int i)
void WriteData(int i, int i)
void WriteData(int i, int i)
private:
private:
2.2 The Array As an ADT
• From a perspective on implementation
issues
– An array is a consecutive set of memory
locations with each containing data of the
same type.
– Example:
int term[10];
term
int
int
int
int
int
int
int
int
int
int
0
1
2
3
4
5
6
7
8
9
In C++, to access the third element, use term[2] or *(term+2).
2.2 The Array As an ADT
• What is the advantage of preserving data
in an array? Because it can support
– __________ access, and
– __________ access
through indices.
• The index is used like an ID number for
the value stored in array.
2.2 The Array As an ADT
• When considering array as an ADT
– An array is a set of pairs, <index, value>.
• Each index at most has a value associated with it.
• Correspondence / Mapping
index
2
value
13
13
2.2 The Array As an ADT
class GeneralArray1D {
public:
//Create an array of a given size; each element is initialized with initValue
GeneralArray1D(int size, float initValue);
//If the index i is valid, return the value associated with it;
//otherwise, throw an exception.
float Retrieve(int index);
//bool Retrieve(int index, float &result);
//If the index i is valid, replace the old value associated with it by x;
//otherwise, throw an exception.
void Store(index i, float x);
//bool Store(int i, float x);
};
2.2 The Array As an ADT
• GeneralArray1D is more flexible about the
composition of the index set.
– Integers in the index set is not required to be
consecutive.
– Range checking can be provided to ensure
valid access.
– Time complexity to retrieve a specific index is
an issue.
• C++ array:
– Finding the value associated with the index i: _____.
• GeneralArray1D:
– Finding the value associated with the index i: _____.
Applications of Arrays
• Ordered List / Linear List
• Polynomials (on a single variable)
• Sparse Matrices
Polynomial
• Example
exponent
a(x) = 7 x 4 – 3x2 + 1
coefficient
• The degree of a polynomial is the largest exponent.
– The degree of a(x) is _________.
• a(x) has ______ terms.
– They are _______, _______, and ________.
• The coefficients are _____, ______, and ______.
• The exponents are _____, ______, and ______.
– Normally, terms with zero coefficients are not displayed.
Sum and Product of Two
Polynomials
• Example:
a(x) = 3x3 + 2x – 4
b(x) = x8 – 10x5 – 3x3 + 1
• a(x) + b(x) = x8 – 10x5 + (3-3)x3 + 2x + (-4+1)
= x8 – 10x5 + 2x – 3
• a(x) × b(x) = (3x3 + 2x – 4)(x8 – 10x5 – 3x3 + 1)
= 3x3(x8 – 10x5 – 3x3 + 1) +
2x(x8 – 10x5 – 3x3 + 1) +
(-4)(x8 – 10x5 – 3x3 + 1)
2.3 The Polynomial ADT
class Polynomial {
e0
en
e1
p
(
x
)

a
x

a
x

...

a
x
//Suppose
0
1
n
public:
Polynomial();
~Polynomial();
Polynomial &Add(Polynomial &poly);
Polynomial &Mult(Polynomial &poly);
//Evaluate the polynomial at f and return the result.
float Eval(float f);
}
Using & to pass parameters and return value by reference.
2.3.1 Polynomial Representation
• Representation 1
– Represent polynomials in C++ array
• Index represent exponent.
• The coefficient of xi is stored in coef[i].
– Example: a(x) = 3x3 + 2x – 4
-4
2
0
3
0
1
2
3
3x3
2x
Polynomial Representation - 1
– Implementation:
private:
int degree;
float coef[MaxDegree + 1];
• MaxDegree: a constant that represents that
largest-degree to be represented.
– Advantage:
• Simple
• Fast
– Disadvantage:
Polynomial Representation - 1
– Advantage:
• Simple.
• Fast.
– Time complexity to retrieve a term with a specific
exponent: ___________.
– Disadvantage:
• Could be very wasteful in its use of computer
memory if degree is much less than MaxDegree.
Polynomial Representation - 2
• Also represented in C++ array, but use
dynamical allocation.
• Implementation:
int degree;
float *coef;
• Define coef so that its size is degree+1.
Polynomial::Polynomial(int d)
{
degree = d;
coef = new float [degree + 1];
}
Polynomial Representation - 2
– Disadvantage:
• Could also be very wasteful in its use of computer
memory if the polynomial is sparse.
– too many zero terms.
• Example:
b(x) = x1000 + x2 + 1
Consider:
– At least how many elements are required?
– Eventually how many elements are used to store b(x)?
Polynomial Representation - 3
• To solve the problem of Representation 1 and 2,
we store only the nonzero terms.
• The exponent now is independent of the index of
the array.
– A nonzero term is stored in an element of the array.
– Each element has to preserve both exponent and
coefficient.
• Example: c(x) = 3x1000 + 2x2 + 1
exp:1000
exp: 2
exp: 0
coef: 3
coef: 2
coef: 1
0
1
2
3
4
5
Polynomial Representation - 3
class Polynomial;
class Term {
friend Polynomial;
count
private:
float coef;
exp
exp
exp
termarray
int exp;
coef
coef
coef
};
class Polynomial {
size
…
private:
Term *termarray;
int size;
//size of termArray
int count;
//number of nonzero terms
};
Polynomial Representation - 3
• Requirement:
– When inserting terms into Polynomial, each
exponent must be unique (cannot be
duplicated).
– Incorrect example:
exp: 3
exp: 3
exp: 2
coef: 3
coef: 2
coef: 1
0
1
2
3
4
5
Ambiguous! What exactly is the coefficient of the
term with exponent of 3?
Comparison
Time complexity
of searching the
term with the
exponent i.
Space Complexity
Representation 2
Representation 3
O(1)
O(count)
O(degree)
O(count)
If the polynomial has few zero terms, Representation 3 uses memory space
about twice as much space as does Representation 2.
•Why?
2.3.2 Polynomial Addition
• Example:
a(x) = 3x3 + 2x2
b(x) = 5x4 +x2 – 2x + 7
exp: 3
exp: 2
coef: 3
coef: 2
exp: 4
exp: 2
exp: 1
exp: 0
coef: 5
coef: 1
coef: -2
coef: 7
0
1
2
3
4
5
0
1
2
3
4
5
aPos
Stopped.
A:
B:
exp: 3
exp: 2
coef: 3
coef: 2
exp: 4
exp: 2
exp: 1
exp: 0
coef: 5
coef: 1
coef: -2
coef: 7
Stopped.
bPos
A.termarray[0].exp = 2
A.termarray[1].exp
3 ==
<2
>
4 2==B.termarray[1].exp
B.termarray[0].exp
B.termarray[1].exp
→A.termarray[1].exp + B.termarray[1].exp = 3 != 0
C:
exp: 4
exp: 3
exp: 2
exp: 1
exp: 0
coef: 5
coef: 3
coef: 3
coef: -2
coef: 7
C(x) = 5x4
+
3x3 +
3x2 –
2x +
7
Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Polynomial Polynomial::Add(Polynomial B)
{
Declare C as Polynomial to be the result;
aPos = 0, bPos = 0;
while aPos < count and bPos < B.count
if (termarray[aPos].exp > B.termarray[bPos].exp)
C.InsertNewTerm < termarray[aPos].exp, termarray[aPos].coef>;
aPos++;
else if (termarray[aPos].exp < B.termarray[bPos].exp)
C.InsertNewTerm <B.termarray[bPos].exp, B.termarray[bPos].coef>;
bPos++;
else
NewCoef = termarray[aPos].coef +B.termarray[bPos].coef;
if NewCoef > 0
C.InsertNewTerm <B.termarray[aPos].exp, NewCoef >;
end if
aPos++, bPos++;
end if
end while
for each remaining term t in this object
Add t to C;
end for
for each remaining term t in B
Add t to C;
end for
Return C;
}
Analysis of Polynomial::Add()
•
Steps to analyze time complexity:
1. Define instance characteristics.
2. Analysis time complexity line by line.
•
Consider worst case.
3. Compute the total complexity.
Analysis of Polynomial::Add()
•
Let m and n be the number of nonzero terms in A
and B.
–
–
•
Line 1-2: O(1).
Line 3-22: aPos or bPos increase by 1 each time until
aPos > m and bPos > n. The total number of iterations
of the while- and for-loop is bounded by m + n.
Therefore, the total time complexity is O(m + n).
Implementation
Polynomial &Polynomial::Add(Polynomial &B)
{
Polynomial *C = new Polynomial();
int aPos = 0, bPos = 0;
while (aPos < count && bPos < B.count) {
if (termarray[aPos].exp > B.termarray[bPos].exp) {
C->NewTerm (termarray[aPos].exp, termarray[aPos].coef);
aPos++;
}
else if (termarray[aPos].exp < B.termarray[bPos].exp) {
C->NewTerm (B.termarray[bPos].exp, B.termarray[bPos].coef);
bPos++;
}
else {
NewCoef = termarray[aPos].coef +B.termarray[bPos].coef;
if (NewCoef > 0)
C->NewTerm (B.termarray[aPos].exp, NewCoef);
end if
aPos++; bPos++;
}
}
for ( ; aPos < count; aPos++)
C->NewTerm (termarray[aPos].exp, termarray[aPos].coef);
for ( ; bPos < count; bPos++)
C->NewTerm (termarray[bPos].exp, termarray[bPos].coef);
return *C;
}
Invoking Polynomial::Add()
Polynomial A, B;
//Construct A and B
…
Polynomial &C = A.Add(B);
• “&” tells compiler that C is exactly the object
returned by Add().
• Discuss:
– what happens if we do not use pass-by-reference?