COS321

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

T

Biên

www.hutech.edu.vn
*1.2021.COS321*
- :
tailieuhoctap@hutech.edu.vn
I

...................................................................................................................I
.......................................................................................................... III
............................................................................................... 1
1.1 TÓM LÝ ........................................................................................... 1
1.1.1 Hàm ............................................................................................................. 1
............................................................................................................ 4
......................................................................................................... 5
1.2 HÀNH ........................................................................................... 6
...................................................................................... 6
................................................................................ 8
....................................................................................................... 10
1.3 HÀNH NÂNG CAO ..................................................................................... 12
..................................................................................... 15
2.1 TÓM LÝ ......................................................................................... 15
...................................................................................... 15
..................................................................... 16
2.2 HÀNH ......................................................................................... 17
......................................................................... 17
.................................................................. 20
2.3 HÀNH NÂNG CAO ..................................................................................... 22
TRÚC QUEUE VÀ STACK ........................................................................... 24
3.1 TÓM LÝ ......................................................................................... 24
......................................................................................... 24
3.1.2 Queue (h i) ........................................................................................ 24
3.2 HÀNH ......................................................................................... 25
........................................................................................... 25
.......................................................................................... 27
RÚC CÂY .................................................... 28
4.1 TÓM LÝ ......................................................................................... 28
.................................................................................................. 28
4.1.2 Các thao tác ................................................................................................ 29
4.2 HÀNH ......................................................................................... 29
............................................................................................... 29
................................................................................. 30
BÀI 5: .............................................................. 34
5.1 TÓM LÝ ......................................................................................... 34
................................................................................................... 34
II
5.1.2 Các thao tác .................................................................................................34
5.2 HÀNH ......................................................................................... 38
5.3 TRA HÀNH ...................................................................................... 45
TÀI THAM ............................................................................................. 46
III

H c xong môn này, sinh viên ph i n c các v sau:

B UY.

B i 4:

C t v logic v ki n th c to n h n.
IV

u t i li n l p và g ch chân nh ng v không
hi c t i li nl i.
BÀI 1: 1

BÀI 1:

1.1 TÓM T T LÝ THUY T


1.1.1 Hàm

C C
n
(không có hàm con) Cách 1 Cách 2

n Khai báo n n
ng s ng s ng s

Khai báo các hàm con Các hàm con


(nguyên m u hàm);

Hàm main() Hàm main() Hàm main()


{ { {
Các câu l nh; Các câu l nh (bao Các câu l nh (bao
} g m câu l nh g i hàm g m câu l nh g i hàm
con ra th c hi n); con ra th c hi n);
} }

Các hàm con


2 BÀI 1:

Tên hàm,

void Ten_Ham(danh sach


tham so neu co)
{

}
BÀI 1: 3

KieuDL Ten_Ham (danh sach


tham so neu co)
{
KieuDL kq;

return kq;
}

không thay
4 BÀI 1:

sau

xong.

1.1.2 M ng
M ng là m t t p h p các ph n t c nh có cùng m t ki u d li u, g i là ki u
ph n t . Ki u ph n t có th là có các ki u b t k : ký t , s , chu i ký t
khi ta s d ng ki u m làm ki u ph n t cho m t m ng h p này
ta g i là m ng c a m ng hay m ng nhi u chi u). Ta có th chia m ng làm 2 lo i:
m ng m t chi u và m ng nhi u chi u.

Cách khai báo m ng:

ng minh (s ph n t nh):

Cú pháp: <ki u d li u> <tên m ng> [<s ph n t >];

- <Tên m ng> theo quy t t tên c a danh bi


a bi n m ng.

- [<S ph n t >]: là m t h ng s nguyên, cho bi t s ng ph n t t


m c c a m ng).
BÀI 1: 5
- <Ki u d li u >: là ki u d li u c a m i ph n t c a m ng

ng minh (s ph n t nh)
Cú pháp: <ki u d li u> <tên m ng> [ ];

Ki c áp d ng h p: v a khai báo v a
gán giá tr , ho c khai báo m ng là tham s hình th c c a hàm.

1.1.3 quy

- Ta có: S(n 1) S(n) = S(n 1) + n


quy.

- Khi n = 0: S(0) = 0

chính nó.

}
quy

}
6 BÀI 1:

1.2 TH N
1.2.1 M ng dãy s nguyên
Bài 1:

a.

b.

c.

d.

e. T

#include <stdio.h>
#define MAX 100
/*----------------------------------

*/
void NhapSL(int &n)
{
do{

}while (n<=0 || n>MAX);


}
/*------------------------------------

*/
void NhapMang (int a[], int n)
{
for (int i = 0; i < n; i ++)
{

}
BÀI 1: 7
}
/*------------------------------------

*/
void XuatMang (int a[], int n)
{
\nMang gom cac phan tu:\
for (int i = 0; i < n; i ++)

}
// ------------------------------------

int TimTuyenTinh(int a[], int n, int X){...}


void SapXep(int a[], int n){...}
int TimNhiPhan(int a[], int n, int X){..}
//==============================
int main()
{
int a[MAX], n;

NhapMang (a,n);
XuatMang (a,n);

int tong = TinhTong(a, n);


float tbc = TinhTrungBinhCong(a, n);

%d\
\

int X;

printf("Nhap gia tri can tim: "); scanf("%d", &X);

int vt=TimTuyenTinh(a,n,X);

if (vt==-1)
printf("Khong tim thay");
8 BÀI 1:

else
printf("Tim thay %d tai vi tri %d",X, vt);

sapxep(a,n);
printf("Mang sau khi sap tang dan:"); XuatMang(a,n);

printf("Nhap gia tri can tim: "); scanf("%d", &X);


vt=TimNhiPhan(a,n,X);

return 0;
}

1.2.2 M ng ph n t ki u struct

Bài 1:
(int), tên sách (char[40]), giá (float).

a.

b.

c.

d.

e.

typedef
{

} ;
BÀI 1: 9
VD:
typedef struct CuonSach
{
int masach;
char tensach[40];
float gia;
}Sach;

void nhap1Sach(Sach &x)

void xuat1Sach(Sach x)

void nhapn(int &n)

void nhapDS(Sach a[], int n)

void xuatDS(Sach a[], int n)

int timTuanTu(Sach a[], int n, int X)

int timNhiPhan(Sach a[], int n, int X)

void nhap1Sach(Sach &x)

void xuat1Sach(Sach x)

void nhapn(int &n)

void nhapDS(Sach a[], int n)


{
-
hàm
for(int i=0; i<n; i++)
{
10 BÀI 1:

nhap1Sach(a[i]);
}
}

-
..., n-

- Hàm tì

int timTuanTu(Sach a[], int n, int X)

int timNhiPhan(Sach a[], int n, int X)

hàm tìm).

1.2.3 quy
Bài 1:

- 1) + n

int Tong(int n)
{
BÀI 1: 11
if (n == 0)
return 0;

return Tong(n 1) + n;
}

Bài 2:

- 1) + f(n 2)

int Fibonacci(int n)
{

if (n == 0 || n == 1)
return 1;

return Fibonacci(n 1) + Fibonacci(n 2);


}

Bài 3:

b, b)

- a)

int UCLN(int a, int b)


{

if (a == b)
return a;
12 BÀI 1:

if (a > b)
return UCLN(a b, b);
else
return UCLN(a, b a);
}

Bài 4:

- -

int Tong(int a[], int n)


{
if (n == 1)
return a[0];
return Tong(a, n-1) + a[n-1];
}

1.3 TH C HÀNH NÂNG CAO


Bài 1: Hãy

Bài 2:

a.

b.

c.

d.

e.

f.

g. Cho
BÀI 1: 13
h.

i.

j.

Bài 3:

a.

b.

c.

d.

e.

f.

g.

h.

i.

Bài 4:

Bài 5: +n^3

Bài 6: -1+2-3+4- -1)^n n

Bài 7: +n/(n+1)

Bài 8:
1×2×3+2×3×4+ +n×(n+1)×(n+2)

Bài 9: +
1/(n×(n+1))

Bài 10:

C(n ,k) = C(n-1, k) + C(n-1, k-


14 BÀI 1:

Bài 11:

Bài 12:

Bài 13:

Bài 14:

Bài 15:

Bài 16:
BÀI 2: 15

BÀI 2:

- Hi c c u tr c danh s ch.

- C t danh s p: c t danh s ch theo ki u k ti p v


ki u liên k t.

- Hi n th c danh s ch liên k t, p d ng v o b i to n th c t .

2.1 TÓM T T LÝ THUY T


2.1.1 C u trúc danh sách

các nút trong danh

- danh s ch k

-
DSLK VÒNG.
16 BÀI 2:

2.1.2 c u trúc danh sách liên k


a.

- Next

b.

typedef struct node{

DataType info;
struct node * next;

}Node;
Node* pHead;
BÀI 2: 17
c.

2.2 TH N
2.2.1 i dãy s nguyên

Bài 1:

a.

b.

c.

d.

e.

f.

g.

h.

theo cách khác.

-
18 BÀI 2:

struct node
{
datatype info;
struct node* next;
};
typedef struct node Node;

- int.

void init (Node* &phead)

int isEmpty(Node* phead)

Node* createNode(int x)

void insertFirst(Node* &phead, int x)

void insertLast(Node* &phead, int x)

void showList(Node* phead)

Cách 1

void input(Node* &phead)


{
init(phead);

int n, x;
printf("Nhap so luong phan tu: "); scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Nhap so can them vao danh sach: ");
scanf("%d", &x);
insertFirst(phead, x);
}
}

-
BÀI 2: 19
- insertFirst(phead, x)
insertLast(phead, x)

Cách 2:

void input(Node* &phead)


{
int x;
do{
printf("Nhap gia tri x: "); scanf("%d", &x);
if(x!=0)
{
Node* p=CreateNode(x);
if (p!=NULL)
InsertFirst(phead, x);
}
}while(x!=0);
}

Cách 3:

//Hàm chính
int main()
{

Node* phead;
init(phead);

int chon, x;
do{
system(cls);

\
\
20 BÀI 2:

\
\

switch (chon){
case 1:
printf("Nhap gia tri phan tu can them:");
scanf("%d", &x);
insertFirst(phead, x);
break;
case 2:
printf("Nhap gia tri phan tu can them:");
scanf("%d", &x);
insertLast(phead, x);
break;
case 3:
showList(phead);
break;
default: chon=0;
}
}while (chon!=0);
return 0;
}

2.2.2 i danh sách sinh viên


Bài 1:

Mã sv -

-
BÀI 2: 21

a.

b.

c.

d. Tìm sinh viên có mã là X.

e.

f.
p

g.

h.

i.

j. Xóa SV có mã là X.

typedef struct SinhVien


{
char masv[10];
char hoten[40];
float dtb;
}SV ;
typedef struct node
{
SV info;
struct node* next;
}Node;

nhap1SV(SV &x)
22 BÀI 2:

xuat1SV(SV x)

void init (Node* &phead)

int isEmpty(Node* phead)

Node* createNode(SV x)

void insertFirst(Node* &phead, SV x)

void insertLast(Node* &phead, SV x)

void showList(Node* phead).

void inputList(Node* &phead)


{
int n;
printf("Nhap so luong sinh vien:"); scanf("%d", &n);
for(int i=0; i<n; i++)
{
SV x;
printf("Nhap thong tin SV can them:");
nhap1SV(x);
insertFirst(phead, x);

}
}

2.3 TH C HÀNH NÂNG CAO


Bài 1:

a.

b.

c.

d.

e.
BÀI 2: 23
f. .

Bài 2:

Bài 3:
nguyên.

Bài 4:
24 BÀI 3:

BÀI 3: C

3.1 TÓM T T LÝ THUY T


3.1.1 p)

Stack. Trên Stack


Stack ho v o c - LIFO
(Last In First Out).

Hai thao tác chính trên Stack:

- T push Stack.

- T pop Stack.

3.1.2 Queue (h i)

Queue (h
phép thêm vào

trúc FIFO (First In First Out). Hai thao

- insert

- remove
BÀI 3: 25
3.2 TH N
3.2.1 ng d ng Stack

Bài 1: Stack.

- Stack Stack

ài.

typedef struct node


{

DataType info;
struct node * next;

}Node;
typedef Node* STACK;

-
26 BÀI 3:

- n

void Convert(int n, Stack &s){


int sodu;

{ sodu=n%2;

n=n/2;
}

-
màn hình.

void Output(Stack s){

- Hàm chính main()

int main()
{

STACK s;
init(s);

printf("Nhap so o he thap phan: "); scanf("%d", &n);

Convert(n, s);

Output(s) ;
return 0 ;
}

Bài 2:

Bài 3:
BÀI 3: VÀ STACK 27
Bài 4:

Bài 5:

3.2.2 ng d ng Queue

Bài 1:

a.

b.

c.

d.

e.

typedef struct benhnhan{

} BenhNhan ;
typedef struct node
{
BenhNhan info;
struct node * next;
}Node;
typedef struct QUEUE
{
Node* pHead;
Node* pTail;
}Queue;

-
g.
28 BÀI 4:

BÀI 4:

4.1 TÓM T T LÝ THUY T


4.1.1
con.

-
BÀI 4: 29

4.1.2 Các thao tác

- CreateNode, FreeNode, Init, IsEmpty

- InsertLeft, InsertRight

- DeleteLeft, DeleteRight

- PreOrder, InOrder, PostOrder

- Search

- ClearTree.

4.2 TH N
4.2.1 Cây nh phân

Bài 1:

a.

b.

c.

typedef struct node


{
int info;
struct node * left;
struct node * right;
} Node;
30 BÀI 4:

- root

- nguyên.

void Create Tree(Node* & proot, int a[], int begin, int end )
{
if (begin>end) return;
int i = (begin+end)/2;

proot = CreateNode(a[i]);

CreateTree(proot ->left, a, begin, i-1);

CreateTree(proot ->right, a, i+1, end);


}
//trong hàm main() : begin=0, end=n-1 ;

void NLR(Node * proot){


if (proot!=NULL){
root ->info);
NLR(proot ->left);
NLR(proot ->right);
}
}

4.2.2 Cây nh phân tìm ki m

Bài 2:

a.
BÀI 4: 31
b.

c.

d.

a.

typedef struct node{


int info;
struct node *left;
struct node *right;
}Node;
void Init(Node* &proot){ proot=NULL ;}
Node* CreateNode(int x){
NODEPTR p=new Node;
p->info=x; p->left=NULL; p->right=NULL;
return p;
}

b.

void Insert(NODEPTR &proot, int x) {

proot = CreateNode(x);
else
if (x==proot-
return;
if (x<proot->info)
Insert(proot->left, x);
else
Insert(proot->right, x);
}
int Remove(NODEPTR &proot, int x) {
if ( proot == NULL)

if (proot->info >x) //tìm và xóa bên trái


return Remove(proot->left, x);
32 BÀI 4:

if (proot->info <x)
return Remove(proot->right, x);
->info==x)
Node* p, f, rp;
p = proot;

if ( proot->left == NULL) //có 1 cây con


proot = proot->right;
else if (proot->right == NULL) //có 1 cây con
proot = proot->left;
else { //TH
f = p;
rp = p->right; ->right
while ( rp->left != NULL) {

rp = rp->left; //rp qua bên trái


nút con trái là null
p->info = rp-

f->right = rp->right;
else //f != p
f->left = rp->right;

}
delete p; //xoá nút p
return TRUE;

c.

Bài 3:

a.

b. cây c ng

c.

d. t cha c a n
BÀI 4: 33
e. t lá, n a cây

f. a cây

g.

h. T nh
34 BÀI 5:

BÀI 5:

5.1 TÓM T T LÝ THUY T


5.1.1 Khái ni m
- Cây AVL do Adelson

a cây con bên trái


chênh nhau không quá 1.

- - balance factor)

bf = height (nút con trái) height

bf(p) = 0

bf(p) = 1

bf(p) = -1

5.1.2 Các thao tác

-
BÀI 5: 35

- Tác v

Các thao tác:

o thoát

o thoát

-
36 BÀI 5:

Các thao tác:

o thoát

o thoát

-
BÀI 5: 37

- -

- -
38 BÀI 5:

5.2 TH N
Bài 1:
thao tác:

a.

b.

c.

a.

typedef struct node{


int info;
struct node *left;
struct node *right;

}Node;

void Init(Node* &proot){ proot=NULL ;}

Node* CreateNode(int x){


NODEPTR p = new Node;
If(p==NULL) return 0;
p->info = x;
p->left = NULL;
p->right = NULL;
p->height = 1;
return p;
}
BÀI 5: 39
b.

-
trong cây.

int getHeight(node *p)


{
if (p == NULL)
return 0;
return p->height;
}

int balanceFactor(node *p)


{
if (p == NULL)
return 0;

return getHeight(p->left) - getHeight(p->right);


}

int max(int a, int b)


{
return a > b ? a : b;
}

-
40 BÀI 5:

node *rotateLeft(node *p)


{

if (p == NULL)
return NULL;

// TH2
if (p->right == NULL)
return NULL;

node *pivot = p->right;

p->right = pivot->left;

pivot->left = p;

p->height = 1 + max(getHeight(p->right), getHeight(p->left));


pivot->height = 1 + max(getHeight(pivot->right), getHeight(pivot->left));

return pivot;
}

node *rotateRight(node *p)


{

if (p == NULL)
return NULL;

if (p->left == NULL)
BÀI 5: 41
return NULL;

node *pivot = p->left;

p->left = pivot->right;

pivot->right = p;

p->height = 1 + max(getHeight(p->right), getHeight(p->left));


pivot->height = 1 + max(getHeight(pivot->right), getHeight(pivot->left));

return pivot;
}

node * balance(node *p)


{
if (balanceFactor(p) < -1)
{
if (balanceFactor(p->right) > 0)
{
p->right = rotateRight(p->right);
}
p = rotateLeft(p);
}
else if (balanceFactor(p) > 1)
{
if (balanceFactor(p->left) < 0)
{
p->left = rotateLeft(p->left);
}
p = rotateRight(p);
}
return p;
}
42 BÀI 5:

node *insert(node *p, int x)


{
if (p == NULL)
return createNode(x);

if (x < p->data) // add to the right side


p->left = insert(p->left, x);
else if (x > p->data) // add to the left side
p->right = insert(p->right, x);

p->height = 1 + max(getHeight(p->left), getHeight(p->right));

return balance(p); // balance the tree


}

node * minValueNode(node* p)
{
node* current = p;

while (current && current->left != NULL)


current = current->left;
BÀI 5: 43

return current;
}

node * removeNode(node *root, int x)


{

if (root == NULL)
return root;

if (root->data > x)
root->left = deleteNode(root->left, x);

else if (root->data < x)


root->right = deleteNode(root->right, x);

else
{

if (root->left == NULL)
{
node *temp = root->right;
delete root;
return temp;
}
else if (root->right == NULL)
{
node *temp = root->left;
delete root;
return temp;
}
44 BÀI 5:

node *temp = minValueNode(root->right);

root->data = temp->data;

// Xóa nút temp


root->right = deleteNode(root->right, temp->data);
}

if (root == NULL)
return root;

root->height = 1 + max(getHeight(root->left), getHeight(root->right));

return balance(root);

c.

node * createAVL(int a[], int n)

node *root = NULL;

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

root = insert(root, a[i]);

return root;

-
BÀI 5: 45
Bài 2:

5.3 KI M TRA TH C HÀNH


46

1.

2.
TPHCM.

3. Lê Minh Hoàng. (1999 -

4. Richard Neapolitan and Kumarss (2004). Foundations of Algorithms Using C++


Pseudocode. Jones and Bartlett Publishers.

5.

6.

7. ThS. V

You might also like