Download Presentation
## Data Structure (Part I)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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.**ADT**Abstract Data Type public: public: int ReadData(int i) int ReadData(int i) void WriteData(int i, int i) void WriteData(int i, int i) private: private:**int**int int int int int int int int int 8 5 9 0 1 7 3 6 4 2 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 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.**index**value 2 13 13 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**2.2 The Array As an ADT**classGeneralArray1D { public: //Create an array of a given size; each element is initialized with initValue GeneralArray1D(intsize, floatinitValue); //If the index i is valid, return the value associated with it; //otherwise, throw an exception. floatRetrieve(intindex); //boolRetrieve(int index, float &result); //If the index i is valid, replace the old value associated with it by x; //otherwise, throw an exception. voidStore(index i, floatx); //boolStore(int i, floatx); };**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**exponent**coefficient 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.**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 { //Suppose public: Polynomial(); ~Polynomial(); Polynomial&Add(Polynomial&poly); Polynomial&Mult(Polynomial&poly); //Evaluate the polynomial at f and return the result. floatEval(floatf); } Using & to pass parameters and return value by reference.**-4**2 0 3 0 1 2 3 3x3 2x 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**Polynomial Representation - 1**• Implementation: private: intdegree; floatcoef[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: intdegree; float *coef; • Define coef so that its size is degree+1. Polynomial::Polynomial(intd) { degree = d; coef = newfloat [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)?**exp:1000**coef: 3 exp: 2 coef: 2 exp: 0 coef: 1 0 1 2 3 4 5 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**count**size Polynomial Representation - 3 classPolynomial; classTerm { friendPolynomial; private: floatcoef; intexp; }; classPolynomial { … private: Term *termarray; intsize; //size of termArray intcount; //number of nonzero terms };**exp: 3**coef: 3 exp: 3 coef: 2 exp: 2 coef: 1 0 1 2 3 4 5 Polynomial Representation - 3 • Requirement: • When inserting terms into Polynomial, each exponent must be unique (cannot be duplicated). • Incorrect example: Ambiguous! What exactly is the coefficient of the term with exponent of 3?**Comparison**• If the polynomial has few zero terms, Representation 3 uses memory space about twice as much space as does Representation 2. • Why?**exp: 4**coef: 5 exp: 2 coef: 1 exp: 1 coef: -2 exp: 0 coef: 7 0 1 2 4 5 3 2.3.2 Polynomial Addition • Example: a(x) = 3x3 + 2x2 b(x) = 5x4 +x2 – 2x + 7 exp: 3 coef: 3 exp: 2 coef: 2**aPos**bPos 0 1 2 3 4 5 Stopped. exp: 3 coef: 3 exp: 2 coef: 2 A: exp: 4 coef: 5 exp: 2 coef: 1 exp: 1 coef: -2 exp: 0 coef: 7 B: Stopped. A.termarray[0].exp = 3 > 2 = B.termarray[1].exp A.termarray[1].exp = 2 == 2 = B.termarray[1].exp A.termarray[0].exp = 3 < 4 = B.termarray[0].exp →A.termarray[1].exp + B.termarray[1].exp = 3 != 0 exp: 4 coef: 5 exp: 3 coef: 3 exp: 2 coef: 3 exp: 1 coef: -2 exp: 0 coef: 7 C: C(x) = 5x4 + 3x3 + 3x2 – 2x + 7**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 < 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 > 0 13 C.InsertNewTerm <B.termarray[aPos].exp, NewCoef >; 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 }**Analysis of Polynomial::Add()**• Steps to analyze time complexity: • Define instance characteristics. • Analysis time complexity line by line. • Consider worst case. • 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?