Advance Data Structure Review of Chapter 2 - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Advance Data Structure Review of Chapter 2

Description:

Advance Data Structure Review of Chapter 2 Review of Chapter 2 Arrays 1.3 Data Abstraction and Encapsulation 2.2 The Array As An abstract Data Type 2.5 The ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 44
Provided by: 540348
Category:

less

Transcript and Presenter's Notes

Title: Advance Data Structure Review of Chapter 2


1
Advance Data StructureReview of Chapter 2
  • ???

2
Review of Chapter 2 Arrays
  • 1.3 Data Abstraction and Encapsulation
  • 2.2 The Array As An abstract Data Type
  • 2.5 The Representation of Arrays
  • Example
  • 2.3 The Polynomial Abstract Data Type
  • 2.4 The Sparse Matrix Abstract Data Type
  • 2.6 The String Abstract Data Type

3
??Data Type
A data type is a collection of objects and a set
of operations that act on those objects.
??Abstract Data Type
An abstract data type(ADT) is a data type that is
organized in such a way that the specification of
the objects and the operations on the objects is
separated from the representation of the objects
and the implementation of the operations.
4
????
  • Data Types
  • Primitive Data Types
  • Accumulated Data Types - Array - Structures -
    Union
  • Abstract Data Types
  • Examples

5
Example Data Type of C
6
Accumulated Data Type
  • Array
  • Struct
  • Union

7
???? Abstract Data Types
  • ADTs ?????????????????????,???????????????
  • Data Structures ??,??????? ADT,??????????????,????
    ???????,????????????????????????????
  • ?????????(????)
  • ??????(???)

8
The Array as an Abstract Data Type
  • Array
  • A collection of data of the same type
  • An array is usually implemented as a consecutive
    set of memory locations
  • int list5, plist5
  • ADT definition of an Array
  • More general structure than "a consecutive set of
    memory locations.
  • An array is a set of pairs, ltindex, valuegt, in
    mathematical, call correspondence or mapping

9
class GeneralArray // objects A set of pairs
ltindex, valuegt where for each value of index in
// IndexSet there is a value of type float.
IndexSet is a finite ordered set of one // or
more dimensions, for example, 0, , n - 1 for
one dimension, (0, 0), // (0, 1), (0, 2), (1,
0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2) for
two dimensions, etc. public
GeneralArray(int j, RangeList list, float
initValue defaultValue) // The
constructor GeneralArray creates a j dimensional
array of floats the // range of the kth
dimension is given by the kth element of list.
For each // index i in the index set, insert
lti, initValuegt into the array. float
Retrieve(index i) // if (i is in the index
set of the array) return the float associated
with i in the // array else signal an
error. void Store(index i, float x)
// if (i is in the index set of the array)
delete any pair of the form lti, ygt //
present in the array and insert the new pair lti,
xgt else signal an error. // end of
GeneralArray
10
Array(??)
  • ??????????????
  • ???????????????
  • ??????,?????????
  • ???????????(index)???

const int ArraySize 100 int iArrayArraySize
11
????????????
Memory
m
m 2
m 4
m 6
int iArray100
m 198
?? sizeof(int) 2
12
????????????
Memory
m
m 22
6
m 44
5
4
m 66
3
m 88
2
m 110
1
0
m 132
0
1
2
3
4
5
6
7
8
9
10
int mesh711
?? sizeof(int) 2
13
???????
type array_namearraySize1 ...... arraySizen
????
int mesh711
float cube683
6
5
4
5
4
3
3
2
2
1
1
2
0
1
0
0
0
1
2
3
4
5
6
7
0
1
2
3
4
5
6
7
8
9
10
14
m
m 48
m 96
m 144
m 192
m 240
m 288
?? sizeof(int) 2
15
struct ????????
m
struct studentType char Name20 // ??
char Address30 // ?? char
PhoneNumber10 // ?? int Age // ??
char Department4 // ?? int Year // ??
char Class // ?? studentType Student1,
Student2
m 20
m 50
m 60
m 62
m 66
m 68
?? sizeof(int) 2
16
Self-Referential Structures
One or more of its components is a pointer to
itself. typedef struct list char data list
link list item1, item2, item3 item1.dataa
item2.datab item3.datac item1.linkitem
2.linkitem3.linkNULL
Construct a list with three nodes item1.linkitem
2 item2.linkitem3 malloc obtain a node
a
b
c
17
union ???
????
????
????
union char charValue int intValue
float floatValue dataCell
typedef union char charValue int
intValue float floatValue dataCellType
18
union ????????
union char charValue / 1 byte / int
intValue / 2 byte / float floatValue / 4
byte / dataCell
m
m 4
typedef struct char opcode union
int intValue char strValue256
data instruction
m
m1
m5
m 257
19
Example
  • Ordered List
  • Polynomial ADT
  • Sparse Matrix ADT
  • String ADT

20
Ordered List Examples
ordered (linear) list (item1, item2, item3, ,
itemn)
  • (MONDAY, TUEDSAY, WEDNESDAY, THURSDAY, FRIDAY,
    SATURDAYY, SUNDAY)
  • (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King,
    ACE)
  • (1941, 1942, 1943, 1944, 1945)
  • (a1, a2, a3, , an-1, an)
  • ()

21
Operations on Ordered List
  • Find the length, n , of the list.
  • Read the items from left to right (or right to
    left).
  • Retrieve the ith element.
  • Store a new value into the ith position.
  • Insert a new element at the position i , causing
    elements numbered i, i1, , n to become
    numberedi1, i2, , n1
  • Delete the element at position i , causing
    elements numbered i1, , n to become numbered i,
    i1, , n-1
  • array (sequential mapping)? (1)(4) O (5)(6) X

22
Polynomial(???)
23
(No Transcript)
24
Polynomial Representation 1
  • Representation by Degrees
  • private
  • int degree // degree ? MaxDegree
  • float coef MaxDegree 1
  • Example
  • Let A(x)Saixi , then
  • a.degree n,
  • a.coefian-i, ,0lt ilt n

25
Polynomial Representation 1
26
Polynomial Representation 2
  • Representation by Degrees, but dynamic allocation
    space
  • private
  • int degree //degree lt MaxDegree
  • float coef
  • polynomialpolynomial(int d)
  • degree d,
  • coefnew floatdegree1

27
Polynomial Representation 3
  • Representation by Terms.
  • Class polynomial
  • Class term
  • Friend polynomial
  • private
  • int exp
  • float coef
  • In polynomial class
  • In Implement file of polynomial class

private static term termArrayMaxTerms
// MaxTerms is a constant. //
termArrayMaxTerms shared by all polynomial
objects. static int free int start,
finish
term PolynomialtermArrayMaxTerms int
polynomialfree 0
28
Polynomial Representation 3-1

A(X)2X10001 B(X)X410X33X21
A.start A.finish B.start
B. finish free
coef
exp
0 1 2 3 4
5 6

In general, A.Finish A.Start n 1. For zero
polynomial, A.Finish A.Start 1
29
Polynomial Representation 3-2
k
numTerm
?
coef
0
1
2
k-1
?
expon
0
1
2
k-1
30
The Representations of Polynomials Compare
Representation 1 Representation 2 Representation 3
Advantages Simply algorithms for most operations Save space than R1 Save space when polynomial is sparse than R1, R2
Disadvantages Waste spaces if a.degree ltlt MaxDegree Waste spaces when polynomial is sparse Waste twice space when all terms are nonzero than R2, and algorithms is complex than R1, R2
31
????
  • ?????
  • As Representation 3,we take O(mn) at time
    complexity,if A(x) has m terms, B(x) has n terms.
    See Book pp.83-84
  • ?????

32
Sparse Matrix
col1 col2 col3 col4 col5 col6
row0 row1 row2 row3 row4 row5
66
53
(a)
(b) Figure 2.3Two matrices
15/15
8/36
sparse matrix
data structure?
33
?????????? define MAX_ROW 100 define MAX_COL
100 typedef int matrixMAX_ROWMAX_COL matrix
m1, m2 ???????,??,???????? 0?????? ???? 0
?????????????????????
34
Abstract Data Type Sparse Matrix
class SparseMatrix //objects a set of
triples, ltrow, column, valuegt, where row and
column are integers and // form a unique
combination, and value comes from the set item.
public SparseMatrix(int MaxRow,
int MaxCol) //create a
SparseMatrix that can hold up to MaxItems
MaxRowMaxCol and whose //maximum
row size is MaxRow and whose maximum column size
is MaxCol SparseMatrix
Transpose() // return the matrix
produced by interchanging the row and column
value of every triple. SparseMatrix
Add(SparseMatrix b) //if the
dimensions of a(this) and b are the same, return
the matrix produced by adding
//corresponding items, namely those with
identical row and column values. else return
//error. SparseMatrix
Multiply(a, b) //if number of columns
in a equals number of rows in b return the matrix
d produced by //multiplying a by b
according to the formula dij
Sum(aik(bkj), //where d(i, j)
is the (i, j)th element, k0 ((columns of a)
1) else return error.
35
Representation of Sparse Matrix
  • class SparseMatrix
  • class MatrixTerm
  • friend class SparseMatrix
  • private
  • int col, row, value
  • In class SparseMatrix
  • private
  • int col, row,Terms
  • MatrixTerm smArrayMaxTerms
  • // Note triples are ordered by row and
    within rows by columns

36
row col value a0 0 0 15 a1 0 3 22 a2 0 5 -
15 a3 1 1 11 a4 1 2 3 a5 2 3 -6 a6 4 0 91
a7 5 2 28
37
Sparse Matrix ????
  • ?? (transpose)
  • ??
  • ??

38
Abstract Data Type String
Class String //objects a finite set
of zero or more characters. public
String (char init, int m)
//Constructor that initializes this to string
init of length is m. int
operator(string t) // if the
string represented by this equal string t return
1(true) else return 0(false) int
operator!() // if this is empty then
return 1(TRUE) else return 0 (FALSE).
int Length() //return the number of
characters in this. String
Concat(String t) //return a string
whose elements are those of this followed by
those of t. String Substr(int i, int
j) //return the string containing j
characters of this at positions i, i1, ...,
ij-1, //if these are valid positions
of this else return empty string. int
Find(String pat) //return an index i
such that pat matches the substring of this that
begins at //position i. Return 1 if
pat is either empty or not a substring of
this.
39
Pattern Matching
  • Given two strings, string and pat, where pat is a
    pattern to be searched for in string
  • Two methods
  • a simple algorithm
  • O(SP) ? O(n2)
  • optimal algorithm (by Knuth-Morris-Pratt)
  • Linear complexity
  • O(SP) ? O(n)

40
A simple algorithm
O(nm)
41
The Failure Function
  • Definition
  • If p p0p1. . .pn-1 is a pattern, then its
    failure function, f is defined as
  • f(j) largest ilt j such that p0p1. . .pi
    pj-ipj-i1. . .pj , igt 0
  • -1, otherwise
  • Example

j 0 1 2 3 4 5 6 7 8 9 pat a b
c a b c a c a b f -1 -1 -1 0
1 2 3 -1 0 1
42
Rule for Optimal Pattern Matching
  • If a partial match is found such that si-j. .
    .si-1 p0p1. . .pj-1 and siltgtpj then matching may
    be resumed by comparing si and pf(j-1)1 if
    jltgt0. If j 0, continue by comparing si1 and p0

string a b c a ? ? . . . ?
pat a b c a b c a c a b f -1
-1 -1 0 1 2 3 -1 0 1
Continue here
43
Example of Optimal Pattern Matching
j 0 1 2 3 4 5 6 7 8 9 pat a b
c a b c a c a b f -1 -1 -1 0 1
2 3 -1 0 1
str c b a b c a a b c a b c a b c a c a b
Write a Comment
User Comments (0)
About PowerShow.com