You are on page 1of 61

V.

Kiu d liu tru tng

Cc khi nim
Xy dng ADT
Cc ton t
K tha

77
V. Kiu d liu tru tng

Kiu d liu tru tng (ADT) kiu c


nh ngha m t cho mt s vt
Cc tnh cht
ng gi cc bin v cc hm c khai bo trong
mt khi, cho php che du cc thnh vin
K tha ADT (dn xut) c xy dng trn nn
mt ADT khc (c s), c th s dng li cc thnh
phn ca ADT c s
i tng mt bin thuc mt ADT

78
V. Kiu d liu tru tng

V d:
Mt hnh ch nht (Rectangle) c xc nh bi:
ta gc trn bn tri: x1, y1
ta gc di bn phi: x2, y2
tnh din tch: Area = (y2 y1) * (x2 x1)

Mt hnh van (Elipse) ni tip trong hnh ch nht:


tnh din tch: Area = Rectagle::Area * / 4

79
V. Kiu d liu tru tng

V d (struct):
struct Rectangle
{
double x1, y1;
double x2, y2;
double Area() { return (y2 - y1) * (x2 - x1); }
};

struct Elipse : public Rectangle


{
double Area()
{
return Rectangle::Area() * 3.14 / 4;
}
};

80
V. Kiu d liu tru tng

V d (class):
class Rectangle
{
public:
double x1, y1;
double x2, y2;
double Area() { return (y2 - y1) * (x2 - x1); }
};

class Elipse : public Rectangle


{
public:
double Area()
{
return s = Rectangle::Area() * 3.14 / 4;
}
};

81
V. Kiu d liu tru tng

V d (cc i tng):

void main()
{
Rectangle r = { 1, 1, 6, 3 };

Elipse e;
e.x1 = 12; e.y1 = 2;
e.x2 = 17; e.y2 = 7;

cout << r.Area() << ' ' << e.Area();


}

82
V. Kiu d liu tru tng

Cc vng truy cp

public protected private

83
V. Kiu d liu tru tng

Cu trc ca ADT
class tn_ADT
{
private|protected|public:
// cc bin thnh vin (cc thuc tnh)
private|protected|public:
// cc hm to
public:
// hm hy
private|protected|public:
// cc hm thnh vin khc (cc phng thc)
public:
// cc ton t thnh vin

// cc hm v cc ton t friend
};

84
V. Kiu d liu tru tng

Cu trc ca ADT
Cc hm n gin (khng cha cc vng lp hoc rt
ngn) c th nh ngha inline

class tn_ADT
{
kiu tn_hm(danh sch tham s)
{
// cc biu thc
}
};

85
V. Kiu d liu tru tng

Cu trc ca ADT
Cc hm phc tp nn nh ngha ngoi khi khai
bo ADT

class tn_ADT
{
kiu tn_hm(danh sch tham s);
};

kiu tn_ADT::tn_hm(danh sch tham s)


{
// cc biu thc
}

86
V. Kiu d liu tru tng

Hm to v hm hy
Hm to (constructor)
Dng thit lp gi tr cho cc bin hoc cp pht
b nh cho cc mng ng
c gi trong cc biu thc khai bo i tng
hoc cp pht b nh

Hm hy (destructor)
Dng gii phng b nh cp pht
c gi khi kt thc khi khai bo i tng hoc
trong biu thc gii phng b nh

87
V. Kiu d liu tru tng

Hm to v hm hy

class tn_ADT
{
public:
tn_ADT(); // hm to mc nh
tn_ADT(danh sch tham s); // hm to thit lp
tn_ADT(const tn_kiu &); // hm to copy
~tn_ADT(); // hm hy
};

88
V. Kiu d liu tru tng

nh ngha hm to (inline)

class tn_ADT
{
public:
tn_ADT(danh sch tham s)
: tn_bin(biu thc)
, ...
{
// cc biu thc
}
};

89
V. Kiu d liu tru tng

V d (class Time)
class Time
{
long ticks;
public:
Time() { ticks = 0; }
Time(long value) : ticks(value) { }
Time(const Time & t) : ticks(t.ticks) { }
~Time() { }
public:
long Seconds() { return ticks / 1000; }
long Minutes() { return Seconds() / 60; }
long Hours() { return Seconds() / 3600; }
};

90
V. Kiu d liu tru tng

V d (class Time)
void main()
{
Time *p;
Time t1; // gi Time()
Time t2(1000); // gi Time(long)
Time t3 = t2; // gi Time(const Time &)

p = new Time(100); // gi Time(long)

cout << t2.Seconds() << endl;


cout << p->Seconds() << endl;

delete p; // gi ~Time() ca *p

} // gi ~Time() ca t3, t2 v t1

91
V. Kiu d liu tru tng

V d (class String)
Cc bin thnh vin v cc hm static

class String
{
char *data; // Vng d liu cha cc k t
int len; // S k t
public:
// Ly di xu src
static int GetLength(const char * src);

// copy xu src vo dst


static void Copy(char * dst, const char * src);

92
V. Kiu d liu tru tng

V d (class String)
Cc hm private
private:
// Hm to i lng cha c n k t
String(int n) { createData(n); }

// copy xu src vo data


void copyData(const char * src) { Copy(data, src); }

// To vng d liu cha n k t


void createData(int n)
{
len = n;
data = new char[n + 1];
data[n] = 0;
}

93
V. Kiu d liu tru tng

V d (class String)
Cc hm to v hm hy
public:
String() { createData(0); }

String(const char * src) {


createData(GetLength(src));
copyData(src);
}

String(const String & src) {


createData(src.len);
copyData(src.data);
}

~String() { delete[] data; }

94
V. Kiu d liu tru tng

V d (class String)
Cc hm public
public:
// Ly s k t
int Length() { return len; }

// So snh vi i tng src


int Compare(const String & src) const
{
return Compare(src.data);
}

// So snh vi xu src
int Compare(const char * src) const;

95
V. Kiu d liu tru tng

V d (class String)
Cc ton t

public:
String operator=(const char * src);
String operator=(String & src);

// Cc ton t khc

}; // class String

96
V. Kiu d liu tru tng

V d (class String)
nh ngha cc hm
int String::GetLength(const char * src)
{
register int i = 0;
while (src[i]) ++i;
return i;
}

void String::Copy(char *dst, const char * src)


{
for (register int i = 0; src[i]; i++)
dst[i] = src[i];
}

97
V. Kiu d liu tru tng

V d (class String)
nh ngha cc hm

int String::Compare(const char *src) const


{
for (int i = 0; i <= len; i++)
{
if (data[i] != src[i])
return (data[i] > src[i]? 1: -1);
}
return 0;
}

98
V. Kiu d liu tru tng

Ton t gn
{
Time t1, t2;
String s1, s2;

t1 = t2; t1.ticks = t2.ticks

s1 = s2; s1.len = s2.len


s1.data = s2.data

} delete[] s2.data
delete[] s1.data sinh ra li v s1.data b xa

99
V. Kiu d liu tru tng

Ton t gn

// B sung ton t gn cho String


String& operator=(const String & right)
{
delete [] data; // xa vng d liu c
createData(right.len); // to vng d liu mi
copyData(right.data); // copy d liu t src

return *this; // tr v bn thn i tng


}

100
V. Kiu d liu tru tng

Cc ton t s hc
// B sung ton t s hc cho Time
Time operator+(const Time right)
{
return Time(ticks + right.ticks);
}

// B sung ton t s hc cho String


String operator+(const String & right)
{
String s(len + right.len);
copy(s.data, data);
copy(s.data + len, right.data); (const Time right)
(const String & right)
return s;
C g khc nhau ?
}

101
V. Kiu d liu tru tng

Cc ton t kt hp
// B sung ton t kt hp cho Time
Time & operator+=(const Time right) {
ticks += right.ticks;
return *this;
}

// B sung ton t kt hp cho String


String operator+=(const String & right) {
String s(len + right.len);
copy(s.data, data);
copy(s.data + len, right.data);

len = s.len;
char *p = data; data = s.data; s.data = p;
return *this;
}

102
V. Kiu d liu tru tng

Cc ton t so snh

// B sung ton t so snh cho Time


int operator==(const Time right) const
{
return (ticks == right.ticks);
}

// B sung ton t so snh cho String


int operator==(const String & right) const
{
return (Compare(right.data) == 0);
}

103
V. Kiu d liu tru tng

Ton t ch s

// B sung ton t ch s cho String


char & operator[](int index)
{
return data[index];
}

104
V. Kiu d liu tru tng

Ton t hm
kiu operator()(danh sch tham s) { ... }

V d

Ly gi tr ca hm theo mt tp cc i s
Truy cp phn t ca mng n chiu

105
V. Kiu d liu tru tng

Cc ton t tng/gim
// B sung ton t ++ cho Time

// Trng hp ton t bn phi


Time & operator++() {
ticks++;
return *this;
}

// Trng hp ton t bn tri


friend Time & operator++(Time & right) {
right.ticks++;
return right;
}

106
V. Kiu d liu tru tng

Cc ton t lung vo/ra


// B sung ton t lung ra cho Time
friend ostream & operator<<(ostream & left, Time & right)
{
left << right.Hours() << ':'
<< right.Minutes() % 60 << ':'
<< right.Seconds() % 60 << '.'
<< right.ticks % 1000;
return left;
}

// B sung ton t lung ra cho String


friend ostream & operator<<(ostream & left, Time & right)
{
return left << right.data;
}

107
V. Kiu d liu tru tng

Cc ton t p kiu

// B sung ton t p sang kiu long cho Time


operator long() { return ticks; }

// B sung ton t p sang kiu xu k t cho String


operator char*() { return data; }

108
V. Kiu d liu tru tng

Xy dng class PhanSo m t kiu phn s gm cc thnh phn sau:


Hai bin a, b cha t s v mu s
Cc hm to
Cc ton t cng, tr, nhn, chia v kt hp
Ton t lung ra
VD. PhanSo a(2, 4); cout << a + 1; 3/2

Xy dng class Complex m t kiu s phc gm cc thnh phn sau:


Hai bin r, i cha phn thc v phn o
Cc hm to
Cc ton t cng, tr, nhn, chia v kt hp
Ton t hm ly m-un
Ton t lung ra
VD. Complex z(1, 3); cout << z + 1; (2, 3i)

109
V. Kiu d liu tru tng

M hnh
class ADT_c_s {
protected|public:
virtual void foo() { ... }
};

class ADT_dn_xut : public|protected|private ADT_c_s {


protected|public:
void foo()
{
// phn m rng
ADT_c_s::foo();
// phn m rng
}
};

110
V. Kiu d liu tru tng

Cc vng truy cp

ADT_dn_xut : public ADT_c_s

public protected private

ADT_c_s

111
V. Kiu d liu tru tng

V d v s k tha
class B {
protected: int x;
public:
B(int a = 0) : x(a) { }
virtual void Print() { cout << x; }
void Inc() { x++; }
};

class D : public B {
int x;
public:
D(int a, int b) : x(a), B(b) { }
void Print() {
cout << x << ", ";
B::Print();
}
};

112
V. Kiu d liu tru tng

V d v s k tha
void main()
{
B b(5);
D d(1, 2);

d.Inc();

B *p = &b;
p->Print(); // gi B::Print() -> 5
p = &d;
p->Print(); // gi D::Print() -> 1, 3
}

113
V. Kiu d liu tru tng

Lp c s tru tng
class ADT_c_s {
...
virtual void foo() = 0;
};

class ADT_dn_xut : public|protected|private ADT_c_s {


...
void foo()
{
// ...
}
};

114
V. Kiu d liu tru tng

Lp c s tru tng

class Shape
{
String _name;
public:
Shape(const char *name) : _name(name) { }
void Print()
{
cout << _name << ": Area = " << Area() << endl;
}
virtual double Area() = 0;
};

115
V. Kiu d liu tru tng

K tha t lp c s tru tng

class Rectangle : public Shape


{
int _w, _h;
public:
Rectangle(int a, int b)
: Shape("Rectangle"), _w(a), _h(b) { }
double Area() { return _w * _h; }
};

116
V. Kiu d liu tru tng

K tha lp c s tru tng


class Triangle : public Shape
{
int _a, _b, _c;
public:
Triangle(int a, int b, int c)
: Shape("Triangle"), _a(a), _b(b), _c(c) { }
double Area()
{
double s = (_a + _b + _c) / 2;
return sqrt(s * (s - _a) * (s - _b) * (s - _c));
}
};

117
V. Kiu d liu tru tng

V d
void main()
{
Shape *s[2];

s[0] = new Rectangle(2, 5);


s[1] = new Triangle(3, 4, 5);

for (int i = 0; i < 2; i++)


{ Rectangle: Area = 10
s[i]->Print(); Triangle: Area = 12
delete s[i];
}
}

118
VI. Cc cu trc d liu c bn

ADT mu
template <class _T, int n> class Shape
{
_T edge[n]; // cc cnh
public:
_T Bound();
};

template <class _T, int n> _T Shape<_T, n>::Bound()


{
int c = 0;
for (int i = 0; i < n; i++) c += edge[i];
return c;
}

void main()
{
Shape<int, 3> tamGiac; // to ra class Shape c 3 cnh kiu int
Shape<double, 4> tuGiac;// to ra class Shape c 4 cnh kiu double
}

119
V. Kiu d liu tru tng

class Matrix m t mt ma trn c cho di y


template <class _T> class Matrix {
_T **data;
int rows, cols; // s hng v s ct

void createData(); // to vng d liu t rows v cols


void createData(int m, int n); // to vng d liu m hng, n ct
void deleteData(); // xa d liu

public:
Matrix() : data(0) { }
Matrix(int m, int n = 0); // to ma trn mxn hoc mxm
Matrix(int m, int n, const _T*); // to ma trn mxn kiu u tin
// hng t mng 1 chiu
Matrix(const Matrix& M);
~Matrix() { deleteData(); }

_T& operator()(int i, int j) { return data[i][j]; }


};

120
V. Kiu d liu tru tng

nh ngha cc hm ca Matrix
template <class _T> void Matrix<_T>::createData()
{
data = new _T *[rows];
for (int i = 0; i < rows; i++)
data[i] = new _T[cols];
}

template <class _T> void Matrix<_T>::deleteData()


{
if (data == 0) return;
for (int i = 0; i < rows; i++)
delete []data[i];
delete data;
}

121
V. Kiu d liu tru tng

Cc yu cu:
Hon thnh tt c cc hm ca class Matrix
B sung ton t gn, v cng ma trn
Xy dng class Graphic m t th c hng khng trng s k
tha t Matrix v c th s dng trong on biu thc sau:

int M[] = {
0, 1, 1, 0, 1,
1, 0, 1, 0, 0,
1, 0, 0, 1, 1,
0, 0, 1, 0, 1,
0, 1, 0, 1, 0
};
Graphic g(5, M);
g.DFT(2); // in ra mn hnh ch s cc nh
// theo chiu su bt u t nh 2

122
VI. Cc cu trc d liu c bn

Array, BoundStack v
BoundQueue
LinkedList

Binary Search Tree

Link n file code y :


https://docs.google.com/open?id=0B4vBa0QnLWSraGRyVkJKaFUwekE

123
VI. Cc cu trc d liu c bn

template <class T> class Array {


protected:
T *data;
int size;
public:
Array();
Array(int length);
Array(const Array& a);
~Array();
public:
void CopyFrom(T *);
int Size();
public:
T& operator[](int index);
Array& operator=(const Array& a);
};

124
VI. Cc cu trc d liu c bn

template <class T> class BoundStack : protected Array<T> {


int top;
public:
BoundStack();
BoundStack(int size);
public:
void Push(T value);
T Pop();
T Top();

int IsEmpty();
int IsFull();
int Count();
T operator[](int index);
};

125
VI. Cc cu trc d liu c bn

template <class T> class BoundQueue : protected Array<T> {


int front, rear;
int count;

void Inc(int &i);


public:
BoundQueue();
BoundQueue(int size);
public:
void Enqueue(T value);
T Dequeue();

int IsEmpty();
int IsFull();
int Count();

T operator[](int index);
};

126
VI. Cc cu trc d liu c bn

ng dng stack trong QuickSort


template <class T> class Sort {
class Segment {
public:
int Lb, Ub;
Segment() { }
Segment(int l, int r) : Lb(l), Ub(r) { }

void Swap(T &a, T &b) { T t = a; a = b; b = t; }


int DoPart(Array<T> &); // Phn on Array

// Sp xp Array trong on [Lb, Ub] bng InsertSort


void DoSort(Array<T> &);
}; // Segment
public:
static void DoSort(Array<T> &);
};

127
VI. Cc cu trc d liu c bn

ng dng stack trong QuickSort


template <class T> void Sort<T>::DoSort(Array<T> &a)
{
int n0 = 10, len = a.Size();
BoundStack<Segment> s(len/n0 + 1);
s.Push(Segment(0, len - 1));

while (!s.IsEmpty()) {
Segment b = s.Pop();
if (b.Ub - b.Lb + 1 > n0) {
int j = b.DoPart(a);

if (b.Lb < j - 1) s.Push(Part(b.Lb, j - 1));


if (b.Ub > j + 1) s.Push(Part(j + 1, b.Ub));
} else
b.DoSort(a);
}
}

128
VI. Cc cu trc d liu c bn

S dng class Sort


#include "stdafx.h"
#include "ds.h"
#include <iostream>
using namespace std;

int main()
{
int v[] = { 7, 5, 4, 8, 9, 1, 3, 2, 0, 6 };
Array<int> a(10);
a.CopyFrom(v);

Sort<int>::DoSort(a);

for (int i = 0; i < a.Size(); i++) cout << a[i] << ' ';
}

129
VI. Cc cu trc d liu c bn

class LinkedList
template <class _Ti> class LinkedList
{
protected:
// on nh ngha cc lp con //
baseList<_Ti> h;
int n;
public:
LinkedList() : n(0) { }
~LinkedList() { h.makeEmpty(); }

int IsEmpty() { return n == 0; }


int Count() { return n; }

_Ti PopBegin();
_Ti PopEnd();
void RemoveAll() { h.makeEmpty(); n = 0; }
void PushBegin(const _Ti& i);
void PushEnd(const _Ti& i)
};

130
VI. Cc cu trc d liu c bn

Cc lp con
class baseList {
public:
baseList *next, *prev;
baseList() { next = prev = this; }

void insertAfter(const _Ti& info);


void remove();
void makeEmpty();
};

class infoList : public baseList {


public:
_Ti info;
infoList(const _Ti& i) : info(i) { }
};

131
VI. Cc cu trc d liu c bn

ng dng i c s m
std::string IntToBin(int x)
{
std::string s;
LinkedList<char> stack;

do {
char c = (char)((x & 1) + 48);
x >>= 1;
stack.PushBegin(c);
} while (x);

while (!stack.IsEmpty())
s += stack.PopBegin();
return s;
}

132
VI. Cc cu trc d liu c bn

class c s
template <class _Ti> class baseBST {
public:
baseBST() { }
virtual baseBST * insert(const _Ti& ) = 0;
virtual baseBST * contents(const _Ti& ) { return 0 ; }
virtual baseBST * remove(const _Ti& ) { return this; }
virtual baseBST * getSuccessor() { return 0; }
virtual void makeEmpty() { }
virtual _Ti * Info() { return 0; }
};

template <class _Ti>


int _compare(const _Ti& a, const _Ti& b)
{
return (a < b? -1: a > b);
}

133
VI. Cc cu trc d liu c bn

class BST
template <class _Ti, int comp(const _Ti&, const _Ti& ) = _compare>
class BST {
// on nh ngha cc class con //
nullBST nullBST;
baseBST<_Ti> * root;
public:
BST() { root = (baseBST<_Ti> *)&nullBST; }
~BST() { root->makeEmpty(); }

void Insert(const _Ti& info) {


root = root->insert(info);
}
void Delete(const _Ti& i) {
root = root->remove(i);
}
bool Contents(const _Ti& i) {
return root->contents(i);
}
};

134
VI. Cc cu trc d liu c bn

nh ngha cc class con


class nullBST : baseBST<_Ti> {
public:
baseBST * insert(const _Ti& i) { return new infoBST(i, this); }
};

class infoBST : baseBST<_Ti> {


_Ti info;
baseBST *left, *right;
public:
infoBST(const _Ti& i, baseBST *nullBST) : info(i) {
left = right = nullBST;
}

void makeEmpty();
baseBST * insert(const _Ti& );
baseBST * contents(const _Ti& );
baseBST * remove(const _Ti& );
baseBST * getSuccessor();
};

135
VI. Cc cu trc d liu c bn

ng dng kim tra khai bo bin


#include "ds.h"
#include <iostream>
using namespace std;

class VarDef
{
public:
string name;
int line;
VarDef() { }
VarDef(const char* s, int l) : name(s), line(l) { }

static int compare(const VarDef& a, const VarDef& b)


{
return a.name.compare(b.name);
}
};

136
VI. Cc cu trc d liu c bn

ng dng kim tra khai bo bin


void main()
{
char vars[][100] =
{ "x", "y", "a", "b", "x", "k", "i", "k", "m", "n" };
BST<VarDef, VarDef::compare> bst;
BoundQueue<VarDef> e(10);

for (int i = 0; i < 10; i++) {


VarDef v(vars[i], i + 1);
if (!bst.Contents(v)) bst.Insert(v);
else e.Enqueue(v);
}

for (int i = 0; i < e.Count(); i++) {


VarDef v(e[i]);
cout << "redefinition variable " << v.name.data()
<< " in line " << v.line << endl;
}
}

137

You might also like