LÝ THUYẾT CTDL

You might also like

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

LÝ THUYẾT CTDL & GT

1. DANH SÁCH LIÊN KẾT ĐƠN


- Định nghĩa DSLK đơn
typedef struct CELL *LIST;
struct CELL{
int key;
LIST next;
};
LIST L;
- Hàm khởi tạo danh sách ListInitialize
void ListInitialize(LIST &L){
L = null;
}
- Hàm ListSearch
LIST ListSearch(LIST L, int k) {
LIST x;
x=L;
while(x!=NULL && x->key!=k) x=x->next;
return x;
}
- Hàm ListInsert
void ListInsert(LIST &L, int k) {
LIST x;
x=new(CELL);
x->key=k;
x->next=L;
L=x;
}

- Hàm ListDelete
void ListDelete(LIST &L,int k) {
LIST x, y;
if(L!= NULL) {
y= NULL; x =L;
while(x!=NULL && x->key!=k) {
y=x; x=x->next;
}
if(x!=NULL) {
if(y==NULL) L=x->next; //nút cần xóa ở đầu DS
else y->next=x->next; // Loại bỏ nút x khỏi DS
delete(x); // Xóa khỏi bộ nhớ
}
}
}
- Duyệt DS: ListWalk
void ListWalk(LIST L) {
if(L!=NULL) {
cout<key)<<' ';
ListWalk(L->next);
}
}
2. DANH SÁCH LIÊN KẾT KÉP
- Định nghĩa DSLK kép
typedef struct CELL *LIST;
struct CELL {
int key;
LIST prev, next;
};
LIST L;
- Hàm khởi tạo DSLK đôi
void ListInitialize(LIST &L) {
L=NULL;
}
- Hàm ListSearch
LIST ListSearch(LIST L, int k) {
LIST x;
x=L;
while(x!=NULL && x->key!=k) x=x->next;
return x;
}
- Hàm ListInsert
void ListInsert(LIST &L, int k) {
LIST x;
x=new(CELL); x->key=k;
x->next=NULL; x->prev=NULL x->next=L;
if(L!=NULL) L->prev=x;
L = x;
x->prev=NULL;
}
- Hàm ListDelete
void ListDelete(LIST &L, int k) {
LIST x=L;
while(x!=NULL && x->key!=k) x=x->next;
if(x!=NULL) {
if(x->prev!=NULL) x->prev->next=x->next;
else L = x->next;
if(x->next!=NULL) x->next->prev=x->prev;
delete x;
}
}
3. STACK ( Last in, First out LIFO ) , ĐPT là O(1)
 Biểu diễn Stack bằng Mảng
- Định nghĩa Stack
#define MAX 200
struct STACK{
int a[MAX];
int top;
int N; //kich thuoc stack
};
STACK S;
- Hàm StackInitialize
void StackInitialize(STACK &S) {
S.top=0;
S.N=30;
}
- Hàm StackEmpty
bool StackEmpty(STACK S) {
if(S.top==0) return true;
else return false;
}
- Hàm StackFull
bool StackFull(STACK S) {
if(S.top==S.N) return true;
else return false;
}
- Push
void Push(STACK &S, int x) {
if(StackFull(S)) cout<<"overflows";
else{
S.top=S.top+1;
S.a[S.top]=x;
}
}
- Pop
int pop(STACK &S) {
if(StackEmpty(S)) { cout<<"underflows"; return -1;}
else {
S.top=S.top-1;
return S.a[S.top+1];
}
}
 Biểu diễn Stack bằng DSLK
- Định nghĩa Stack
typedef struct CELL *STACK;
struct CELL {
int Obj;
STACK next;
};
STACK S;
- Hàm StackInitialize
void StackInitialize(STACK & S) {
S=NULL;
}
- Hàm StackEmpty
bool StackEmpty(STACK S) {
if(S==NULL) return true;
else return false;
}
- Hàm StackFull
bool StackFull(STACK S) {
return false;
}
- Push
void Push(STACK &S, int x) {
STACK p;
p=new(CELL);
p->Obj=x;
p->next=S;
S=p;
}
- Pop
int Pop(STACK &S) {
int x; STACK p;
if(StackEmpty(S)) {cout<<"underflows"; return -1;}
else{
p=S;
S=p->next;
x=p->Obj;
delete(p);
return x;
}
}
4. QUEUE ( FIRST IN, FIRST OUT FIFO ) , ĐPT là O(1)
 BIỂU DIỄN QUEUE BẰNG MẢNG
- Định nghĩa Queue
typedef struct QUEUE{
int a[MAX];
int head, tail;
int n;
};
QUEUE Q;
- Hàm QueueInitialize
void QueueInitialize(QUEUE &Q) {
Q.head=Q.tail=1;
Q.n=20; //tùy chọn
}
- Hàm Enqueue
void Enqueue(QUEUE &Q, int x) {
if(queue_full(Q)) cout<<"overflow";
else{
Q.a[Q.tail]=x;
if(Q.tail==Q.n) Q.tail=1;
else Q.tail=Q.tail+1;
}
}
- Hàm Dequeue
int dequeue(QUEUE &Q) {
int x;
if(queue_empty(Q)){ cout<<"underflow"; return -1;}
else {
x=Q.a[Q.head];
if(Q.head==Q.n) Q.head=1;
else Q.head=Q.head+1;
return x;
}
}
- Hàm QueueEmpty
bool QueueEmpty(QUEUE Q) {
if(Q.head==Q.tail) return true;
else return fasle;
}
- Hàm QueueFull
bool QueueFull(QUEUE Q) {
if((Q.tail==Q.n&&Q.head==1)||(Q.head==Q.tail+1))
return true;
else return fasle;
}
 BIỂU DIỄN QUEUE BẰNG DSLK
- Định nghĩa Queue
typedef struct CELL *LIST;
struct CELL{
int Obj;
LIST next;
};
struct QUEUE{
LIST head, tail;
};
QUEUE Q;
- Hàm QueueInitialize
void QueueInitialize(QUEUE &Q) {
Q.head=Q.tail=NULL;
}
- Hàm Enqueue
void Enqueue(QUEUE &Q,int x) {
LIST p;
p=new(CELL);
p->Obj=x;
p->next=NULL;
if(Q.head==NULL) Q.head=p;
else Q.tail ->next=p;
Q.tail=p;
}
- Hàm Dequeue
int Dequeue(QUEUE &Q) {
int x;
LIST p;
if(QueueEmpty(Q)){cout<<"underflows"; return -1;}
else {
p=Q.head;
x=p->Obj;
if(Q.head==Q.tail) {Q.head=Q.tail=NULL;}
else Q.head=p->next;
delete p;
return x;
}
}
- Hàm QueueEmpty
bool QueueEmpty(QUEUE Q) {
if(Q.head==NULL) return true;
else return false;
}
5. TREE ( CÂY )
 CÂY NHỊ PHÂN
- Định nghĩa cây nhị phân
typedef struct CELL *TREE;
struct CELL {
int key; //trong thực tế có thêm nhiều dữ liệu khác
TREE left, right;
};
TREE T;
- Duyệt theo trung thứ tự ( inorder tree walk )
void InorderTreeWalk(TREE x) {
if(x!=NULL) {
InorderTreeWalk(x->left);
cout<key)<<' ';
InorderTreeWalk(x->right);
}
}
 CÂY NHỊ PHÂN TÌM KIẾM, ĐPT là O(h) với h là chiều cao cây
 Nếu y là một nút trong cây con trái của nút x thì key[y]< key[x]

 Nếu y là một nút trong cây con phải của nút x thì key[y]> key[x]

- Hàm khởi tạo cây TreeInitialize


void TreeInitialize(TREE &T) {
T=NULL;
}
- Hàm tìm một phần tử có khóa cho trước TreeSearch
TREE TreeSearch(TREE x,int k) {
if(x==NULL||k==x->key) return x;
if(kkey) return TreeSearch(x->left,k);
else return TreeSearch(x->right,k);
}
- Hàm tìm một phần tử có khóa nhỏ nhất TreeMinimum
TREE TreeMinimum(TREE x) {
if(x!=NULL) while(x->left!=NULL) x=x->left;
return x;
}
- Hàm tìm một phần tử có khóa lớn nhất TreeMaximum
TREE TreeMaximum(TREE x) {
if(x!=NULL) while(x->right!=NULL) x=x->right;
return x;
}
- Hàm chèn một nút vào cây TreeInsert
void TreeInsert(TREE &T, int k) {
if(T==NULL) {
T=new(CELL); T->key=k; T->left=NULL; T->right=NULL;
}
else if(kkey) TreeInsert(T->left, k);
else if(k>T->key) TreeInsert(T->right, k);
}
- Hàm chèn một nút vào cây TreeDelete
void TreeDelete(TREE &T, int k) {
if(T!=NULL)
if(kkey) TreeDelete(T->left,k);
else if(k>T->key) TreeDelete(T->right,k);
else if(T->right==NULL) T=T->left;
else if(T->left==NULL) T=T->right;
else T->key=DeleteMin(T->right);
}
int DeleteMin(TREE &T) {
int min;
if(T ->left==NULL) {
min=T ->key;
T=T ->right;
return min;
}
else return DeleteMin(T ->left);
}
 CÂY NHỊ PHÂN TÌM KIẾM CÂN BẰNG
- Hàm định nghĩa
typedef struct AVLNode *AVLTree;
struct AVLNode {
int balFactor;
int key;
AVLTree pLeft, pRight;
};
AVLTree T;
• Các hằng số biểu diễn quan hệ về độ cao các cây con
#define LH -1 //Cây con trái cao hơn
#define EH 0 //Hai cây con có chiều cao bằng nhau
#define RH 1 //Cây con phải cao hơn

- Quay đơn LEFT – LEFT


void rotateLL(AVLTree &T) {
AVLTree T1=T->pLeft;
T->pLeft=T1->pRight;
T1->pRight=T;
switch(T1->balFactor) {
case LH: T->balFactor=EH; T1->balFactor=EH; break;
case EH: T->balFactor=LH; T1->balFactor=RH; break;
}
T=T1;
}
- Quay đơn RIGHT – RIGHT
void rotateRR(AVLTree &T) {
AVLTree T1=T->pRight;
T->pRight=T1->pLeft;
T1->pLeft=T;
switch(T1->balFactor) {
case RH: T->balFactor=EH; T1->balFactor=EH; break;
case EH: T->balFactor=RH; T1->balFactor=LH; break;
}
T=T1;
}
- Quay kép LEFT – RIGHT
void rotateLR(AVLTree &T) {
AVLTree T1=T->pLeft, T2=T1->pRight;
T->pLeft=T2->pRight;
T2->pRight=T;
T1->pRight=T2->pLeft;
T2->pLeft=T1;
switch(T2->balFactor) {
case LH: T->balFactor=RH; T1->balFactor=EH; break;
case EH: T->balFactor=EH; T1->balFactor=EH; break;
case RH: T->balFactor=EH; T1->balFactor=LH; break;
}
T2->balFactor=EH;
T=T2;
}
- Quay đơn RIGHT – LEFT
void rotateRl(AVLTree &T) {
AVLTree T1=T->pRight, T2=T1->pLeft;
T->pRight=T2->pLeft;
T2->pLeft=T;
T1->pLeft=T2->pRight;
T2->pRight=T1;
switch(T2->balFactor) {
case RH: T->balFactor=LH; T1->balFactor=EH; break;
case EH: T->balFactor=EH; T1->balFactor=EH; break;
case LH: T->balFactor=EH; T1->balFactor=RH; break;
}
T2->balFactor=EH;
T=T2;
}
- Hàm cân bằng khi cây lệch trái
void balanceLeft(AVLTree &T) {
AVLTree T1=T->pLeft;
switch(T1->balFactor) {
case LH: rotateLL(T);
case EH: rotateLL(T);
case RH: rotateLR(T);
}
}
- Hàm cân bằng khi cây lệch phải
void balanceRight(AVLTree &T) {
AVLTree T1=T->pRight;
switch(T1->balFactor) {
case LH: rotateRL(T);
case EH: rotateRR(T);
case RH: rotateRR(T);
}
}
- Hàm chèn một phần tử vào cây cân bằng
int insertNode(AVLTree &T, int x) {
int res;
if(T) { if(T->key==x) return 0;
if(T->key>x){
res= insertNode(T->pLeft, x);
if(res<2) return res;
switch(T->balFactor) {
case RH: T->balFactor=EH; return 1;
case EH: T->balFactor=LH; return 2;
case LH: balanceLeft(T); return 1;
}
} else {
res= insertNode(T->pRight, x);
if(res<2) return res;
switch(T->balFactor) {
case LH: T->balFactor=EH; return 1;
case EH: T->balFactor=RH; return 2;
case RH: balanceRight(T); return 1;
}
}
T=new(AVLNode);
if(T==NULL) return -1;
T->key=x;
T->balFactor=EH;
T->Pleft=T->pRight=NULL;
return 2
}
- Hàm xóa một phần tử cây cân bằng
int deleteNode(AVLTree& T, int x) {
if (T == NULL) return 0;
int res;
if (T->key > x) {
res = deleteNode(T->pLeft, x);
if (res < 2) return res;
switch (T->balFactor) {
case LH: T->balFactor = EH; return 2;
case EH: T->balFactor = RH; return 1;
case RH: balanceRight(T); return 1;
}
} else if (T->key < x) {
res = deleteNode(T->pRight, x);
if (res < 2) return res;
switch (T->balFactor) {
case RH: T->balFactor = EH; return 2;
case EH: T->balFactor = LH; return 1;
case LH: balanceLeft(T); return 1;
}
} else {
// Tìm được node cần xóa
AVLNode* temp = T;
if (T->pLeft == NULL) {
T = T->pRight;
delete temp;
return 2;
}
if (T->pRight == NULL) {
T = T->pLeft;
delete temp;
return 2;
}
// Node có hai con
res = deleteLeftmost(T->pRight, T->key);
if (res < 2) return res;
switch (T->balFactor) {
case RH: T->balFactor = EH; return 1;
case EH: T->balFactor = LH; return 2;
case LH: balanceLeft(T); return 1;
}
}
return 1;
}

 SELECTIONSORT
void SelectionSort(int A[], int n) {
for(int i=0;i<n-1;i++){
int min =i;
for(int j=i+1;j<n;j++){
if(A[j]<A[min]) min=j; // Đổi thành A[j]>A[min] nếu giảm dần
swap(A[i],A[min]);
}
}

 INSERTIONSORT
void InsertionSort(int A[], int n) {
for(int j=1;j<n;j++){
int key = A[j];
int i = j -1;
while(i>=0 && A[i]>key) { // chuyển thành A[i] < key nếu giảm
dần
A[i+1]=A[i];
i=i-1;
}
A[i+1]=key;
}
}

 BUMBLESORT
void BubbleSort(int A[], unsigned n) {
for(int i=n-1;i>=1;i--) {
for(int j=0; j<=i-1; j++)
if(A[j]>A[j+1]) // Đổi thành A[j]<A[j+1] nếu giảm dần
Swap(A[j], A[j+1]);
}
}

 HEAPSORT
void heapify(int a[],int n,int i){
int left = 2*i + 1;
int right = 2*i + 2;
int largest = i;
if(left < n && a[left] > a[largest]){
largest = left;
}
if(right < n && a[right] > a[largest]){
largest = right;
}
if(largest != i){
swap(a[i],a[largest]);
heapify(a,n,largest);
}
}
//Sap xep tang dan
void HeapSort(int a[],int n){
//Xay dung MaxHeap
for(int i=n/2 -1;i>=0;i--){
heapify(a,n,i);
}
//
for(int i=n-1;i>=0;i--){
swap(a[i],a[0]);
heapify(a,i,0);
}
}
// Sap xep giam dan thi sua phan heapify. Cu the la so sanh 2 thang con la left va right nho hon
thang cha ( largest or smallest )
 MERGESORT
void merge(int a[], int l, int m,int r){
vector<int> x(a+l,a+m+1);
vector<int> y(a+m+1,a+r+1);
int i=0, j=0;
while(i < x.size() && j < y.size()){
if(x[i]<=y[j]){
a[l]=x[i];
++l;
++i;
}
else{
a[l]=y[j];
++l;
++j;
}
}
while(i < x.size()){
a[l]=x[i];
++l;
++i;
}
while(j < y.size()){
a[l]=y[j];
++l;
++j;
}
}
// phuong thuc tach mang
void MergeSort(int a[], int l, int r){
if(l>=r) return;
int m=(l+r)/2;
MergeSort(a,l,m);
MergeSort(a,m+1,r);
merge(a,l,m,r);
}
 QUICKSORT
int Partition(int A[], int p, int r) {
int i, j, x;
x=A[r];
i=p-1;
for(j=p; j<r ; j++)
if(A[j] <=x){
i=i+1;
exchange(A[i], A[j]); //hoán vị A[i] và A[j]
}
exchange(A[i+1], A[r]);
return i+1;
}

void QuickSort(int A[], int p, int r) {


int q;
if(p<r){
q=Partition(A,p,r);
QuickSort(A, p,q-1);
QuickSort(A, q+1,r);
}
}
 TÌM KIẾM TUẦN TỰ
bool Search(int A[MAX], int n, int k) {
for( int i = 0; i < n; i++)
if( A[i] == k)
return true;
return false;
}
 TÌM KIẾM NHỊ PHÂN
bool BinarySearch(int A[MAX], int n, int k) {
int i=0, j=n-1, m;
while(i<=j) {
m=(i+j)/2;
if(k==A[m]) return true;
else if(k>A[m]) i=m+1;
else j=m-1;
}
return false;
}

You might also like