Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Data Structure (Part I) Chapter 2 – Arrays

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

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

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

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

6 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]; int 0 1 2 3 4 5 6 7 8 9 term In C++, to access the third element, use term[2] or *(term+2).

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

8 2.2 The Array As an ADT When considering array as an ADT –An array is a set of pairs,. Each index at most has a value associated with it. Correspondence / Mapping indexvalue 213

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

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

11 Applications of Arrays Ordered List / Linear List Polynomials (on a single variable) Sparse Matrices

12 Polynomial Example a(x) = 7 x 4 – 3x 2 + 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

13 Sum and Product of Two Polynomials Example: a(x) = 3x 3 + 2x – 4 b(x) = x 8 – 10x 5 – 3x 3 + 1 a(x) + b(x) = x 8 – 10x 5 + (3-3)x 3 + 2x + (-4+1) = x 8 – 10x 5 + 2x – 3 a(x) × b(x) = (3x 3 + 2x – 4)(x 8 – 10x 5 – 3x 3 + 1) = 3x 3  (x 8 – 10x 5 – 3x 3 + 1) + 2x  (x 8 – 10x 5 – 3x 3 + 1) + (-4)  (x 8 – 10x 5 – 3x 3 + 1)

14 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); } Using & to pass parameters and return value by reference.

15 2.3.1 Polynomial Representation Representation 1 –Represent polynomials in C++ array Index represent exponent. The coefficient of x i is stored in coef[i]. –Example: a(x) = 3x 3 + 2x – 4 -4 0 2 1 0 2 3 3 2x2x 3x33x3

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

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

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

19 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) = x 1000 + x 2 + 1 Consider: –At least how many elements are required? –Eventually how many elements are used to store b(x)?

20 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) = 3x 1000 + 2x 2 + 1 exp:1000 coef: 3 0 exp: 2 coef: 2 1 exp: 0 coef: 1 2345

21 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 }; count size

22 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 2345 Ambiguous! What exactly is the coefficient of the term with exponent of 3?

23 Comparison Representation 2Representation 3 Time complexity of searching the term with the exponent i. O(1)O(count) Space ComplexityO(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?

24 2.3.2 Polynomial Addition Example: a(x) = 3x 3 + 2x 2 b(x) = 5x 4 +x 2 – 2x + 7 exp: 4 coef: 5 0 exp: 2 coef: 1 1 exp: 1 coef: -2 245 exp: 3 coef: 3 exp: 2 coef: 2 exp: 0 coef: 7 3

25 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 012345 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) = 5x 4 + 3x 3 + 3x 2 – 2x + 7 Stopped.

26 Algorithm Polynomial 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.count 4 if (termarray[aPos].exp > B.termarray[bPos].exp) 5 C.InsertNewTerm ; 6 aPos++; 7 else if (termarray[aPos].exp < B.termarray[bPos].exp) 8 C.InsertNewTerm ; 9 bPos++; 10 else 11 NewCoef = termarray[aPos].coef +B.termarray[bPos].coef; 12 if NewCoef > 0 13 C.InsertNewTerm ; 14 end if 15 aPos++, bPos++; 16 end if 17 end while 18 for each remaining term t in this object 19 Add t to C; 20 end for 21 for each remaining term t in B 22 Add t to C; 23 end for 24 Return C; 25}

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

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

29 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; }

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


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

Similar presentations


Ads by Google