Professional Documents
Culture Documents
Cơ Sở Dữ Liệu Và Cấu Trúc Dư Liệu
Cơ Sở Dữ Liệu Và Cấu Trúc Dư Liệu
Cơ Sở Dữ Liệu Và Cấu Trúc Dư Liệu
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
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:
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
Tên hàm,
}
BÀI 1: 3
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.
ng minh (s ph n t nh):
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
- 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{
*/
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 ++)
}
// ------------------------------------
NhapMang (a,n);
XuatMang (a,n);
%d\
\
int 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);
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 xuat1Sach(Sach x)
void xuat1Sach(Sach x)
nhap1Sach(a[i]);
}
}
-
..., n-
- Hàm tì
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;
Bài 3:
b, b)
- a)
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:
- -
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 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:
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.
- Hi n th c danh s ch liên k t, p d ng v o b i to n th c t .
- danh s ch k
-
DSLK VÒNG.
16 BÀI 2:
- Next
b.
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.
-
18 BÀI 2:
struct node
{
datatype info;
struct node* next;
};
typedef struct node Node;
- int.
Node* createNode(int x)
Cách 1
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:
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;
}
Mã sv -
-
BÀI 2: 21
a.
b.
c.
e.
f.
p
g.
h.
i.
j. Xóa SV có mã là X.
nhap1SV(SV &x)
22 BÀI 2:
xuat1SV(SV x)
Node* createNode(SV x)
}
}
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
- T push Stack.
- T pop Stack.
3.1.2 Queue (h i)
Queue (h
phép thêm vào
- insert
- remove
BÀI 3: 25
3.2 TH N
3.2.1 ng d ng Stack
Bài 1: Stack.
- Stack Stack
ài.
DataType info;
struct node * next;
}Node;
typedef Node* STACK;
-
26 BÀI 3:
- n
{ sodu=n%2;
n=n/2;
}
-
màn hình.
int main()
{
STACK s;
init(s);
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.
} 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:
-
BÀI 4: 29
- InsertLeft, InsertRight
- DeleteLeft, DeleteRight
- Search
- ClearTree.
4.2 TH N
4.2.1 Cây nh phân
Bài 1:
a.
b.
c.
- 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]);
Bài 2:
a.
BÀI 4: 31
b.
c.
d.
a.
b.
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)
return Remove(proot->right, x);
->info==x)
Node* p, f, rp;
p = proot;
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:
- - balance factor)
bf(p) = 0
bf(p) = 1
bf(p) = -1
-
BÀI 5: 35
- Tác v
o thoát
o thoát
-
36 BÀI 5:
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.
}Node;
-
trong cây.
-
40 BÀI 5:
if (p == NULL)
return NULL;
// TH2
if (p->right == NULL)
return NULL;
p->right = pivot->left;
pivot->left = p;
return pivot;
}
if (p == NULL)
return NULL;
if (p->left == NULL)
BÀI 5: 41
return NULL;
p->left = pivot->right;
pivot->right = p;
return pivot;
}
node * minValueNode(node* p)
{
node* current = p;
return current;
}
if (root == NULL)
return root;
if (root->data > x)
root->left = deleteNode(root->left, 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:
root->data = temp->data;
if (root == NULL)
return root;
return balance(root);
c.
return root;
-
BÀI 5: 45
Bài 2:
1.
2.
TPHCM.
5.
6.
7. ThS. V