data structure (part i) chapter 2 – arrays. 2.1.2 data abstraction and encapsulation in c++...

30
Data Structure (Part I) Chapter 2 – Arrays

Upload: arron-nichols

Post on 11-Jan-2016

233 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

Data Structure (Part I)

Chapter 2 – Arrays

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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.

Page 5: 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

Abstract Data Type

int ReadData(int i)

void WriteData(int i, int i)

int ReadData(int i)

void WriteData(int i, int i)

ADT

public: public:

private: private:

Page 6: 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

2.2 The Array As an ADT

• From a perspective on implementation issues – An array is a consecutive set of memory locati

ons with each containing data of the same type.

– Example:

int term[10];

int

0

int

1

int

2

int

3

int

4

int

5

int

6

int

7

int

8

int

9

term

In C++, to access the third element, use term[2] or *(term+2).

Page 7: 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

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.

Page 8: 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

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 value

2 13

13

Page 9: 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

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);

};

Page 10: 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

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: _____.

Page 11: 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

Applications of Arrays

• Ordered List / Linear List

• Polynomials (on a single variable)

• Sparse Matrices

Page 12: 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

Polynomial

• Example

a(x) = 7 x 4 – 3x2 + 1

• 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.

exponent

coefficient

Page 13: 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

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)

Page 14: 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

2.3 The Polynomial ADT

class Polynomial {//Suppose public:

Polynomial();~Polynomial();Polynomial &Add(Polynomial &poly);Polynomial &Mult(Polynomial &poly);//Evaluate the polynomial at f and return the result.float Eval(float f);

}

nen

ee xaxaxaxp ...)( 1010

Using & to pass parameters and return value by reference.

Page 15: 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

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

0

2

1

0

2

3

3

2x

3x3

Page 16: 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

Polynomial Representation - 1

– Implementation:

private:

int degree;

float coef[MaxDegree + 1];• MaxDegree: a constant that represents that larges

t-degree to be represented.

– Advantage:• Simple• Fast

– Disadvantage:

Page 17: 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

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 mem

ory if degree is much less than MaxDegree.

Page 18: 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

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

}

Page 19: 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

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)?

Page 20: 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

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 coe

fficient. • Example: c(x) = 3x1000 + 2x2 + 1

exp:1000

coef: 3

0

exp: 2

coef: 2

1

exp: 0

coef: 1

2 3 4 5

Page 21: 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

Polynomial Representation - 3

class Polynomial;class Term {

friend Polynomial;private:

float coef; int exp;

};class Polynomial {

…private:

Term *termarray; int size; //size of termArray int count; //number of nonzero terms

};

expcoef

expcoef

expcoef

termarray

count

size

Page 22: 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

Polynomial Representation - 3• Requirement:

– When inserting terms into Polynomial, each exponent must be unique (cannot be duplicated).

– Incorrect example:

exp: 3

coef: 3

0

exp: 3

coef: 2

1

exp: 2

coef: 1

2 3 4 5

Ambiguous! What exactly is the coefficient of the term with exponent of 3?

Page 23: 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

Comparison

Representation 2 Representation 3

Time complexity of searching the term with the exponent i.

O(1) O(count)

Space Complexity 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?

Page 24: 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

2.3.2 Polynomial Addition

• Example:a(x) = 3x3 + 2x2

b(x) = 5x4 +x2 – 2x + 7

exp: 4

coef: 5

0

exp: 2

coef: 1

1

exp: 1

coef: -2

2 4 5

exp: 3

coef: 3

exp: 2

coef: 2

exp: 0

coef: 7

3

Page 25: 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

exp: 4

coef: 5

exp: 2

coef: 1

exp: 1

coef: -2

exp: 3

coef: 3

exp: 2

coef: 2

exp: 0

coef: 7

exp: 4

coef: 5

exp: 3

coef: 3

exp: 2

coef: 3

exp: 1

coef: -2

A:

B:

aPos

bPos

C:exp: 0

coef: 7

A.termarray[0].exp = 3 < 4 = B.termarray[0].exp

0 1 2 3 4 5

A.termarray[0].exp = 3 > 2 = B.termarray[1].expA.termarray[1].exp = 2 == 2 = B.termarray[1].exp

→A.termarray[1].exp + B.termarray[1].exp = 3 != 0

Stopped.

C(x) = 5x4 + 3x3 + 3x2 – 2x + 7

Stopped.

Page 26: 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

AlgorithmPolynomial Polynomial::Add(Polynomial B){

1 Declare C as Polynomial to be the result;2 aPos = 0, bPos = 0;3 while aPos < count and bPos < B.count4 if (termarray[aPos].exp > B.termarray[bPos].exp) 5 C.InsertNewTerm < termarray[aPos].exp, termarray[aPos].coef>;6 aPos++;7 else if (termarray[aPos].exp < B.termarray[bPos].exp)8 C.InsertNewTerm <B.termarray[bPos].exp, B.termarray[bPos].coef>;9 bPos++;10 else 11 NewCoef = termarray[aPos].coef +B.termarray[bPos].coef;12 if NewCoef > 013 C.InsertNewTerm <B.termarray[aPos].exp, NewCoef >; 14 end if15 aPos++, bPos++;16 end if17 end while18 for each remaining term t in this object19 Add t to C;20 end for21 for each remaining term t in B22 Add t to C;23 end for24 Return C;25 }

Page 27: 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

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.

Page 28: 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

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).

Page 29: 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

ImplementationPolynomial &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;}

Page 30: 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

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?